about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-01-20 10:57:31 -0500
committerelioat <elioat@tilde.institute>2025-01-20 10:57:31 -0500
commitbeaad9c475ec66243b2f0f0acc210e5082526664 (patch)
tree3db09505074fc9e402f5abefc76d54d0c74e2cfd
parent1dda44054a253e027a2cb6f8d33ad56dac6bda2b (diff)
downloadtour-beaad9c475ec66243b2f0f0acc210e5082526664.tar.gz
*
-rw-r--r--awk/scheme/scheme/README.md16
-rwxr-xr-xawk/scheme/scheme/bin/compiler.awk71
2 files changed, 54 insertions, 33 deletions
diff --git a/awk/scheme/scheme/README.md b/awk/scheme/scheme/README.md
index a5cdc80..6fcafe6 100644
--- a/awk/scheme/scheme/README.md
+++ b/awk/scheme/scheme/README.md
@@ -113,6 +113,22 @@ scheme> (cons 1 (cons 2 (cons 3 nil)))
 P:1  ; This represents the list (1 2 3)
 ```
 
+### Define
+- Define a new variable or function
+```scheme
+scheme> (define x 10)
+N:10
+scheme> (define add (x y) (+ x y))
+F:1
+```
+
+### Let
+- Define local bindings within a let expression
+```scheme
+scheme> (let ((x 10) (y 20)) (+ x y))
+N:30
+```
+
 ## Expression Structure
 - All expressions must be properly parenthesized
 - Operators come first in a form (prefix notation)
diff --git a/awk/scheme/scheme/bin/compiler.awk b/awk/scheme/scheme/bin/compiler.awk
index 6a57f19..615ee41 100755
--- a/awk/scheme/scheme/bin/compiler.awk
+++ b/awk/scheme/scheme/bin/compiler.awk
@@ -351,40 +351,45 @@ function compile_define(args,    name, params, body, param_array, nparams, i, pa
     if (i == 0) error("Malformed define expression")
     name = substr(args, 1, i - 1)
     args = substr(args, i + 1)
-    
-    # Find parameter list
-    if (substr(args, 1, 1) != "(") error("Missing parameter list in define")
-    
-    # Find matching closing parenthesis for params
-    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]
-    }
-    
-    # Compile function body
-    compile_expr(body)
-    
-    # Clean up parameters and return
-    for (i = nparams; i >= 1; i--) {
-        print "POP_ENV"
+
+    # Check if it's a function or variable definition
+    if (substr(args, 1, 1) == "(") {
+        # 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]
+        }
+
+        # Compile function body
+        compile_expr(body)
+
+        # Clean up parameters and return
+        for (i = nparams; i >= 1; i--) {
+            print "POP_ENV"
+        }
+        print "RETURN"
+    } else {
+        # It's a variable definition
+        debug("Defining variable: " name " with value: " args)
+        compile_expr(args)  # Compile the value
+        print "STORE " name  # Store the variable
     }
-    print "RETURN"
 }
 
 function compile_expr(expr,    split_result, op, args) {