-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathawkforth.awk
More file actions
executable file
·255 lines (239 loc) · 4.47 KB
/
awkforth.awk
File metadata and controls
executable file
·255 lines (239 loc) · 4.47 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#! /usr/local/bin/gawk -f
#
# FORTH inerpreter by awk
#
# 2012.05.07 Pstack & standard op
# 2012.05.09 ( comment) & ." string"
# 2012.05.10 word
#
#
BEGIN{
Version=1.1
Pptr=0 # Parameter Stack Pointer
Rptr=0 # Return Stack Pointer
Pmax=30000
Rmax=30000
P[0]="" # Parameter Stack
R[0]="" # Return Stack
M[0]="" # Memory
# Words[],Wattr[]
TRUE=-1
FALSE=0
Printbegin=""
Printend=""
Comment=""
Compile=""
Variable=""
print "* awkforth *"
print "-- " Version
}
{
r=eval($0);
if (r==0) {
if (Compile!="") {
print " compiled"
} else {
print " ok"
}
} else {
print "ERR: " r
}
}
function eval(str ,wd,wds,num,w,wj,i,j,k,r){
r=0
num = split(str,wd,FS,wds)
for(i=1;i<=num;i++) {
w=wd[i]
if (Compile=="" && w==":") { # compile mode
Compile=":"
continue
}
if (Compile==":") { # compile word
Compile=w
Words[Compile]=""
continue
}
if (Compile!="") { # compile
if (w==";") {
Wattr[Compile]="W"
Compile=""
} else {
Words[Compile]=Words[Compile] " " w
}
continue
}
if (w=="VARIABLE") { # define Variable
Variable="V"
continue
}
if (Variable=="V") {
Words[w]=0
Wattr[w]="V"
Variable=""
continue
}
if (w=="CONSTANT") { # define Constant
Variable="C"
continue
}
if (Variable=="C") {
Words[w]=pop()
Wattr[w]="C"
Variable=""
continue
}
if (w=="(") { # コメント
Comment="("; continue
}
if (Comment!="" && substr(w,length(w),1)==")") {
Comment=""; continue
}
if (w==".\"" || w==".'") { # 文字列表示
Printbegin=w
Printend=last(w)
continue
}
if (Printbegin!="") {
for(j=i;j<=num;j++) {
i++
wj=wd[j]
if (wj==Printbegin) continue ;
if(last(wj)==Printend) {
string = string head(wj)
print string
string=""
PrintBegin=""
break
}
string=string wj
if (j<num) string=string wds[j];
}
}
if ((r1=isnum(w))&&(r2=isop(w))) r=r1+r2;
delete wd[i]
delete wds[i]
}
return r
}
##
function push(n) {
if (Pptr>=Pmax) {
print "# P Stack Overflow!"
return 0
}
P[Pptr]=n
Pptr++
}
function pop( n) {
if (Pptr<=0) {
print "# P stack Underflow!"
return 0
}
--Pptr
n=P[Pptr]
return n
}
function last(word){ # 文字列の最後一文字
return substr(word,length(word),1)
}
function head(word){ # 文字列の先頭一文字
return substr(word,1,length(word)-1)
}
function isnum(n) { # 現在、整数のみ...
if (n~"^[-]*[0-9]+$") {
push(n); return 0
} else {
return 1
}
}
##
function isop(x ,t,n,nn,r) {
switch(x) {
case ".": # . ( n -- )
print " " pop()
r=0;break
case "+": # ( n1 n2 -- n2+n1 )
t=pop();push(pop()+t)
r=0;break
case "-": # ( n1 n2 -- n2-n1 )
t=pop();push(pop()-t)
r=0;break
case "*": # ( n1 n2 -- n2*n1 )
t=pop();push(pop()*t)
r=0;break
case "/": # ( n1 n2 -- n2/n1 )
t=pop();push(pop()/t)
r=0;break
case "1+": # ( n -- n+1 )
t=pop();push(t+1)
r=0;break
case "1-": # ( n -- n-1 )
t=pop();push(t-1)
r=0;break
case "NEGATE": # ( n -- -n )
t=pop();push(-t)
r=0;break
case "ABS": # ( n -- |n| )
t=pop();push(t+0<0?-t:t+0)
r=0;break
case "MAX": # ( n1 n2 -- n3 )
t=pop();n=pop()
push(t>n?t:n)
r=0;break
case "MIN": # ( n1 n2 -- n3 )
t=pop();n=pop()
push(t<n?t:n)
r=0;break
case "<": # ( n1 n2 -- n3 )
t=pop();n=pop()
push(n<t?TRUE:FALSE)
r=0;break
case ">": # ( n1 n2 -- n3 )
t=pop();n=pop()
push(n>t?TRUE:FALSE)
r=0;break
case "DROP": # ( n -- )
pop()
r=0;break
case "DUP": # ( n -- n n )
t=pop();push(t);push(t)
r=0;break
case "DDUP": # ( n -- n n n )
t=pop();push(t);push(t);push(t)
r=0;break
case "OVER": # ( n1 n2 -- n1 n2 n1 )
t=pop();n=pop();push(n);push(t);push(n)
r=0;break
case "ROT": # ( n1 n2 n3 -- n2 n3 n1 )
t=pop();n=pop();nn=pop()
push(n);push(t);push(nn)
r=0;break
case "SWAP": # ( n1 n2 -- n2 n1 )
t=pop();n=pop()
push(t);push(n)
r=0;break
case "VLIST": # ( -- )
for(t in Words) {
if (Wattr[t]=="V") print t
}
r=0;break
case "CLIST": # ( -- )
for(t in Words) {
if (Wattr[t]=="C") print t
}
r=0;break
case "WLIST": # ( -- )
for(t in Words) {
if (Wattr[t]=="W") print t
}
r=0;break
case "LIST": # ( -- )
for(t in Words) {
print t,Wattr[t]
}
r=0;break
default:
r=n
}
return r
}