-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrapforth.py
More file actions
executable file
·272 lines (222 loc) · 6.86 KB
/
strapforth.py
File metadata and controls
executable file
·272 lines (222 loc) · 6.86 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python
# A Forth interpreter in Python.
# First step on an exercise on bootstrapping
# Copyright (C) 2024 Pablo Martin <pablo@odkq.com>. See LICENSE file.
from sys import argv
st = [] # The data stack
compiling = False # Whether we are in 'compile' mode
compiling_symbol = None # Current symbol being compiled
no_new_line = False # Output new line after line executed or not
dead_branch = False
base = 10
infix = None # Certain words get the next token
# like 'see word' or 'include <path>'
# This var stores the word to be executed
# when the second one is parsed
def execforth(line_or_tokens):
''' Execute a string of tokens separated by spaces,
or directly a list of tokens '''
global in_comment
in_comment = False
if isinstance(line_or_tokens, str):
tokens = line_or_tokens.split(' ')
else:
tokens = line_or_tokens
for token in tokens:
if exectoken(token):
break
def exectoken(token):
''' Main interpreter function. Operate over one token
Returns False if the rest of the line/tokens are to be executed,
and True if not '''
if token == '':
return False
# Handle ( comments ) between parenthesis
global in_comment
if in_comment:
if token == ')':
in_comment = False
return False # Discard the tokens within a comment
else:
if token == '(':
in_comment = True
return False
if dead_branch: # We are on the non-executing part of an if
if token not in ['else', 'then']:
return False
# Look for infix meta words that get the next token as argument
# call them
global infix
if infix is None:
if token in infix_words.keys():
infix = infix_words[token]
return False
else:
fun = infix
infix = None # Reset before calling or it will get the first
# word in the included file as infix suffix
fun(token)
return False
# Handle compiling tokens, : <symbol> <tokens> ;
global compiling
global compiling_symbol
if compiling:
if token == ';':
compiling = False
compiling_symbol = None
return False
if compiling_symbol is None:
compiling_symbol = token
# Set the symbol as an array of tokens
sym[compiling_symbol] = []
return False
sym[compiling_symbol].append(token)
return False
else:
if token == ':':
compiling = True
return False
# A number, push it as integer
try:
i = int(token, base)
push(i)
return False
except ValueError:
pass
# line comment, discard all the rest of tokens in the line
if token == '\\':
return True
# If it is not a number, a comment, or the see keyword, it
# needs to be symbol
if token not in sym:
print(f'symbol \'{token}\' not defined')
return True
# A 'compiled word'
if isinstance(sym[token], list):
execforth(sym[token])
return False
# A python function (code)
try:
sym[token]()
return False
except IndexError:
print('empty stack')
# Stack manipulation abbreviations
def push(a):
st.append(a)
def pop():
return st.pop()
# FORTH WORDS
def swap():
" Swap last and second to last element in stack "
st[-1], st[-2] = st[-2], st[-1]
def rot():
" Rotate the top 3 elements 6 4 5 -> 4 5 6 "
st[-1], st[-2], st[-3] = st[-3], st[-2], st[-1]
def nip():
''' Delete second to last element in the stack '''
del st[-2]
def tuck():
''' Insert top element in the second to last position '''
st[-2:-2] = [st[-1]]
def pick():
''' Get in the top position the element indexed '''
idx = pop()
push(st[-(idx + 1)])
return idx
def roll():
''' get the element indexed, and remove from its original position '''
idx = pick()
del st[-(idx + 2)]
def div():
''' Integer division '''
divisor = pop()
dividend = pop()
push(dividend // divisor)
def ifword():
global dead_branch
condition = pop()
if condition == 0:
dead_branch = True
def elseword():
global dead_branch
if dead_branch:
dead_branch = False
else:
dead_branch = True
def thenword():
global dead_branch
dead_branch = False
def hex():
global base
base = 16
def see(name):
''' Show contents of a compiled symbol '''
if isinstance(sym[name], list):
if no_new_line:
# Maintain byte-by-byte output similarity with gforth for testing
print('')
print(f": {name} \n {' '.join([str(e) for e in sym[name]])} ;",
end='' if no_new_line else '\n')
else:
# Here the coherent thing would be to show the code for the python
# function
print(f"code {name}",
end='' if no_new_line else '\n')
def include(path):
execfile(path)
def forthprint():
a = pop()
if isinstance(a, int):
if base == 16:
try:
s = '%2.2x' % a
except TypeError:
print(f"a [{a}] type {type(a)}")
else:
s = str(a)
else:
s = str(a)
print(s, end= ' ' if no_new_line else '\n')
infix_words = {'see': see,
'include': include}
# Table of symbols. Pointing to the above functions or defined directly
# inline
sym = {'.': forthprint,
'+': lambda: push(pop() + pop()),
'-': lambda: push(-(pop() - pop())),
'*': lambda: push(pop() * pop()),
'=': lambda: push(-1 if pop() == pop() else 0),
'<>': lambda: push(-1 if pop() != pop() else 0),
'>': lambda: push(-1 if pop() < pop() else 0),
'dup': lambda: push(st[-1]), # Duplicate top element
'swap': swap, 'rot': rot, 'nip': nip, 'tuck': tuck,
'pick': pick, 'roll': roll, 'see': see, '/': div,
'drop': lambda: pop(), 'hex': hex,
'over': lambda: push(st[-2]),
'if': ifword, 'else': elseword, 'then': thenword,
'.s': lambda: print(f"<{len(st)}> {' '.join([str(e) for e in st])}",
end=' ' if no_new_line else '\n')
}
def execfile(filename):
global no_new_line
f = open(filename, 'r')
for line in f.readlines():
# Do not output a '\n' after each line executed
no_new_line = True
# Remove extra '\n' at the end
if line[-1] == '\n':
line = line[:-1]
execforth(line)
f.close()
# Entrypoint
if len(argv) == 2:
execfile(argv[1]) # If we receive an argument, it is a filename to execute
else:
# Interactive usage
while True:
try:
line = input()
except EOFError:
break
execforth(line)