diff options
| author | elioat <elioat@tilde.institute> | 2025-07-17 20:21:23 -0400 |
|---|---|---|
| committer | elioat <elioat@tilde.institute> | 2025-07-17 20:21:23 -0400 |
| commit | 82c679ae376dc0adad9b8bbb8fbf0509dd75233d (patch) | |
| tree | 9714b7ab91437cba94490f9b52529e4bb6ac0fea /awk | |
| parent | 80471faf3fce99334dc3bd7ac7d2afaeb232049c (diff) | |
| download | tour-82c679ae376dc0adad9b8bbb8fbf0509dd75233d.tar.gz | |
*
Diffstat (limited to 'awk')
| -rwxr-xr-x | awk/scheme/scheme/bin/compiler.awk | 10 | ||||
| -rwxr-xr-x | awk/scheme/scheme/bin/repl | 24 | ||||
| -rwxr-xr-x | awk/scheme/scheme/bin/vm.awk | 126 |
3 files changed, 122 insertions, 38 deletions
diff --git a/awk/scheme/scheme/bin/compiler.awk b/awk/scheme/scheme/bin/compiler.awk index debaa2c..6cf403a 100755 --- a/awk/scheme/scheme/bin/compiler.awk +++ b/awk/scheme/scheme/bin/compiler.awk @@ -316,14 +316,14 @@ function compile_primitive_call(op, args, arg_array, nargs, i) { else { # Function call for user-defined functions debug("Function call: " op) - # Look up the function name - print "LOOKUP " op - # Get the actual function name - print "GET_VALUE" - # Then compile arguments + # First compile arguments for (i = 1; i <= nargs; i++) { compile_expr(arg_array[i]) } + # Then look up the function name + print "LOOKUP " op + # Get the actual function name + print "GET_VALUE" # Call the function print "CALL" } diff --git a/awk/scheme/scheme/bin/repl b/awk/scheme/scheme/bin/repl index 14a10cf..8eecf2b 100755 --- a/awk/scheme/scheme/bin/repl +++ b/awk/scheme/scheme/bin/repl @@ -1,7 +1,7 @@ #!/bin/bash # Enable debug tracing -DEBUG=0 +DEBUG=${DEBUG:-0} debug() { if [ "$DEBUG" = "1" ]; then @@ -35,10 +35,11 @@ STATE_FILE="/tmp/scheme_vm.state" cleanup() { debug "Cleaning up temp dir: $TMPDIR" rm -rf "$TMPDIR" - if [ "$1" != "keep_state" ]; then - rm -f "$STATE_FILE" - rm -f "/tmp/scheme_vm.env" - fi + # Temporarily disable state file deletion for debugging + # if [ "$1" != "keep_state" ]; then + # rm -f "$STATE_FILE" + # rm -f "/tmp/scheme_vm.env" + # fi } trap "cleanup" EXIT @@ -48,10 +49,11 @@ ASM_FILE="$TMPDIR/output.asm" DEBUG_FILE="$TMPDIR/debug.out" # Initialize/clear state files at REPL start -if [ "$#" -eq 0 ]; then # Only for interactive mode - : > "/tmp/scheme_vm.state" - : > "/tmp/scheme_vm.env" -fi +# Temporarily disable state file clearing for debugging +# if [ "$#" -eq 0 ]; then # Only for interactive mode +# : > "/tmp/scheme_vm.state" +# : > "/tmp/scheme_vm.env" +# fi # Function to handle evaluation evaluate_expression() { @@ -77,8 +79,8 @@ evaluate_expression() { cat "$ASM_FILE" >&2 debug "Running VM..." - # Use persistent VM state - result=$(awk -v PERSIST=1 -f "$VM" "$ASM_FILE" 2>&1) + # Use persistent VM state and pass debug flag + result=$(awk -v PERSIST=1 -v DEBUG="$DEBUG" -f "$VM" "$ASM_FILE" 2>&1) debug "VM output: $result" if [ -n "$result" ]; then echo "$result" diff --git a/awk/scheme/scheme/bin/vm.awk b/awk/scheme/scheme/bin/vm.awk index 4e7d2c7..d2158cb 100755 --- a/awk/scheme/scheme/bin/vm.awk +++ b/awk/scheme/scheme/bin/vm.awk @@ -36,12 +36,23 @@ BEGIN { # Call stack for function returns call_stack_ptr = 0 + # Global function registry - clear it first + delete FUNCTIONS # Maps function names to implementations + # State persistence configuration STATE_FILE = "/tmp/scheme_vm.state" + print "STATE_FILE_PATH: " STATE_FILE > "/dev/stderr" + # Test file creation + print "TEST" > "/tmp/test_file" + print "PERSIST_FLAG: " PERSIST > "/dev/stderr" if (PERSIST) { debug("Loading state from: " STATE_FILE) + print "LOADING_STATE: Attempting to read " STATE_FILE > "/dev/stderr" + print "LOADING_STATE: FUNCTIONS table size before loading: " length(FUNCTIONS) > "/dev/stderr" if ((getline line < STATE_FILE) >= 0) { # Check if file exists and is readable + print "LOADING_STATE: File opened successfully, first line: " line > "/dev/stderr" do { + print "LOADING_STATE: Processing line: " line > "/dev/stderr" if (line ~ /^FUNC /) { # Parse and load function definition sub(/^FUNC /, "", line) @@ -50,13 +61,26 @@ BEGIN { code = line sub(/^[^ ]+ /, "", code) + # Read the rest of the function code (until next FUNC or end of file) + print "LOADING_STATE: Reading function code for " name > "/dev/stderr" + while ((getline line < STATE_FILE) > 0) { + print "LOADING_STATE: Reading line: " line > "/dev/stderr" + if (line ~ /^FUNC /) { + # Found next function, put the line back + print "LOADING_STATE: Found next function, stopping" > "/dev/stderr" + break + } + code = code "\n" line + } + debug("Loaded function: " name) debug("Code: " code) - # Store function in function table - func_def_names[func_def_size] = name - func_def_code[func_def_size] = code - func_def_size++ + # Store function in FUNCTIONS table + FUNCTIONS[name] = code + print "LOADED_FUNCTION: " name " with code length " length(code) > "/dev/stderr" + print "LOADED_FUNCTION: FUNCTIONS table size after loading " name ": " length(FUNCTIONS) > "/dev/stderr" + print "LOADED_FUNCTION: Checking if " name " is in table: " (name in FUNCTIONS) > "/dev/stderr" } } while ((getline line < STATE_FILE) > 0) close(STATE_FILE) @@ -68,8 +92,17 @@ BEGIN { delete func_env_vals # Variable values in function scope delete func_env_sizes # Size of each function's environment - # Global function registry - delete FUNCTIONS # Maps function names to implementations + # Register built-in functions first + print "REGISTERING_BUILTINS: " length(FUNCTIONS) " functions before" > "/dev/stderr" + FUNCTIONS["+"] = "add" + FUNCTIONS["-"] = "subtract" + FUNCTIONS["*"] = "multiply" + FUNCTIONS["/"] = "divide" + FUNCTIONS["="] = "equals" + FUNCTIONS["<"] = "less_than" + FUNCTIONS[">"] = "greater_than" + FUNCTIONS["add1"] = "add_one" + print "REGISTERING_BUILTINS: " length(FUNCTIONS) " functions after" > "/dev/stderr" # Environment persistence configuration ENV_STATE_FILE = "/tmp/scheme_vm.env" @@ -97,16 +130,6 @@ BEGIN { } } - # Register built-in functions - FUNCTIONS["+"] = "add" - FUNCTIONS["-"] = "subtract" - FUNCTIONS["*"] = "multiply" - FUNCTIONS["/"] = "divide" - FUNCTIONS["="] = "equals" - FUNCTIONS["<"] = "less_than" - FUNCTIONS[">"] = "greater_than" - FUNCTIONS["add1"] = "add_one" - # Track if VM halted normally (vs error) normal_exit = 0 } @@ -298,6 +321,7 @@ function execute(instr) { if (stack_ptr < 2) error("SWAP requires two operands") val2 = pop() val1 = pop() + val1 = pop() push(val2) push(val1) } @@ -333,14 +357,20 @@ function execute(instr) { print peek() } else if (op == "HALT") { + print "EXECUTING_HALT" > "/dev/stderr" normal_exit = 1 + print "HALT: Stack pointer = " stack_ptr > "/dev/stderr" if (stack_ptr > 0) { result = peek() + print "HALT: Result = " result > "/dev/stderr" + } else { + print "HALT: Stack is empty" > "/dev/stderr" } if (PERSIST) { save_state() } if (result) { + print "PRINTING_RESULT: " result > "/dev/stderr" print result } exit(0) @@ -358,9 +388,10 @@ function execute(instr) { vm_define_function(parts[2], pc) } else if (op == "CALL") { - vm_call_function(parts[2]) + vm_call_function() } else if (op == "RETURN") { + print "EXECUTING_RETURN" > "/dev/stderr" vm_return() } else if (op == "GET_VALUE") { @@ -379,6 +410,7 @@ function execute(instr) { # Main execution loop END { while (pc < length(program)) { + # print "EXECUTING_PC_" pc ": " program[pc] > "/dev/stderr" execute(program[pc++]) } @@ -461,16 +493,27 @@ function vm_lookup(name, i, global_name, val) { debug("Looking up " name " in environment of size: " env_size) dump_env() + + # Check if it's a function (built-in or user-defined) + print "LOOKUP_CHECKING: " name " in FUNCTIONS table" > "/dev/stderr" + print "FUNCTIONS_TABLE_SIZE: " length(FUNCTIONS) > "/dev/stderr" + print "FUNCTIONS_IN_TABLE:" > "/dev/stderr" + for (f in FUNCTIONS) { + print " " f > "/dev/stderr" + } if (name in FUNCTIONS) { debug("Found function: " name) push(makeValue(T_SYMBOL, name)) return } + print "FUNCTION_LOOKUP_FAILED: " name > "/dev/stderr" # Try global name first, then local global_name = "__global_" name + print "LOOKUP_ENV: Looking for " name " in environment of size " env_size > "/dev/stderr" for (i = env_size - 1; i >= 0; i--) { + print "LOOKUP_ENV: Checking index " i ": " env_name[i] " = " env_val[i] > "/dev/stderr" if (env_name[i] == global_name || env_name[i] == name) { debug("Found " name " = " env_val[i] " at position " i) push(env_val[i]) @@ -484,6 +527,15 @@ function vm_lookup(name, i, global_name, val) { function vm_define_function(name, start_pc) { debug("Defining function: " name " at " start_pc) + # Clear the from_define flag + for (i = env_size - 1; i >= 0; i--) { + if (env_name[i] == "from_define") { + debug("Clearing from_define flag") + env_size-- + break + } + } + # Build function code code = "" i = start_pc @@ -497,13 +549,18 @@ function vm_define_function(name, start_pc) { # Store function debug("Storing function: " name " = " code) FUNCTIONS[name] = code + debug("Function stored. Total functions: " length(FUNCTIONS)) + print "FUNCTION_STORED: " name > "/dev/stderr" pc = i + 1 } # Function call implementation -function vm_call_function(func_name, code_lines, j, saved_pc, saved_env_size, arg, param_name) { +function vm_call_function(code_lines, j, saved_pc, saved_env_size, arg, param_name) { + # Get function name from stack + func_name = pop() debug("Calling function: " func_name) + print "CALLING_FUNCTION: " func_name > "/dev/stderr" # If name is a symbol, get its value if (isSymbol(func_name)) { @@ -522,6 +579,9 @@ function vm_call_function(func_name, code_lines, j, saved_pc, saved_env_size, ar saved_pc = pc saved_env_size = env_size + # Save the original instruction at the call position + original_instruction = program[pc] + # Split function code into lines split(FUNCTIONS[func_name], code_lines, "\n") @@ -536,16 +596,22 @@ function vm_call_function(func_name, code_lines, j, saved_pc, saved_env_size, ar # Get parameter name from STORE instruction param_name = substr(code_lines[1], 7) debug("Found parameter name: " param_name) + print "FUNCTION_PARAM: " param_name > "/dev/stderr" # Get argument from stack arg = pop() debug("Function argument: " arg) + print "FUNCTION_ARG: " arg > "/dev/stderr" # Create new environment frame debug("Creating new environment frame at size: " env_size) env_name[env_size] = param_name env_val[env_size] = arg env_size++ + print "FUNCTION_ENV_STORE: " param_name " = " arg " at index " (env_size-1) > "/dev/stderr" + + # Skip the first STORE instruction since we already handled parameter storage + pc++ } else { # This is a built-in function or non-parameterized function debug("Calling non-parameterized function: " func_name) @@ -554,6 +620,7 @@ function vm_call_function(func_name, code_lines, j, saved_pc, saved_env_size, ar # Save return info and jump to function call_stack[++call_stack_ptr] = saved_pc env_stack[call_stack_ptr] = saved_env_size + original_instructions[call_stack_ptr] = original_instruction debug("Function found, jumping to PC: " pc " with env_size: " saved_env_size) dump_env() @@ -561,9 +628,11 @@ function vm_call_function(func_name, code_lines, j, saved_pc, saved_env_size, ar # Function return implementation function vm_return() { + print "VM_RETURN: call_stack_ptr = " call_stack_ptr > "/dev/stderr" if (call_stack_ptr > 0) { # Save return value ret_val = pop() + print "VM_RETURN: return value = " ret_val > "/dev/stderr" # Restore environment while (env_size > env_stack[call_stack_ptr]) { @@ -573,9 +642,14 @@ function vm_return() { # Restore program counter pc = call_stack[call_stack_ptr--] + print "VM_RETURN: restored PC = " pc > "/dev/stderr" + + # Restore the original instruction at the call position + program[pc] = original_instructions[call_stack_ptr + 1] # Push return value push(ret_val) + print "VM_RETURN: pushed return value " ret_val > "/dev/stderr" debug("Returned with value: " ret_val " and env_size: " env_size) } @@ -601,10 +675,18 @@ function lookup_no_error(name, i) { # State persistence implementation function save_state() { + print "SAVE_STATE_CALLED" > "/dev/stderr" debug("Saving state to: " STATE_FILE) - for (i = 0; i < func_def_size; i++) { - debug("Saving function: " func_def_names[i]) - print "FUNC " func_def_names[i] " " func_def_code[i] > STATE_FILE + # Save functions from FUNCTIONS table + print "SAVE_STATE: Saving " length(FUNCTIONS) " functions" > "/dev/stderr" + for (func_name in FUNCTIONS) { + if (func_name !~ /^__lambda_/) { # Don't save lambda functions + debug("Saving function: " func_name) + print "SAVE_STATE: About to write function " func_name > "/dev/stderr" + print "SAVE_STATE: Function code length: " length(FUNCTIONS[func_name]) > "/dev/stderr" + printf "FUNC %s %s\n", func_name, FUNCTIONS[func_name] > STATE_FILE + print "SAVE_STATE: Saved function " func_name " to " STATE_FILE > "/dev/stderr" + } } close(STATE_FILE) @@ -694,7 +776,7 @@ function add_one() { # Get value from top of stack function vm_get_value() { - val = peek() + val = pop() if (isSymbol(val)) { name = getValue(val) # If it's a function name, just push the name directly |