about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xawk/forth/f.awk40
-rw-r--r--awk/forth/test.forth6
2 files changed, 42 insertions, 4 deletions
diff --git a/awk/forth/f.awk b/awk/forth/f.awk
index 73fea1e..4894b34 100755
--- a/awk/forth/f.awk
+++ b/awk/forth/f.awk
@@ -67,6 +67,8 @@ function interpret(line) {
         word = words[i]
         if (word == "") continue
         
+        # print "Processing word: " word  # Debugging output (commented out)
+        
         if (word == ":") {
             compiling = 1
             i++
@@ -86,6 +88,12 @@ function interpret(line) {
             continue
         }
         
+        # Execute the word and skip further processing if it's .s
+        if (word == ".s") {
+            execute_word(word)
+            break  # Exit the loop after executing .s
+        }
+        
         execute_word(word)
     }
 }
@@ -110,7 +118,10 @@ function execute_word(word) {
             else if (word == "*") math_mul()
             else if (word == "/") math_div()
             else if (word == ".") stack_print()
-            else if (word == ".s") stack_show()
+            else if (word == ".s") {
+                # print "Executing .s command"  # Debugging output
+                stack_show()
+            }
             else if (word == "dup") stack_dup()
             else if (word == "drop") stack_drop()
             else if (word == "swap") stack_swap()
@@ -121,6 +132,28 @@ function execute_word(word) {
             else if (word == ">") compare_gt()
             else if (word == "bye") exit_program()
             else if (word == "words") list_words()
+            else if (word == "if") {
+                # Handle the if statement
+                if_condition = pop()
+                if (if_condition == 0) {
+                    # Skip to the next part until we find 'then' or 'else'
+                    skip_if = 1
+                }
+            }
+            else if (word == "else") {
+                # Handle the else statement
+                if (skip_if) {
+                    skip_if = 0  # Reset the skip flag
+                } else {
+                    # Skip to the next part until we find 'then'
+                    skip_else = 1
+                }
+            }
+            else if (word == "then") {
+                # End of the conditional
+                skip_if = 0
+                skip_else = 0
+            }
         }
     } else {
         print "Error: Unknown word '" word "'"
@@ -197,6 +230,11 @@ function stack_show() {
         printf "%s ", stack[i]
     }
     print ""
+    # print "Stack state after .s: "  # Debugging output
+    # for (i = 0; i < stack_ptr; i++) {
+    #     print stack[i]
+    # }
+    # print ""  # Add a newline for better readability
 }
 
 function stack_dup() {
diff --git a/awk/forth/test.forth b/awk/forth/test.forth
index 1beedaf..daa6943 100644
--- a/awk/forth/test.forth
+++ b/awk/forth/test.forth
@@ -17,9 +17,9 @@ rot .s            \ Should show 1 2 3
 5 3 < .          \ Should print 0 (false)
 3 5 > .          \ Should print 0 (false)
 
-\ Test conditionals
-10 20 if .s then  \ Should print 1 2 (since the condition is true)
-10 5 if .s else 1 then  \ Should print 1 (since the condition is false)
+\ Test conditionals within user-defined words
+: test_if 10 20 if .s then ;  \ Should print 1 2 (since the condition is true)
+: test_else 10 5 if .s else 1 then ;  \ Should print 1 (since the condition is false)
 
 \ Test user-defined words
 : square dup * ;   \ Define a word to square a number