aboutsummaryrefslogtreecommitdiffstats
path: root/awk
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-07-20 21:26:28 -0400
committerelioat <elioat@tilde.institute>2025-07-20 21:26:28 -0400
commit2a5b31c067d6aa2c790f6d70564f1ad015cf5aa3 (patch)
tree63f6bc6b064479af5c973a58d5154515a8a665cc /awk
parentb7ad39a6e2d95970ba06c602a9a37ce57180d1c0 (diff)
downloadtour-2a5b31c067d6aa2c790f6d70564f1ad015cf5aa3.tar.gz
*
Diffstat (limited to 'awk')
-rwxr-xr-xawk/scheme/scheme/bin/compiler.awk11
-rwxr-xr-xawk/scheme/scheme/bin/repl8
-rwxr-xr-xawk/scheme/scheme/bin/vm.awk34
-rwxr-xr-xawk/scheme/scheme/test/run_tests.sh2
4 files changed, 42 insertions, 13 deletions
diff --git a/awk/scheme/scheme/bin/compiler.awk b/awk/scheme/scheme/bin/compiler.awk
index bf84e37..2b5094e 100755
--- a/awk/scheme/scheme/bin/compiler.awk
+++ b/awk/scheme/scheme/bin/compiler.awk
@@ -127,7 +127,7 @@ function split_expressions(prog, current, paren_count, i, c, expr, cleaned) {
program = expr # Set for parser
expr = parse_expr()
compile_expr(expr)
- # Add CLEAR_STACK to clear stack between expressions
+ # Clear stack between expressions to prevent pollution
print "CLEAR_STACK" # Clear stack between expressions
current = ""
}
@@ -145,7 +145,7 @@ function split_expressions(prog, current, paren_count, i, c, expr, cleaned) {
program = expr # Set for parser
expr = parse_expr()
compile_expr(expr)
- # Add CLEAR_STACK to clear stack between expressions
+ # Clear stack between expressions to prevent pollution
print "CLEAR_STACK" # Clear stack between expressions
}
current = ""
@@ -163,7 +163,8 @@ function split_expressions(prog, current, paren_count, i, c, expr, cleaned) {
program = expr # Set for parser
expr = parse_expr()
compile_expr(expr)
- # Don't POP the final expression - keep its result
+ # Clear stack after the final expression
+ print "CLEAR_STACK"
}
}
@@ -395,6 +396,10 @@ function compile_string(str) {
debug("Compiling string: " str)
# Remove outer quotes and emit string constant
content = substr(str, 2, length(str) - 2)
+ # Escape newlines and other special characters for output
+ gsub(/\n/, "\\n", content)
+ gsub(/\t/, "\\t", content)
+ gsub(/\\/, "\\\\", content)
print "PUSH_CONST STR:" content
}
diff --git a/awk/scheme/scheme/bin/repl b/awk/scheme/scheme/bin/repl
index 8663210..1c290d1 100755
--- a/awk/scheme/scheme/bin/repl
+++ b/awk/scheme/scheme/bin/repl
@@ -89,10 +89,15 @@ evaluate_expression() {
# BASH FEATURE: -v var=value passes variables to awk
# Unlike JS where you'd use process.env, this sets awk variables directly
result=$(awk -v PERSIST=1 -v DEBUG="$DEBUG" -f "$VM" "$ASM_FILE" 2>&1)
+ vm_exit_code=$?
debug "VM output: $result"
+ debug "VM exit code: $vm_exit_code"
if [ -n "$result" ]; then
echo "$result"
fi
+ if [ $vm_exit_code -ne 0 ]; then
+ return $vm_exit_code
+ fi
return 0
else
echo "Compilation error" >&2
@@ -112,8 +117,9 @@ if [ "$#" -gt 0 ]; then
file_content=$(cat "$1")
debug "File content: $file_content"
evaluate_expression "$file_content"
+ exit_code=$?
cleanup "keep_state" # Keep state after file execution
- exit 0
+ exit $exit_code
fi
# REPL state
diff --git a/awk/scheme/scheme/bin/vm.awk b/awk/scheme/scheme/bin/vm.awk
index 550a0d2..b820ca9 100755
--- a/awk/scheme/scheme/bin/vm.awk
+++ b/awk/scheme/scheme/bin/vm.awk
@@ -470,6 +470,17 @@ function execute(instr) {
for (i = 3; i <= length(parts); i++) {
value = value " " parts[i]
}
+
+ # Handle escape sequences in string constants
+ if (value ~ /^STR:/) {
+ str_content = substr(value, 5) # Remove "STR:" prefix
+ # Convert escape sequences back to actual characters
+ gsub(/\\n/, "\n", str_content)
+ gsub(/\\t/, "\t", str_content)
+ gsub(/\\\\/, "\\", str_content)
+ value = "STR:" str_content
+ }
+
push(value)
}
else if (op == "POP") {
@@ -590,8 +601,11 @@ function execute(instr) {
# NR is a built-in variable that contains the current record (line) number
# Unlike JS where you'd need to track line numbers manually
{
- program[NR-1] = $0 # $0 is the current line being processed
- original_program[NR-1] = $0 # Keep a copy of the original program
+ # Skip empty lines
+ if (length($0) > 0) {
+ program[NR-1] = $0 # $0 is the current line being processed
+ original_program[NR-1] = $0 # Keep a copy of the original program
+ }
}
# AWK FEATURE: END block runs after all input has been processed
@@ -837,6 +851,7 @@ function vm_call_function(code_lines, j, saved_pc, saved_env_size, arg, param_na
func_name = pop()
debug("Calling function: " func_name)
debug("CALLING_FUNCTION: " func_name)
+ debug("Stack pointer before function call: " stack_ptr)
# If name is a symbol, get its value
if (isSymbol(func_name)) {
@@ -922,10 +937,10 @@ function vm_call_function(code_lines, j, saved_pc, saved_env_size, arg, param_na
debug("Calling standard library function: stdlib_length")
stdlib_length()
return
- } else if (built_in_name == "stdlib_append") {
- debug("Calling standard library function: stdlib_append")
- stdlib_append()
- return
+ } else if (built_in_name == "stdlib_append") {
+ debug("Calling standard library function: stdlib_append")
+ stdlib_append()
+ return
} else if (built_in_name == "display") {
debug("Calling built-in function: display")
display()
@@ -942,8 +957,7 @@ function vm_call_function(code_lines, j, saved_pc, saved_env_size, arg, param_na
debug("Calling standard library function: stdlib_member")
stdlib_member()
return
- return
- } else if (built_in_name == "stdlib_cadr") {
+ } else if (built_in_name == "stdlib_cadr") {
debug("Calling standard library function: stdlib_cadr")
stdlib_cadr()
return
@@ -1357,6 +1371,7 @@ function display_value(val, t, idx, pair, car_val, cdr_val, result) {
# The implementation reverses the stack order to maintain proper list construction
function stdlib_list() {
debug("stdlib_list called with stack_ptr: " stack_ptr)
+ debug("Stack contents before list: " stack_ptr " items")
# This will be a special form handled by the compiler
# For now, we'll implement it as a function that takes arguments from stack
if (stack_ptr < 1) {
@@ -1383,6 +1398,7 @@ function stdlib_list() {
}
debug("Final result: " result)
+ debug("Stack contents after list: " stack_ptr " items")
push(result)
}
@@ -1580,9 +1596,11 @@ function stdlib_reverse() {
# This function returns the sublist starting from the matching element
# or NIL: if the element is not found
function stdlib_member() {
+ debug("stdlib_member called with stack_ptr: " stack_ptr)
if (stack_ptr < 2) error("member requires two arguments")
list_val = pop()
elem_val = pop()
+ debug("stdlib_member: elem_val=" elem_val " list_val=" list_val)
if (list_val == "NIL:") {
push("NIL:")
diff --git a/awk/scheme/scheme/test/run_tests.sh b/awk/scheme/scheme/test/run_tests.sh
index cac23f5..d87ef52 100755
--- a/awk/scheme/scheme/test/run_tests.sh
+++ b/awk/scheme/scheme/test/run_tests.sh
@@ -3,7 +3,7 @@
# Awk-Scheme Test Runner
# Runs all tests and reports results
-set -e # Exit on any error
+# set -e # Exit on any error - commented out to allow all tests to run
# Colors for output
RED='\033[0;31m'