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