aboutsummaryrefslogtreecommitdiffstats
path: root/awk/scheme
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-07-22 17:47:13 -0400
committerelioat <elioat@tilde.institute>2025-07-22 17:47:13 -0400
commit2addff075d89001e5d21a82905304328f61de225 (patch)
tree1b93d24335c2fc3b7d50e8999c9aee01108a24ca /awk/scheme
parentaa3b4c63c53b28a6345780265c6a370bb05f88f3 (diff)
downloadtour-2addff075d89001e5d21a82905304328f61de225.tar.gz
*
Diffstat (limited to 'awk/scheme')
-rwxr-xr-xawk/scheme/scheme/bin/compiler.awk127
-rwxr-xr-xawk/scheme/scheme/bin/vm.awk85
-rw-r--r--awk/scheme/scheme/test/unit/minimal_function_persistence.scm2
-rw-r--r--awk/scheme/scheme/test/unit/minimal_global_persistence.scm2
4 files changed, 93 insertions, 123 deletions
diff --git a/awk/scheme/scheme/bin/compiler.awk b/awk/scheme/scheme/bin/compiler.awk
index ca4d71b..ae0b5ce 100755
--- a/awk/scheme/scheme/bin/compiler.awk
+++ b/awk/scheme/scheme/bin/compiler.awk
@@ -923,102 +923,71 @@ function compile_define(args, name, params, body, param_array, nparams, i, paren
# Set flag for global definition
print "PUSH_CONST B:1"
print "STORE from_define" # Must match exactly what vm_store checks for
-
+
# Find the function name (everything up to the first space)
i = index(args, " ")
if (i == 0) error("Malformed define expression")
name = substr(args, 1, i - 1)
args = substr(args, i + 1)
- # Check if it's a function or variable definition
- # A function definition has the form: (define name (param1 param2) body)
- # A variable definition has the form: (define name value)
debug("compile_define: args = [" args "]")
if (substr(args, 1, 1) == "(") {
- # Check if this is a function definition or a variable definition with function call
- # Look for the pattern: (name ...) where name is not a function call
- # For (list 1), we need to check if the first word after ( is a function name
- paren_count = 0
- i = 1
- first_word = ""
- while (i <= length(args)) {
+ # Find the matching closing paren for the first group
+ paren_count = 1
+ close_paren = 0
+ for (i = 2; i <= length(args); i++) {
c = substr(args, i, 1)
if (c == "(") paren_count++
if (c == ")") paren_count--
- if (c == " " && paren_count == 1) {
- # Found a space inside the first parentheses
- # Check if the first word is a function name (like list, cons, etc.)
- if (first_word == "list" || first_word == "cons" || first_word == "append" ||
- 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 == "lambda" ||
- first_word == "map" || first_word == "filter") {
- # 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
- print "STORE " name # Store the variable
- return
- } else {
- # This is a function definition with parameters
- break
- }
+ if (paren_count == 0) { close_paren = i; break }
+ }
+ if (close_paren == 0) error("Unmatched parenthesis in define")
+ # Check for non-whitespace after the closing paren
+ rest = substr(args, close_paren + 1)
+ sub(/^[ \t\n]+/, "", rest)
+ if (rest == "") {
+ # This is a variable definition with a parenthesized value
+ if (DEBUG_SEXPR) print "[DEBUG_SEXPR] variable definition (parenthesized value): [" name "] value: [" args "]" > "/dev/stderr"
+ compile_expr(args)
+ print "STORE " name
+ return
+ } else {
+ # This is a function definition
+ params = substr(args, 2, close_paren - 2)
+ body = rest
+ if (DEBUG_SEXPR) print "[DEBUG_SEXPR] function definition: [" name "] params: [" params "] body: [" body "]" > "/dev/stderr"
+ print "LABEL " name
+ nparams = split(params, param_array, " ")
+ for (i = 1; i <= nparams; i++) {
+ print "STORE " param_array[i]
}
- if (paren_count == 1 && c != "(") {
- first_word = first_word c
+ nsexprs = split_sexpressions(body, sexprs)
+ if (DEBUG_SEXPR) {
+ printf("[DEBUG_SEXPR] compile_define: splitting body, found %d expressions\n", nsexprs) > "/dev/stderr"
+ for (j = 1; j <= nsexprs; j++) {
+ printf("[DEBUG_SEXPR] %d: [%s]\n", j, sexprs[j]) > "/dev/stderr"
+ }
}
- i++
- }
-
- # It's a function definition
- paren_count = 1
- i = 2
- while (paren_count > 0 && i <= length(args)) {
- if (substr(args, i, 1) == "(") paren_count++
- if (substr(args, i, 1) == ")") paren_count--
- i++
- }
- if (paren_count > 0) error("Unmatched parenthesis in parameter list")
-
- params = substr(args, 2, i - 3) # Remove parentheses
- body = substr(args, i + 1)
-
- # Create function label
- print "LABEL " name
-
- # Process parameters
- nparams = split(params, param_array, " ")
- for (i = 1; i <= nparams; i++) {
- print "STORE " param_array[i]
- }
-
- # --- Use split_sexpressions for robust multi-expression body support ---
- nsexprs = split_sexpressions(body, sexprs)
- if (DEBUG_SEXPR) {
- printf("[DEBUG_SEXPR] compile_define: splitting body, found %d expressions\n", nsexprs) > "/dev/stderr"
- for (j = 1; j <= nsexprs; j++) {
- printf("[DEBUG_SEXPR] %d: [%s]\n", j, sexprs[j]) > "/dev/stderr"
+ if (nsexprs < 1) {
+ if (DEBUG_SEXPR) print "[DEBUG_SEXPR] No expressions found in function body" > "/dev/stderr"
+ } else {
+ for (j = 1; j <= nsexprs; j++) {
+ expr = sexprs[j]
+ sub(/^[ \t\n]+/, "", expr)
+ sub(/[ \t\n]+$/, "", expr)
+ if (DEBUG_SEXPR) printf("[DEBUG_SEXPR] body expr: [%s]\n", expr) > "/dev/stderr"
+ if (expr != "") compile_expr(expr)
+ }
}
- }
- if (nsexprs < 1) {
- if (DEBUG_SEXPR) print "[DEBUG_SEXPR] No expressions found in function body" > "/dev/stderr"
- } else {
- for (j = 1; j <= nsexprs; j++) {
- expr = sexprs[j]
- sub(/^[ \t\n]+/, "", expr)
- sub(/[ \t\n]+$/, "", expr)
- if (DEBUG_SEXPR) printf("[DEBUG_SEXPR] body expr: [%s]\n", expr) > "/dev/stderr"
- if (expr != "") compile_expr(expr)
+ for (i = nparams; i >= 1; i--) {
+ print "POP_ENV"
}
+ print "RETURN"
+ return
}
- # --- End local define support ---
- for (i = nparams; i >= 1; i--) {
- print "POP_ENV"
- }
- print "RETURN"
} else {
- # Variable definition
- debug("Defining variable: " name " with value: " args)
+ # Variable definition (non-parenthesized value)
+ if (DEBUG_SEXPR) print "[DEBUG_SEXPR] variable definition: [" name "] value: [" args "]" > "/dev/stderr"
compile_expr(args) # Compile the value
print "STORE " name # Store the variable
}
diff --git a/awk/scheme/scheme/bin/vm.awk b/awk/scheme/scheme/bin/vm.awk
index 7e68e92..f0fcd15 100755
--- a/awk/scheme/scheme/bin/vm.awk
+++ b/awk/scheme/scheme/bin/vm.awk
@@ -931,7 +931,7 @@ function vm_define_function(name, start_pc) {
}
# Function call implementation
-function vm_call_function(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, param_names, nparams, k) {
# Get function name from stack
func_name = pop()
debug("Calling function: " func_name)
@@ -1152,62 +1152,59 @@ function vm_call_function(code_lines, j, saved_pc, saved_env_size, arg, param_na
saved_pc = pc
saved_env_size = env_size
- # AWK FEATURE: split(string, array, separator) splits string into array elements
- # Unlike JS string.split() which returns an array, this populates an existing array
split(FUNCTIONS[func_name], code_lines, "\n")
- # Check if this is a parameterized function
- if (code_lines[1] ~ /^STORE /) {
- # This is a parameterized function (lambda)
- # Get parameter name from STORE instruction
- param_name = substr(code_lines[1], 7)
- debug("Found parameter name: " param_name)
- debug("FUNCTION_PARAM: " param_name)
-
- # Get argument from stack
- arg = pop()
- debug("Function argument: " arg)
- debug("FUNCTION_ARG: " arg)
-
- # 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++
- debug("FUNCTION_ENV_STORE: " param_name " = " arg " at index " (env_size-1))
-
- # Execute function code directly, skipping STORE and POP_ENV instructions
- for (j = 2; j <= length(code_lines); j++) {
+ # --- Multi-parameter function support ---
+ # Collect all leading STORE instructions as parameter names
+ nparams = 0
+ for (j = 1; j <= length(code_lines); j++) {
+ if (code_lines[j] ~ /^STORE /) {
+ nparams++
+ param_names[nparams] = substr(code_lines[j], 7)
+ } else {
+ break
+ }
+ }
+ if (nparams > 0) {
+ debug("Function parameters: " nparams)
+ # Pop arguments in reverse order so first argument is bound to first param
+ for (k = nparams; k >= 1; k--) {
+ arg = pop()
+ param_name = param_names[k]
+ debug("Binding argument to parameter: " param_name " = " arg)
+ env_name[env_size] = param_name
+ env_val[env_size] = arg
+ env_size++
+ debug("FUNCTION_ENV_STORE: " param_name " = " arg " at index " (env_size-1))
+ }
+ # Execute function code, skipping STORE and POP_ENV instructions
+ for (j = nparams + 1; j <= length(code_lines); j++) {
if (code_lines[j] != "" && code_lines[j] != "POP_ENV") {
debug("Executing function instruction: " code_lines[j])
execute(code_lines[j])
}
}
-
- # Clean up parameter
- vm_pop_env()
-
- # Return to caller
+ # Clean up parameters
+ for (k = 1; k <= nparams; k++) {
+ vm_pop_env()
+ }
debug("Function completed, returning to PC: " saved_pc)
pc = saved_pc
return
- } else {
- # This is a built-in function or non-parameterized function
- debug("Calling non-parameterized function: " func_name)
+ }
+ # --- End multi-parameter support ---
- # Execute all function code directly
- for (j in code_lines) {
- if (code_lines[j] != "") {
- debug("Executing function instruction: " code_lines[j])
- execute(code_lines[j])
- }
+ # This is a built-in function or non-parameterized function
+ debug("Calling non-parameterized function: " func_name)
+ for (j in code_lines) {
+ if (code_lines[j] != "") {
+ debug("Executing function instruction: " code_lines[j])
+ execute(code_lines[j])
}
-
- # Return to caller
- debug("Function completed, returning to PC: " saved_pc)
- pc = saved_pc
- return
}
+ debug("Function completed, returning to PC: " saved_pc)
+ pc = saved_pc
+ return
}
# Function call with argument count implementation
diff --git a/awk/scheme/scheme/test/unit/minimal_function_persistence.scm b/awk/scheme/scheme/test/unit/minimal_function_persistence.scm
new file mode 100644
index 0000000..c6825e4
--- /dev/null
+++ b/awk/scheme/scheme/test/unit/minimal_function_persistence.scm
@@ -0,0 +1,2 @@
+(define add2 (lambda (x) (+ x 2)))
+(add2 40) \ No newline at end of file
diff --git a/awk/scheme/scheme/test/unit/minimal_global_persistence.scm b/awk/scheme/scheme/test/unit/minimal_global_persistence.scm
new file mode 100644
index 0000000..7b75b28
--- /dev/null
+++ b/awk/scheme/scheme/test/unit/minimal_global_persistence.scm
@@ -0,0 +1,2 @@
+(define foo 42)
+foo \ No newline at end of file