about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-01-12 12:56:31 -0500
committerelioat <elioat@tilde.institute>2025-01-12 12:56:31 -0500
commitdf1bbf51de157fdffd189a9df36544d0a1e1375f (patch)
treeeca0a8ed7a311c379d4a1dc1cdc44525cff0be8e
parente5ca28ac779b9c4e9e981694853940f199b0e83d (diff)
downloadtour-df1bbf51de157fdffd189a9df36544d0a1e1375f.tar.gz
*
-rwxr-xr-xawk/forth/f.awk28
-rw-r--r--awk/forth/test.forth34
2 files changed, 48 insertions, 14 deletions
diff --git a/awk/forth/f.awk b/awk/forth/f.awk
index a00a12d..312c8e2 100755
--- a/awk/forth/f.awk
+++ b/awk/forth/f.awk
@@ -105,20 +105,20 @@ function execute_word(word) {
             }
         } else {
             # Built-in words
-            if (dict[word] == "math_add") math_add()
-            else if (dict[word] == "math_sub") math_sub()
-            else if (dict[word] == "math_mul") math_mul()
-            else if (dict[word] == "math_div") math_div()
-            else if (dict[word] == "stack_print") stack_print()
-            else if (dict[word] == "stack_show") stack_show()
-            else if (dict[word] == "stack_dup") stack_dup()
-            else if (dict[word] == "stack_drop") stack_drop()
-            else if (dict[word] == "stack_swap") stack_swap()
-            else if (dict[word] == "stack_over") stack_over()
-            else if (dict[word] == "stack_rot") stack_rot()
-            else if (dict[word] == "compare_eq") compare_eq()
-            else if (dict[word] == "compare_lt") compare_lt()
-            else if (dict[word] == "compare_gt") compare_gt()
+            if (word == "+") math_add()
+            else if (word == "-") math_sub()
+            else if (word == "*") math_mul()
+            else if (word == "/") math_div()
+            else if (word == ".") stack_print()
+            else if (word == ".s") stack_show()
+            else if (word == "dup") stack_dup()
+            else if (word == "drop") stack_drop()
+            else if (word == "swap") stack_swap()
+            else if (word == "over") stack_over()
+            else if (word == "rot") stack_rot()
+            else if (word == "=") compare_eq()
+            else if (word == "<") compare_lt()
+            else if (word == ">") compare_gt()
             else if (word == "bye") exit_program()
             else if (word == "words") list_words()
         }
diff --git a/awk/forth/test.forth b/awk/forth/test.forth
new file mode 100644
index 0000000..1beedaf
--- /dev/null
+++ b/awk/forth/test.forth
@@ -0,0 +1,34 @@
+\ Test arithmetic operations
+10 5 + .          \ Should print 15
+10 5 - .          \ Should print 5
+10 5 * .          \ Should print 50
+10 5 / .          \ Should print 2
+
+\ Test stack manipulation
+1 2 3 .s          \ Should show 3 values: 1 2 3
+dup .             \ Should print 3 again
+drop .            \ Should print 2
+swap .s           \ Should show 2 1
+over .s           \ Should show 2 1 2
+rot .s            \ Should show 1 2 3
+
+\ Test comparisons
+5 5 = .           \ Should print -1 (true)
+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 user-defined words
+: square dup * ;   \ Define a word to square a number
+4 square .         \ Should print 16
+
+: add_three 1 2 + + ;  \ Define a word to add three numbers
+1 2 add_three .    \ Should print 6
+
+\ List all words
+words              \ Should list all available words
+
+bye                \ Exit the interpreter 
\ No newline at end of file