diff options
author | elioat <elioat@tilde.institute> | 2025-01-11 22:08:28 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2025-01-11 22:08:28 -0500 |
commit | 1c1d7fefdc5ee13e0bfe9908becf5f51581beef6 (patch) | |
tree | f01f3c0f4a93bfbf91ae3bb067a647bc2498cc23 | |
parent | 3bbd8bafaae0830b541324e4268af317024c6f29 (diff) | |
download | tour-1c1d7fefdc5ee13e0bfe9908becf5f51581beef6.tar.gz |
*
-rwxr-xr-x | awk/forth/f.awk | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/awk/forth/f.awk b/awk/forth/f.awk index 6c18bfc..960fae5 100755 --- a/awk/forth/f.awk +++ b/awk/forth/f.awk @@ -5,23 +5,23 @@ BEGIN { stack_ptr = 0 dict_size = 0 - # Built-in words are stored with their function names - dict["+"] = "math_add" - dict["-"] = "math_sub" - dict["*"] = "math_mul" - dict["/"] = "math_div" - dict["."] = "stack_print" - dict[".s"] = "stack_show" - dict["dup"] = "stack_dup" - dict["drop"] = "stack_drop" - dict["swap"] = "stack_swap" - dict["over"] = "stack_over" - dict["rot"] = "stack_rot" - dict["="] = "compare_eq" - dict["<"] = "compare_lt" - dict[">"] = "compare_gt" - dict["bye"] = "exit_program" - dict["words"] = "list_words" + # Built-in words are stored with their function names and documentation + 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." + dict["/"] = "/ : Divides the second top number by the top number on the stack." + dict["."] = ". : Prints the top of the stack." + dict[".s"] = ".s : Shows all values on the stack." + dict["dup"] = "dup : Duplicates the top value on the stack." + dict["drop"] = "drop : Removes the top value from the stack." + dict["swap"] = "swap : Swaps the top two values on the stack." + dict["over"] = "over : Copies the second top value to the top of the stack." + dict["rot"] = "rot : Rotates the top three values on the stack." + dict["="] = "= : Compares the top two values for equality." + dict["<"] = "< : Checks if the second top value is less than the top value." + dict[">"] = "> : Checks if the second top value is greater than the top value." + dict["bye"] = "bye : Exits the interpreter." + dict["words"] = "words : Lists all available words and their documentation." # State flags compiling = 0 @@ -70,6 +70,7 @@ function interpret(line) { if (compiling) { if (word == ";") { + # Store user-defined word with its name and definition dict[def_name] = "word " current_def compiling = 0 continue @@ -288,6 +289,12 @@ function exit_program() { function list_words() { print "Available words:" for (w in dict) { - print w + split(dict[w], parts, ": ") + if (parts[1] ~ /^word /) { + # If it's a user-defined word, display its name + print w ": " parts[2] " (User-defined)" + } else { + print parts[1] ": " parts[2] + } } } \ No newline at end of file |