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 "'"
+        }
+    }
+}
Rumpf <andreas@andreas-desktop> 2009-12-07 01:21:35 +0100 committer Andreas Rumpf <andreas@andreas-desktop> 2009-12-07 01:21:35 +0100 version 0.8.5: bugfixes; compiler now maintained in Nimrod' href='/ahoang/Nim/commit/koch.nim?h=devel&id=90119066adf6a9a2e8d779d4955637c6dd99aba3'>90119066a ^
765366c1f ^






cd292568d ^


90119066a ^






90119066a ^
90119066a ^

90119066a ^
f56816def ^
90119066a ^

90119066a ^


e487c0a24 ^
90119066a ^




90119066a ^
2169fd63b ^
90119066a ^

cd292568d ^



90119066a ^
90119066a ^


2169fd63b ^


5b789f2da ^
cd292568d ^
5b789f2da ^

8c799da86 ^
5b789f2da ^
a58a2f382 ^
5b789f2da ^
90119066a ^
cd292568d ^
5b789f2da ^




cd292568d ^
5b789f2da ^






90119066a ^

























73c355176 ^

90119066a ^











6ff8752be ^
4d5c3ebd4 ^


6ff8752be ^


3091bc495 ^




90119066a ^

















6ff8752be ^
90119066a ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207