about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <elioat@tilde.institute>2025-01-02 07:00:13 -0500
committerelioat <elioat@tilde.institute>2025-01-02 07:00:13 -0500
commit9c4bde6f5e44454a6c1db0a53cb4b94732711272 (patch)
treedceeec86a9760370d937fd203e9f768ccb71b9c3
parent8a82575121fdde1f891c754411abaa4872a144c9 (diff)
downloadtour-9c4bde6f5e44454a6c1db0a53cb4b94732711272.tar.gz
*
-rwxr-xr-xawk/forth/f.awk152
1 files changed, 152 insertions, 0 deletions
diff --git a/awk/forth/f.awk b/awk/forth/f.awk
new file mode 100755
index 0000000..260310e
--- /dev/null
+++ b/awk/forth/f.awk
@@ -0,0 +1,152 @@
+#!/usr/bin/awk -f
+
+# Forth interpreter in AWK
+
+# Add a dictionary array to hold available words
+BEGIN {
+    print "Welcome to the AWK Forth Interpreter!"
+    print "Type your commands below. Use 'bye' to quit."
+    # Initialize the dictionary with basic words
+    words["+"] = "Addition"
+    words["-"] = "Subtraction"
+    words["*"] = "Multiplication"
+    words["/"] = "Division"
+    words["dup"] = "Duplicate the top value"
+    words["over"] = "Copy the second value to the top"
+    words["swap"] = "Swap the top two values"
+    words["."] = "Print top of stack"
+    words["bye"] = "Exit the interpreter"
+}
+
+# Stack to hold values
+function push(value) {
+    stack[++top] = value
+}
+
+function pop() {
+    if (top < 0) {
+        print "Error: Stack underflow"
+        return 0
+    }
+    return stack[top--]
+}
+
+function add() {
+    if (top < 1) {
+        print "Error: Not enough values on the stack"
+        return
+    }
+    push(pop() + pop())
+}
+
+# Function for subtraction
+function subtract() {
+    if (top < 1) {
+        print "Error: Not enough values on the stack"
+        return
+    }
+    second = pop()
+    first = pop()
+    push(first - second)
+}
+
+# Function for multiplication
+function multiply() {
+    if (top < 1) {
+        print "Error: Not enough values on the stack"
+        return
+    }
+    push(pop() * pop())
+}
+
+# Function for division
+function divide() {
+    if (top < 1) {
+        print "Error: Not enough values on the stack"
+        return
+    }
+    second = pop()
+    if (second == 0) {
+        print "Error: Division by zero"
+        return
+    }
+    first = pop()
+    push(first / second)
+}
+
+# Function to duplicate the top value
+function dup() {
+    if (top < 0) {
+        print "Error: Stack is empty"
+        return
+    }
+    push(stack[top])  # Push the top value again
+}
+
+# Function to copy the second value to the top
+function over() {
+    if (top < 1) {
+        print "Error: Not enough values on the stack"
+        return
+    }
+    push(stack[top - 1])  # Push the second value
+}
+
+# Function to swap the top two values
+function swap() {
+    if (top < 1) {
+        print "Error: Not enough values on the stack"
+        return
+    }
+    temp = pop()  # Pop the top value
+    second = pop()  # Pop the second value
+    push(temp)  # Push the first value back
+    push(second)  # Push the second value back
+}
+
+function print_top() {
+    if (top < 0) {
+        print "Error: Stack is empty"
+        return
+    }
+    print stack[top]
+}
+
+# Function to list all available words
+function list_words() {
+    print "Available words:"
+    for (word in words) {
+        print word
+    }
+}
+
+# Main loop to read commands
+{
+    for (i = 1; i <= NF; i++) {
+        if ($i ~ /^[0-9]+$/) {
+            push($i)
+        } else if ($i == "+") {
+            add()
+        } else if ($i == "-") {
+            subtract()
+        } else if ($i == "*") {
+            multiply()
+        } else if ($i == "/") {
+            divide()
+        } else if ($i == "dup") {
+            dup()
+        } else if ($i == "over") {
+            over()
+        } else if ($i == "swap") {
+            swap()
+        } else if ($i == ".") {
+            print_top()
+        } else if ($i == "bye") {
+            exit
+        } else if ($i == "words") {
+            list_words()
+        } else {
+            print "Error: Unknown command '" $i "'"
+        }
+    }
+}