aboutsummaryrefslogtreecommitdiffstats
path: root/awk/scheme
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-07-20 21:55:12 -0400
committerelioat <elioat@tilde.institute>2025-07-20 21:55:12 -0400
commit8a403316fab3027f20988a146774586d40229a42 (patch)
tree31983773a65d1ecd2f4459de81a3b09959a5cd6c /awk/scheme
parent2a5b31c067d6aa2c790f6d70564f1ad015cf5aa3 (diff)
downloadtour-8a403316fab3027f20988a146774586d40229a42.tar.gz
*
Diffstat (limited to 'awk/scheme')
-rwxr-xr-xawk/scheme/scheme/bin/compiler.awk32
-rwxr-xr-xawk/scheme/scheme/bin/vm.awk122
-rwxr-xr-xawk/scheme/scheme/test/run_tests.sh7
3 files changed, 149 insertions, 12 deletions
diff --git a/awk/scheme/scheme/bin/compiler.awk b/awk/scheme/scheme/bin/compiler.awk
index 2b5094e..4b1735b 100755
--- a/awk/scheme/scheme/bin/compiler.awk
+++ b/awk/scheme/scheme/bin/compiler.awk
@@ -73,18 +73,20 @@ function split_expressions(prog, current, paren_count, i, c, expr, cleaned) {
paren_count = 0
# Clean up the input:
- # 1. Remove comments (text between ; and next opening paren)
- # 2. Normalize whitespace
- # 3. Trim leading/trailing whitespace
+ # 1. Remove comments (lines starting with ;; or ;)
+ # 2. Remove comments within lines (text between ; and end of line)
+ # 3. Normalize whitespace
+ # 4. Trim leading/trailing whitespace
cleaned = prog
# AWK FEATURE: gsub() is a built-in function for global substitution (like replaceAll in JS)
# gsub(pattern, replacement, target) - modifies target in place and returns count
- gsub(/;[^(]*\(/, "(", cleaned) # Remove comments before expressions
- gsub(/\)[^)]*;/, ")", cleaned) # Remove comments after expressions
- gsub(/[ \t\n]+/, " ", cleaned) # Normalize whitespace to single spaces
+ gsub(/^[ \t]*;;.*$/, "", cleaned) # Remove lines starting with ;;
+ gsub(/^[ \t]*;.*$/, "", cleaned) # Remove lines starting with ;
+ gsub(/;[^"]*$/, "", cleaned) # Remove comments at end of lines (but not in strings)
+ gsub(/[ \t\n]+/, " ", cleaned) # Normalize whitespace to single spaces
# AWK FEATURE: sub() is like gsub() but only replaces the first occurrence
- sub(/^[ \t\n]+/, "", cleaned) # Trim leading whitespace
- sub(/[ \t\n]+$/, "", cleaned) # Trim trailing whitespace
+ sub(/^[ \t\n]+/, "", cleaned) # Trim leading whitespace
+ sub(/[ \t\n]+$/, "", cleaned) # Trim trailing whitespace
debug("Cleaned program: [" cleaned "]")
@@ -560,7 +562,7 @@ function compile_primitive_call(op, args, arg_array, nargs, i) {
}
print "LOOKUP list"
print "GET_VALUE"
- print "CALL"
+ print "CALL_WITH_ARGS " nargs
}
else if (op == "reverse") {
if (nargs != 1) error("reverse requires 1 argument")
@@ -595,7 +597,7 @@ function compile_primitive_call(op, args, arg_array, nargs, i) {
}
print "LOOKUP string-append"
print "GET_VALUE"
- print "CALL"
+ print "CALL_WITH_ARGS " nargs
}
else if (op == "string-ref") {
if (nargs != 2) error("string-ref requires 2 arguments")
@@ -804,7 +806,7 @@ function compile_define(args, name, params, body, param_array, nparams, i, paren
first_word == "reverse" || first_word == "member" || first_word == "length" ||
first_word == "car" || first_word == "cdr" || first_word == "cadr" ||
first_word == "caddr" || first_word == "list-ref" || first_word == "list-tail" ||
- first_word == "null?" || first_word == "pair?") {
+ first_word == "null?" || first_word == "pair?" || first_word == "lambda") {
# This is a variable definition with function call value
debug("Defining variable with function call value: " name " with value: " args)
compile_expr(args) # Compile the value
@@ -1158,6 +1160,12 @@ function compile_expr(expr, split_result, op, args) {
return
}
+ # Handle comment lines
+ if (expr ~ /^[ \t]*;;/ || expr ~ /^[ \t]*;/) {
+ debug("Skipping comment line: [" expr "]")
+ return
+ }
+
# Handle string literals
if (substr(expr, 1, 1) == "\"") {
compile_string(expr)
@@ -1187,7 +1195,7 @@ function compile_expr(expr, split_result, op, args) {
}
# Handle variable lookup
- if (expr ~ /^[a-zA-Z_][a-zA-Z0-9_]*$/) {
+ if (expr ~ /^[a-zA-Z_][a-zA-Z0-9_-]*$/) {
print "LOOKUP " expr
return
}
diff --git a/awk/scheme/scheme/bin/vm.awk b/awk/scheme/scheme/bin/vm.awk
index b820ca9..00b0f24 100755
--- a/awk/scheme/scheme/bin/vm.awk
+++ b/awk/scheme/scheme/bin/vm.awk
@@ -174,6 +174,8 @@ BEGIN {
FUNCTIONS["caddr"] = "stdlib_caddr"
FUNCTIONS["list-ref"] = "stdlib_list_ref"
FUNCTIONS["list-tail"] = "stdlib_list_tail"
+ FUNCTIONS["assert"] = "assert"
+ FUNCTIONS["error"] = "user_error"
debug("REGISTERING_BUILTINS: " length(FUNCTIONS) " functions after")
@@ -562,6 +564,9 @@ function execute(instr) {
else if (op == "CALL") {
vm_call_function()
}
+ else if (op == "CALL_WITH_ARGS") {
+ vm_call_function_with_args(parts[2])
+ }
else if (op == "RETURN") {
debug("EXECUTING_RETURN")
# The call_stack_ptr is no longer used for return, so this instruction is effectively removed.
@@ -973,6 +978,14 @@ function vm_call_function(code_lines, j, saved_pc, saved_env_size, arg, param_na
debug("Calling standard library function: stdlib_list_tail")
stdlib_list_tail()
return
+ } else if (built_in_name == "assert") {
+ debug("Calling built-in function: assert")
+ assert()
+ return
+ } else if (built_in_name == "user_error") {
+ debug("Calling built-in function: user_error")
+ user_error()
+ return
}
}
@@ -1061,6 +1074,38 @@ function vm_call_function(code_lines, j, saved_pc, saved_env_size, arg, param_na
}
}
+# Function call with argument count implementation
+function vm_call_function_with_args(arg_count, code_lines, j, saved_pc, saved_env_size, arg, param_name) {
+ # Get function name from stack
+ func_name = pop()
+ debug("Calling function with args: " func_name " (arg_count: " arg_count ")")
+ debug("CALLING_FUNCTION_WITH_ARGS: " func_name " (arg_count: " arg_count ")")
+ debug("Stack pointer before function call: " stack_ptr)
+
+ # If name is a symbol, get its value
+ if (isSymbol(func_name)) {
+ func_name = getValue(func_name)
+ }
+
+ # Check if it's a built-in function first
+ if (func_name in FUNCTIONS) {
+ built_in_name = FUNCTIONS[func_name]
+ if (built_in_name == "stdlib_list") {
+ debug("Calling standard library function with args: stdlib_list (arg_count: " arg_count ")")
+ stdlib_list_with_args(arg_count)
+ return
+ } else if (built_in_name == "string_append") {
+ debug("Calling built-in function with args: string_append (arg_count: " arg_count ")")
+ string_append_with_args(arg_count)
+ return
+ }
+ }
+
+ # For other functions, fall back to regular call
+ debug("Falling back to regular function call for: " func_name)
+ vm_call_function()
+}
+
# Function return implementation - no longer needed with direct execution
# function vm_return() {
# debug("VM_RETURN: call_stack_ptr = " call_stack_ptr)
@@ -1238,6 +1283,20 @@ function string_append() {
push(makeString(result))
}
+function string_append_with_args(arg_count) {
+ if (arg_count < 2) error("string-append requires at least two operands")
+ if (stack_ptr < arg_count) error("string-append requires " arg_count " arguments, but only " stack_ptr " available")
+
+ result = ""
+ # Pop specified number of arguments and concatenate (in reverse order)
+ for (i = arg_count; i >= 1; i--) {
+ val = pop()
+ if (!isString(val)) error("string-append requires string operands")
+ result = getValue(val) result # Prepend to maintain order
+ }
+ push(makeString(result))
+}
+
function string_ref() {
if (stack_ptr < 2) error("string-ref requires two operands")
index_val = pop()
@@ -1328,6 +1387,28 @@ function display() {
print display_value(val)
}
+# Assert function for testing - checks if condition is true
+function assert() {
+ if (stack_ptr < 1) error("assert requires one argument")
+ val = pop()
+ if (!isBoolean(val) && !isNumber(val)) {
+ error("assert requires boolean or number argument")
+ }
+ if (getValue(val) == "0" || getValue(val) == "B:0") {
+ error("Assertion failed")
+ }
+}
+
+# User-callable error function
+function user_error() {
+ if (stack_ptr < 1) error("error requires one argument")
+ val = pop()
+ if (!isString(val)) {
+ error("error requires string argument")
+ }
+ error(getValue(val))
+}
+
# Recursively convert a value to a string for display
function display_value(val, t, idx, pair, car_val, cdr_val, result) {
t = getType(val)
@@ -1402,6 +1483,47 @@ function stdlib_list() {
push(result)
}
+# Create a list from elements with explicit argument count
+# This function only consumes the specified number of arguments
+function stdlib_list_with_args(arg_count) {
+ debug("stdlib_list_with_args called with arg_count: " arg_count)
+ debug("Stack contents before list: " stack_ptr " items")
+
+ if (arg_count < 0) {
+ error("Invalid argument count for list: " arg_count)
+ }
+
+ if (arg_count == 0) {
+ # No arguments, return empty list
+ debug("No arguments, returning NIL:")
+ push("NIL:")
+ return
+ }
+
+ if (stack_ptr < arg_count) {
+ error("list requires " arg_count " arguments, but only " stack_ptr " available")
+ }
+
+ # Build list from stack elements (arguments are in reverse order on stack)
+ result = "NIL:"
+ debug("Building list with " arg_count " arguments")
+
+ # Pop arguments and build list in correct order
+ for (i = arg_count; i >= 1; i--) {
+ val = pop()
+ debug("Popped argument " i ": " val)
+ # Create cons cell with current value and result
+ pair_val = val "," result
+ pair_idx = allocate(pair_val)
+ result = makeValue(T_PAIR, pair_idx)
+ debug("Created pair: " result)
+ }
+
+ debug("Final result: " result)
+ debug("Stack contents after list: " stack_ptr " items")
+ push(result)
+}
+
# Check if value is null (empty list)
# This predicate is essential for list processing and control flow
function stdlib_null_p() {
diff --git a/awk/scheme/scheme/test/run_tests.sh b/awk/scheme/scheme/test/run_tests.sh
index d87ef52..aac834d 100755
--- a/awk/scheme/scheme/test/run_tests.sh
+++ b/awk/scheme/scheme/test/run_tests.sh
@@ -16,6 +16,7 @@ NC='\033[0m' # No Color
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
+FAILED_TEST_NAMES=()
# Get the project root directory
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
@@ -87,6 +88,7 @@ run_test() {
else
print_status "FAIL" "$test_name (exit code: $exit_code)"
FAILED_TESTS=$((FAILED_TESTS + 1))
+ FAILED_TEST_NAMES+=("$test_name")
echo "Error output:"
echo "$output" | sed 's/^/ /'
@@ -136,6 +138,11 @@ print_summary() {
exit 0
else
echo -e "${RED}Some tests failed!${NC}"
+ echo
+ echo "Failed tests:"
+ for test_name in "${FAILED_TEST_NAMES[@]}"; do
+ echo -e " ${RED}✗${NC} $test_name"
+ done
exit 1
fi
}