diff options
author | elioat <elioat@tilde.institute> | 2025-01-12 08:54:02 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-01-12 08:54:02 -0500 |
commit | b0fde81d0ae35673277e2ffccd209b8e78675bf5 (patch) | |
tree | 62809862e50a50ea019530ff3ca846834315dd17 | |
parent | 5547204f98172f130c6138a3b7f7c7f5fe1940ed (diff) | |
download | tour-b0fde81d0ae35673277e2ffccd209b8e78675bf5.tar.gz |
*
-rwxr-xr-x | awk/forth/f.awk | 29 |
1 files changed, 9 insertions, 20 deletions
diff --git a/awk/forth/f.awk b/awk/forth/f.awk index 6f6f546..380563d 100755 --- a/awk/forth/f.awk +++ b/awk/forth/f.awk @@ -5,7 +5,7 @@ BEGIN { stack_ptr = 0 dict_size = 0 - # Built-in words are stored with their function names and documentation + # Built-in words, and some documentation (I could use stack comments, but I find those sort of unintuitive) dict["+"] = "+ : Adds the top two numbers on the stack." dict["-"] = "- : Subtracts the top number from the second top number on the stack." dict["*"] = "* : Multiplies the top two numbers on the stack." @@ -28,7 +28,7 @@ BEGIN { current_def = "" def_name = "" - # If no input file specified, enter REPL mode + # If an input file isn't specified, enter REPL mode if (ARGC == 1) { repl() } @@ -51,7 +51,7 @@ function repl() { } function interpret(line) { - # Remove comments from the line + gsub(/\(.*\)/, "", line) # Remove everything from ( to ) n = split(line, words, /[ \t]+/) @@ -97,7 +97,7 @@ function execute_word(word) { } } } else { - # Built-in word + # 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() @@ -120,7 +120,6 @@ function execute_word(word) { } } -# Stack operations function push(val) { stack[stack_ptr++] = val } @@ -133,7 +132,6 @@ function pop() { return stack[--stack_ptr] } -# Math operations function math_add() { if (stack_ptr < 2) { print "Error: Stack underflow" @@ -178,7 +176,6 @@ function math_div() { push(int(a / b)) } -# Stack manipulation function stack_print() { if (stack_ptr < 1) { print "Error: Stack underflow" @@ -248,7 +245,6 @@ function stack_rot() { push(a) } -# Comparison operations function compare_eq() { if (stack_ptr < 2) { print "Error: Stack underflow" @@ -279,43 +275,36 @@ function compare_gt() { push(a > b ? -1 : 0) } -# New function to handle exiting the program function exit_program() { print "Exiting program." exit 0 } -# New function to list all available words function list_words() { print "Available words:" - # Arrays to hold built-in and user-defined words + # Separate arrays to hold built-in and user-defined words split("", built_in_words) split("", user_defined_words) - # Populate the arrays for (w in dict) { split(dict[w], parts, ": ") if (parts[1] ~ /^word /) { - # Store user-defined words user_defined_words[w] = parts[2] } else { - # Store built-in words built_in_words[w] = parts[2] } } - # Sort built-in words manually + # Sort built-in words manually because I'm picky n = 0 for (w in built_in_words) { sorted_words[n++] = w } - # Simple bubble sort for the sorted_words array for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (sorted_words[i] > sorted_words[j]) { - # Swap temp = sorted_words[i] sorted_words[i] = sorted_words[j] sorted_words[j] = temp @@ -323,13 +312,13 @@ function list_words() { } } - # Print sorted built-in words + # First print the built-in words for (i = 0; i < n; i++) { print sorted_words[i] ": " built_in_words[sorted_words[i]] } - # Print user-defined words + # Then print the user-defined words for (w in user_defined_words) { - print w ": " user_defined_words[w] " (User-defined)" + print w ": " user_defined_words[w] " ( User-defined )" } } \ No newline at end of file |