aboutsummaryrefslogtreecommitdiffstats
path: root/awk
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-07-23 21:14:45 -0400
committerelioat <elioat@tilde.institute>2025-07-23 21:14:45 -0400
commit737312e341c6ad02cef0bfe49ebfac84fabeb5aa (patch)
treefca352e451a233a7022f97afefe74dac7a42da81 /awk
parentb57f9c561da3f159a65fbbb07edeca44ecbcd68e (diff)
downloadtour-737312e341c6ad02cef0bfe49ebfac84fabeb5aa.tar.gz
*
Diffstat (limited to 'awk')
-rw-r--r--awk/uxn/awk/uxnasm.awk390
1 files changed, 323 insertions, 67 deletions
diff --git a/awk/uxn/awk/uxnasm.awk b/awk/uxn/awk/uxnasm.awk
index d66886f..cfcdd00 100644
--- a/awk/uxn/awk/uxnasm.awk
+++ b/awk/uxn/awk/uxnasm.awk
@@ -1,7 +1,6 @@
#!/usr/bin/awk -f
# Uxntal Assembler in AWK - Two-Pass Implementation
-# Based on the C implementation by Devine Lu Linvega, Andrew Alderwick
BEGIN {
# Constants
@@ -16,6 +15,7 @@ BEGIN {
# Label and reference tracking
labels_len = 0
refs_len = 0
+ macros_len = 0
# Device tracking
devices_len = 0
@@ -23,6 +23,10 @@ BEGIN {
last_size = 0
current_device = ""
+ # Lambda tracking
+ lambda_len = 0
+ lambda_ptr = 0
+
# Opcode table
split("LIT INC POP NIP SWP ROT DUP OVR EQU NEQ GTH LTH JMP JCN JSR STH LDZ STZ LDR STR LDA STA DEI DEO ADD SUB MUL DIV AND ORA EOR SFT", ops)
@@ -31,17 +35,17 @@ BEGIN {
# Check arguments
if (ARGC < 3) {
- printf "usage: uxnasm [-v] input.tal output.rom\n" > "/dev/stderr"
+ printf "usage: uxnasm.awk [-v] input.tal output.rom\n" > "/dev/stderr"
exit 1
}
if (ARGC == 3 && substr(ARGV[1], 1, 2) == "-v") {
- printf "Uxnasm - Uxntal Assembler, 15 Jan 2025\n"
+ printf "Uxnasm.awk\n"
exit 0
}
if (ARGC != 3) {
- printf "usage: uxnasm [-v] input.tal output.rom\n" > "/dev/stderr"
+ printf "usage: uxnasm.awk [-v] input.tal output.rom\n" > "/dev/stderr"
exit 1
}
@@ -69,6 +73,28 @@ BEGIN {
}
# Utility functions
+function remove_comments(line, result, i, c, depth) {
+ # Remove comments from a line
+ # Comments are delimited by ( and ) and can be nested
+ result = ""
+ depth = 0
+
+ for (i = 1; i <= length(line); i++) {
+ c = substr(line, i, 1)
+ if (c == "(") {
+ depth++
+ } else if (c == ")") {
+ depth--
+ } else if (depth == 0) {
+ result = result c
+ }
+ }
+
+ # Trim whitespace
+ gsub(/^[ \t]+|[ \t]+$/, "", result)
+ return result
+}
+
function shex(s, d, n, c, i) {
n = 0
for (i = 1; i <= length(s); i++) {
@@ -197,7 +223,7 @@ function finddevicefield(device_name, field_name, i, j) {
return -1
}
-# Pass 1: Collect all symbols and labels
+# --- PASS 1: Symbol/Label Collection ---
function pass1_collect_symbols(filename) {
ptr = PAGE
data_length = PAGE
@@ -211,53 +237,86 @@ function pass1_collect_symbols(filename) {
}
function pass1_process_line(line_text, tokens, i, token, j) {
- # Simple tokenization - split on whitespace
- split(line_text, tokens)
- for (j = 1; j <= length(tokens); j++) {
- printf "DEBUG: token[%d] = '%s'\n", j, tokens[j] > "/dev/stderr"
- }
- for (i = 1; i <= length(tokens); i++) {
- if (tokens[i] != "" && tokens[i] != " ") {
- token = tokens[i]
- # Handle tokens that might be split by whitespace
- if (substr(token, 1, 1) == "<" && substr(token, length(token), 1) != ">") {
- # Look ahead for closing >
- if (i < length(tokens) && tokens[i + 1] == ">") {
- token = token ">"
- i++ # Skip the next token
- }
+ line_text = remove_comments(line_text)
+ if (line_text == "") return 1
+
+ # Custom tokenization to handle quoted strings properly
+ tokens_len = 0
+ i = 1
+ while (i <= length(line_text)) {
+ c = substr(line_text, i, 1)
+ if (c == " " || c == "\t") {
+ i++
+ continue
+ }
+
+ if (c == "\"") {
+ # Handle quoted string - capture everything until closing quote
+ token = "\""
+ i++
+ while (i <= length(line_text) && substr(line_text, i, 1) != "\"") {
+ token = token substr(line_text, i, 1)
+ i++
}
- # Handle tokens that start with - but are followed by a device field
- if (token == "-" && i < length(tokens) && index(tokens[i + 1], "/") > 0) {
- token = token tokens[i + 1]
- i++ # Skip the next token
+ if (i <= length(line_text)) {
+ token = token "\""
+ i++
}
- # Skip standalone > tokens
- if (token != ">") {
- pass1_parse_token(token)
+ tokens[++tokens_len] = token
+ } else {
+ # Regular token - capture until whitespace
+ token = ""
+ while (i <= length(line_text) && substr(line_text, i, 1) != " " && substr(line_text, i, 1) != "\t") {
+ token = token substr(line_text, i, 1)
+ i++
+ }
+ if (token != "") {
+ tokens[++tokens_len] = token
}
}
}
-}
-
-function pass1_parse_token(w) {
- # Debug output for label processing
- if (substr(w, 1, 1) == "@") {
- printf "DEBUG: Processing label token '%s' at ptr %d\n", w, ptr > "/dev/stderr"
+
+ # Combine - tokens with / (like -Screen/pixel)
+ for (i = 1; i < tokens_len; i++) {
+ if (tokens[i] == "-" && index(tokens[i+1], "/") > 0) {
+ tokens[i] = tokens[i] tokens[i+1]
+ for (j = i + 1; j < tokens_len; j++) {
+ tokens[j] = tokens[j+1]
+ }
+ tokens_len--
+ }
}
- # Skip comments
- if (substr(w, 1, 1) == "(") {
- return 1
+ for (i = 1; i <= tokens_len; i++) {
+ token = tokens[i]
+ printf "DEBUG: pass1 processing token: '%s'\n", token > "/dev/stderr"
+ if (!pass1_parse_token(token)) {
+ printf "ERROR: Failed to parse token '%s' at line %d\n", token, line_number > "/dev/stderr"
+ return 0
+ }
}
+ return 1
+}
+
+function pass1_parse_token(w) {
# Skip standalone tokens
if (w == ">" || w == "-") {
return 1
}
- # Handle device definitions
+ # Handle device definitions and labels
if (substr(w, 1, 1) == "@") {
+ printf "DEBUG: Processing @ token: %s\n", w > "/dev/stderr"
+ # Check if this is a macro definition (labels starting with @<)
+ printf "DEBUG: Checking macro condition: substr(w, 2, 1)='%s', substr(w, length(w), 1)='%s'\n", substr(w, 2, 1), substr(w, length(w), 1) > "/dev/stderr"
+ printf "DEBUG: Condition check: '%s' == '<' && '%s' == '>' = %s\n", substr(w, 2, 1), substr(w, length(w), 1), (substr(w, 2, 1) == "<" && substr(w, length(w), 1) == ">") > "/dev/stderr"
+ if (substr(w, 2, 1) == "<" && substr(w, length(w), 1) == ">") {
+ printf "DEBUG: Found macro definition: %s\n", w > "/dev/stderr"
+ makemacro(substr(w, 3, length(w) - 3))
+ return 1
+ }
+
# Check if this is a device definition (has base address)
if (last_padding != "" && current_device == "") {
makedevice(substr(w, 2), shex(last_padding))
@@ -284,6 +343,12 @@ function pass1_parse_token(w) {
return 1
}
+ # Handle lambda labels
+ if (w == "}") {
+ makelabel(makelambda(lambda_len++))
+ return 1
+ }
+
# Handle padding and size
if (substr(w, 1, 1) == "|") {
last_padding = substr(w, 2)
@@ -358,7 +423,7 @@ function pass1_parse_token(w) {
return 1
}
- # Handle hex literals
+ # Handle hex literals (with # prefix or raw hex values)
if (substr(w, 1, 1) == "#") {
if (length(substr(w, 2)) > 2) {
ptr += 3 # LIT2 + 2 bytes
@@ -368,6 +433,16 @@ function pass1_parse_token(w) {
return 1
}
+ # Handle raw hex values (like font data)
+ if (ishex(w)) {
+ if (length(w) > 2) {
+ ptr += 3 # LIT2 + 2 bytes
+ } else {
+ ptr += 2 # LIT + 1 byte
+ }
+ return 1
+ }
+
# Handle opcodes
if (isopc(w)) {
ptr++
@@ -380,13 +455,26 @@ function pass1_parse_token(w) {
return 1
}
+ # Handle macro definitions (labels starting with @<)
+ if (substr(w, 1, 1) == "@" && substr(w, 2, 1) == "<" && substr(w, length(w), 1) == ">") {
+ makemacro(substr(w, 3, length(w) - 3))
+ return 1
+ }
+
+ # Handle macro calls (tokens starting with <)
+ if (substr(w, 1, 1) == "<" && substr(w, length(w), 1) == ">") {
+ # Just advance pointer in pass1, will be expanded in pass2
+ ptr += 1 # Placeholder - actual size depends on macro content
+ return 1
+ }
+
# Handle unknown tokens as label references (fallback)
makeref(w, " ", ptr + 1)
ptr += 3 # LIT2 + 2 bytes
return 1
}
-# Pass 2: Generate code with resolved references
+# --- PASS 2: Code Generation ---
function pass2_generate_code(filename) {
ptr = PAGE
data_length = PAGE
@@ -399,41 +487,70 @@ function pass2_generate_code(filename) {
return 1
}
-function pass2_process_line(line_text, tokens, i, token) {
- # Simple tokenization - split on whitespace
- split(line_text, tokens)
- for (i = 1; i <= length(tokens); i++) {
- if (tokens[i] != "" && tokens[i] != " ") {
- token = tokens[i]
- # Handle tokens that might be split by whitespace
- if (substr(token, 1, 1) == "<" && substr(token, length(token), 1) != ">") {
- # Look ahead for closing >
- if (i < length(tokens) && tokens[i + 1] == ">") {
- token = token ">"
- i++ # Skip the next token
- }
+function pass2_process_line(line_text, tokens, i, token, j) {
+ line_text = remove_comments(line_text)
+ if (line_text == "") return 1
+
+ # Custom tokenization to handle quoted strings properly
+ tokens_len = 0
+ i = 1
+ while (i <= length(line_text)) {
+ c = substr(line_text, i, 1)
+ if (c == " " || c == "\t") {
+ i++
+ continue
+ }
+
+ if (c == "\"") {
+ # Handle quoted string - capture everything until closing quote
+ token = "\""
+ i++
+ while (i <= length(line_text) && substr(line_text, i, 1) != "\"") {
+ token = token substr(line_text, i, 1)
+ i++
}
- # Handle tokens that start with - but are followed by a device field
- if (token == "-" && i < length(tokens) && index(tokens[i + 1], "/") > 0) {
- token = token tokens[i + 1]
- i++ # Skip the next token
+ if (i <= length(line_text)) {
+ token = token "\""
+ i++
+ }
+ tokens[++tokens_len] = token
+ } else {
+ # Regular token - capture until whitespace
+ token = ""
+ while (i <= length(line_text) && substr(line_text, i, 1) != " " && substr(line_text, i, 1) != "\t") {
+ token = token substr(line_text, i, 1)
+ i++
}
- # Skip standalone > tokens
- if (token != ">") {
- pass2_parse_token(token)
+ if (token != "") {
+ tokens[++tokens_len] = token
}
}
}
+
+ # Combine - tokens with / (like -Screen/pixel)
+ for (i = 1; i < tokens_len; i++) {
+ if (tokens[i] == "-" && index(tokens[i+1], "/") > 0) {
+ tokens[i] = tokens[i] tokens[i+1]
+ for (j = i + 1; j < tokens_len; j++) {
+ tokens[j] = tokens[j+1]
+ }
+ tokens_len--
+ }
+ }
+
+ for (i = 1; i <= tokens_len; i++) {
+ token = tokens[i]
+ if (!pass2_parse_token(token)) {
+ printf "ERROR: Failed to parse token '%s' at line %d\n", token, line_number > "/dev/stderr"
+ return 0
+ }
+ }
+ return 1
}
function pass2_parse_token(w) {
printf "DEBUG: pass2_parse_token processing '%s'\n", w > "/dev/stderr"
- # Skip comments
- if (substr(w, 1, 1) == "(") {
- return 1
- }
-
# Skip standalone tokens (but not device field references)
if (w == ">") {
return 1
@@ -449,6 +566,11 @@ function pass2_parse_token(w) {
return 1
}
+ # Handle lambda labels (just skip, already collected in pass 1)
+ if (w == "}") {
+ return 1
+ }
+
# Handle padding
if (substr(w, 1, 1) == "|") {
# Set pointer based on padding value (no writing, just positioning)
@@ -515,21 +637,33 @@ function pass2_parse_token(w) {
return 1
}
- # Handle hex literals
+ # Handle hex literals (with # prefix or raw hex values)
if (substr(w, 1, 1) == "#") {
writehex(w)
return 1
}
+ # Handle raw hex values (like font data)
+ if (ishex(w)) {
+ writehex(w)
+ return 1
+ }
+
# Handle opcodes
if (isopc(w)) {
writebyte(findopcode(w))
return 1
}
- # Handle strings
+ # Handle string literals
if (substr(w, 1, 1) == "\"") {
- writestring(substr(w, 2))
+ writestring(substr(w, 2, length(w) - 2))
+ return 1
+ }
+
+ # Handle macro calls (tokens starting with <)
+ if (substr(w, 1, 1) == "<" && substr(w, length(w), 1) == ">") {
+ expandmacro(substr(w, 2, length(w) - 2))
return 1
}
@@ -632,6 +766,128 @@ function writehex(w) {
return 0
}
+# Macro functions
+function findmacro(name, i) {
+ for (i = 0; i < macros_len; i++) {
+ if (macro_names[i] == name) {
+ return i
+ }
+ }
+ return -1
+}
+
+function makemacro(name, i) {
+ printf "DEBUG: makemacro called with name: %s\n", name > "/dev/stderr"
+ if (macros_len >= 256) {
+ printf "Macros limit exceeded\n" > "/dev/stderr"
+ return 0
+ }
+ if (findmacro(name) >= 0) {
+ printf "Macro duplicate: %s\n", name > "/dev/stderr"
+ return 0
+ }
+ if (findlabel(name) >= 0) {
+ printf "Label duplicate: %s\n", name > "/dev/stderr"
+ return 0
+ }
+
+ # Store macro name and initialize empty body
+ macro_names[macros_len] = name
+ macro_data[macros_len] = ""
+ macros_len++
+
+ # Note: We'll capture the macro body in pass2 when we process the file again
+ return 1
+}
+
+function capture_macro_body(name, filename, line, in_macro, macro_body, depth) {
+ # Reset file to beginning
+ close(filename)
+ in_macro = 0
+ macro_body = ""
+ depth = 0
+
+ while ((getline line < filename) > 0) {
+ if (in_macro) {
+ # Check if we've reached the end of the macro (next label or closing brace)
+ if (substr(line, 1, 1) == "@" && substr(line, 2, 1) != "|") {
+ # Found next label, end of macro
+ break
+ }
+
+ # Count braces for nested macro handling
+ for (i = 1; i <= length(line); i++) {
+ c = substr(line, i, 1)
+ if (c == "{") depth++
+ else if (c == "}") {
+ depth--
+ if (depth < 0) break # End of macro
+ }
+ }
+
+ if (depth < 0) break # End of macro
+
+ # Add line to macro body
+ macro_body = macro_body line "\n"
+ } else if (substr(line, 1, 1) == "@" && substr(line, 2, 1) == "<") {
+ # Check if this is our macro
+ macro_name = substr(line, 3)
+ if (substr(macro_name, length(macro_name), 1) == ">") {
+ macro_name = substr(macro_name, 1, length(macro_name) - 1)
+ if (macro_name == name) {
+ in_macro = 1
+ # Start capturing from next line
+ }
+ }
+ }
+ }
+
+ close(filename)
+
+ # Store the macro body
+ for (i = 0; i < macros_len; i++) {
+ if (macro_names[i] == name) {
+ macro_data[i] = macro_body
+ return 1
+ }
+ }
+ return 0
+}
+
+function expandmacro(name, macro_idx, macro_text, tokens, i) {
+ macro_idx = findmacro(name)
+ if (macro_idx < 0) {
+ printf "Macro not found: %s\n", name > "/dev/stderr"
+ return 0
+ }
+
+ # If macro body is empty, try to capture it
+ if (macro_data[macro_idx] == "") {
+ capture_macro_body(name, ARGV[1])
+ }
+
+ macro_text = macro_data[macro_idx]
+ if (macro_text == "") {
+ printf "Macro body empty: %s\n", name > "/dev/stderr"
+ return 0
+ }
+
+ # Process macro body line by line
+ split(macro_text, lines, "\n")
+ for (i = 1; i <= length(lines); i++) {
+ if (lines[i] != "") {
+ pass2_process_line(lines[i])
+ }
+ }
+ return 1
+}
+
+# Lambda functions
+function makelambda(id) {
+ # Create a unique lambda name like "λb" where suffix is hex digit
+ return sprintf("λ%c", substr(hexad, int(id / 16) + 1, 1) substr(hexad, (id % 16) + 1, 1))
+}
+
function ord(c) {
return index(" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", c) + 31
}