mirror of
https://github.com/russolsen/sallyforth
synced 2024-12-25 21:58:18 +01:00
General cleanup, use single quotes in Python, added single quoted strings to SallyForth.
This commit is contained in:
parent
8785b97f03
commit
0f63796331
4 changed files with 46 additions and 38 deletions
|
@ -24,7 +24,7 @@ class Forth:
|
|||
def __init__(self, startup=None):
|
||||
self.stack = Stack()
|
||||
self.dictionary = {
|
||||
'*prompt*': const_f("SallyForth>> "),
|
||||
'*prompt*': const_f('SallyForth>> '),
|
||||
'true': const_f(True),
|
||||
'false': const_f(False),
|
||||
'nil': const_f(None),
|
||||
|
@ -108,9 +108,9 @@ class Forth:
|
|||
if token in self.dictionary:
|
||||
word = self.dictionary[token]
|
||||
if 'immediate' in word.__dict__:
|
||||
#print("before immediate word:", self, self.dictionary)
|
||||
#print('before immediate word:', self, self.dictionary)
|
||||
word(self)
|
||||
#print("after immediate word:", self, self.dictionary)
|
||||
#print('after immediate word:', self, self.dictionary)
|
||||
else:
|
||||
self.compiler.add_instruction(self.dictionary[token])
|
||||
return
|
||||
|
@ -118,7 +118,7 @@ class Forth:
|
|||
n = to_number(token)
|
||||
if n == None:
|
||||
self.compiler = None
|
||||
print(token, "? Compile terminated.")
|
||||
print(f'{token}? Compile terminated.')
|
||||
else:
|
||||
self.compiler.add_instruction(push_value_f(n))
|
||||
|
||||
|
@ -133,12 +133,12 @@ class Forth:
|
|||
|
||||
n = to_number(token)
|
||||
if n == None:
|
||||
print(token, "?")
|
||||
print(f'{token}?')
|
||||
else:
|
||||
self.stack.push(n)
|
||||
|
||||
def dump(self):
|
||||
print("Forth:", self)
|
||||
print("Stack:", self.stack)
|
||||
print("Dictionary:", self.dictionary)
|
||||
print("Compiler:", self.compiler)
|
||||
print('Forth:', self)
|
||||
print('Stack:', self.stack)
|
||||
print('Dictionary:', self.dictionary)
|
||||
print('Compiler:', self.compiler)
|
||||
|
|
|
@ -3,11 +3,10 @@ import readline
|
|||
from os import path
|
||||
|
||||
def is_string(token):
|
||||
#print("is string:", token, token[0], token[0] == '"')
|
||||
return token[0] == '"'
|
||||
return token[0] == '"' or token[0] == "'"
|
||||
|
||||
def is_space(ch):
|
||||
return ch == " " or ch == "\t" or ch == "\n"
|
||||
return ch == ' ' or ch == '\t' or ch == '\n'
|
||||
|
||||
def tokenize(s):
|
||||
state = 'start'
|
||||
|
@ -23,12 +22,19 @@ def tokenize(s):
|
|||
continue
|
||||
elif state == 'start' and is_space(ch):
|
||||
continue
|
||||
elif state == 'start' and ch == "'":
|
||||
token = ch
|
||||
state = 's_string'
|
||||
elif state == 'start' and ch == '"':
|
||||
token = ch
|
||||
state = 'string'
|
||||
elif state == 'start':
|
||||
token = ch
|
||||
state = 'word'
|
||||
elif state == 's_string' and ch == "'":
|
||||
tokens.append(token)
|
||||
state = 'start'
|
||||
token = ''
|
||||
elif state == 'string' and ch == '"':
|
||||
tokens.append(token)
|
||||
state = 'start'
|
||||
|
@ -37,7 +43,7 @@ def tokenize(s):
|
|||
tokens.append(token)
|
||||
state = 'start'
|
||||
token = ''
|
||||
elif state == 'word' or state == 'string':
|
||||
elif state == 'word' or state == 'string' or state == 's_string':
|
||||
token += ch
|
||||
else:
|
||||
print(f'State: [{state}] token: [{token}] ch: [{ch}]???')
|
||||
|
@ -51,7 +57,7 @@ def read_tokens(read_f):
|
|||
return tokenize(line)
|
||||
|
||||
def forth_prompt():
|
||||
return input("SallyForth>> ")
|
||||
return input('SallyForth>> ')
|
||||
|
||||
def file_read_f(f):
|
||||
def read_it():
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
"Executing " . *source* . nl
|
||||
|
||||
"builtins" require
|
||||
"time" require
|
||||
"math" require
|
||||
"sys" require
|
||||
|
@ -35,3 +36,5 @@
|
|||
;
|
||||
|
||||
"init.sf" source-if-exists
|
||||
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ def import_native_module(forth, m, alias=None, excludes=[]):
|
|||
names = [x for x in raw_names if x not in excludes]
|
||||
for name in names:
|
||||
localname = f'{alias}.{name}'
|
||||
print(localname)
|
||||
#print(localname)
|
||||
val = m.__getattribute__(name)
|
||||
if isfunction(val) or isbuiltin(val):
|
||||
forth.dictionary[localname] = native_function_handler(val)
|
||||
|
@ -42,13 +42,13 @@ def w_source(f):
|
|||
return 1
|
||||
|
||||
def execute_f(name, instructions):
|
||||
# print("execute_f:", len(instructions))
|
||||
# print('execute_f:', len(instructions))
|
||||
def inner(forth, debug=False):
|
||||
# print("inner f:", name)
|
||||
# print("inner f:", len(instructions))
|
||||
# print('inner f:', name)
|
||||
# print('inner f:', len(instructions))
|
||||
i = 0
|
||||
while i >= 0:
|
||||
# print(i, "=>", instructions[i])
|
||||
# print(i, '=>', instructions[i])
|
||||
delta = instructions[i](forth)
|
||||
i += delta
|
||||
return 1
|
||||
|
@ -56,14 +56,14 @@ def execute_f(name, instructions):
|
|||
|
||||
def ifnot_jump_f(n):
|
||||
def ifnot_jump(forth):
|
||||
# print("If not jump:")
|
||||
# print('If not jump:')
|
||||
x = forth.stack.pop()
|
||||
# print("==>value:", x)
|
||||
# print('==>value:', x)
|
||||
if not x:
|
||||
# print("==>", x, " is false")
|
||||
# print("==>returning", n)
|
||||
# print('==>', x, ' is false')
|
||||
# print('==>returning', n)
|
||||
return n
|
||||
# print("==>returning 1")
|
||||
# print('==>returning 1')
|
||||
return 1
|
||||
return ifnot_jump
|
||||
|
||||
|
@ -80,15 +80,15 @@ def w_import(f):
|
|||
|
||||
def w_px(f):
|
||||
args = f.stack.pop()
|
||||
print("args", args)
|
||||
#print('args', args)
|
||||
name = f.stack.pop()
|
||||
print("name", name)
|
||||
#print('name', name)
|
||||
m = f.stack.pop()
|
||||
print("mod:", m)
|
||||
#print('mod:', m)
|
||||
func = m.__dict__[name]
|
||||
print("f:", f);
|
||||
#print('f:', f);
|
||||
result = func(*args)
|
||||
print("result", result)
|
||||
#print('result', result)
|
||||
f.stack.push(result)
|
||||
return 1
|
||||
|
||||
|
@ -97,7 +97,7 @@ def w_list(f):
|
|||
l = []
|
||||
for i in range(n):
|
||||
l.append(f.stack.pop())
|
||||
print(l)
|
||||
#print(l)
|
||||
f.stack.push(l)
|
||||
return 1
|
||||
|
||||
|
@ -130,7 +130,7 @@ def w_endmap(f): # }
|
|||
l.append(x)
|
||||
x = f.stack.pop()
|
||||
if (len(l) % 2) != 0:
|
||||
print("Maps need even number of entries.")
|
||||
print('Maps need even number of entries.')
|
||||
return 1
|
||||
l.reverse()
|
||||
result = {}
|
||||
|
@ -157,7 +157,7 @@ def w_def(f):
|
|||
value = f.stack.pop()
|
||||
name = f.stack.pop()
|
||||
f.defvar(name, value)
|
||||
print('name', name, 'value', value)
|
||||
#print('name', name, 'value', value)
|
||||
return 1
|
||||
|
||||
def w_gt(f):
|
||||
|
@ -265,11 +265,11 @@ def w_semi(forth):
|
|||
w_semi.__dict__['immediate'] = True
|
||||
|
||||
def w_should_not_happen(forth):
|
||||
print("Should not execute this word!")
|
||||
print('Should not execute this word!')
|
||||
raise ValueError
|
||||
|
||||
def w_if(forth):
|
||||
print("w_if")
|
||||
#print('w_if')
|
||||
compiler = forth.compiler
|
||||
compiler.push_offset()
|
||||
compiler.push_offset()
|
||||
|
@ -306,7 +306,7 @@ def w_else(forth):
|
|||
w_else.__dict__['immediate'] = True
|
||||
|
||||
def w_do(forth):
|
||||
print("w_do")
|
||||
#print('w_do')
|
||||
compiler = forth.compiler
|
||||
compiler.push_offset()
|
||||
compiler.add_instruction(w_should_not_happen)
|
||||
|
@ -336,7 +336,7 @@ def w_until(forth):
|
|||
begin_offset = compiler.pop_offset()
|
||||
until_offset = compiler.offset()
|
||||
delta = begin_offset - until_offset
|
||||
print("Delta:", delta)
|
||||
#print('Delta:', delta)
|
||||
compiler.instructions.append(ifnot_jump_f(delta))
|
||||
return 1
|
||||
|
||||
|
@ -355,6 +355,5 @@ def w_idump(f):
|
|||
w_idump.__dict__['immediate'] = True
|
||||
|
||||
def w_stack(f):
|
||||
print(f.stack)
|
||||
print(f'Stack: <B[{f.stack}]T>')
|
||||
return 1
|
||||
|
||||
|
|
Loading…
Reference in a new issue