mirror of
https://github.com/russolsen/sallyforth
synced 2024-12-25 21:58:18 +01:00
Clean up argument names, debug printing.
This commit is contained in:
parent
3fb272156c
commit
a1669f996c
6 changed files with 172 additions and 176 deletions
|
@ -26,21 +26,21 @@ def native(forth):
|
|||
forth.set(name, wrapped_f)
|
||||
|
||||
@word('raise')
|
||||
def w_raise(f):
|
||||
ex = f.stack.pop()
|
||||
def w_raise(forth):
|
||||
ex = forth.stack.pop()
|
||||
raise ex
|
||||
|
||||
@word(immediate=True)
|
||||
def readtoken(f):
|
||||
t = f.stream.get_token()
|
||||
def readtoken(forth):
|
||||
t = forth.stream.get_token()
|
||||
def push_token(xforth):
|
||||
xforth.stack.push(t)
|
||||
return push_token
|
||||
|
||||
@word("!!")
|
||||
def w_call(f):
|
||||
func = f.stack.pop()
|
||||
args = f.stack.pop()
|
||||
def w_call(forth):
|
||||
func = forth.stack.pop()
|
||||
args = forth.stack.pop()
|
||||
#print('f', f, 'args', args)
|
||||
try:
|
||||
result = func(*args)
|
||||
|
@ -48,93 +48,93 @@ def w_call(f):
|
|||
print(f'Error executing {func}({args})')
|
||||
raise
|
||||
#print('result', result)
|
||||
f.stack.push(result)
|
||||
forth.stack.push(result)
|
||||
|
||||
@word()
|
||||
def unique(f):
|
||||
f.stack.push(Unique())
|
||||
def unique(forth):
|
||||
forth.stack.push(Unique())
|
||||
|
||||
@word()
|
||||
def load(f):
|
||||
name = f.stack.pop()
|
||||
def load(forth):
|
||||
name = forth.stack.pop()
|
||||
m = importlib.import_module(name)
|
||||
f.set_constant(name, m)
|
||||
forth.set_constant(name, m)
|
||||
|
||||
@word('import')
|
||||
def w_import(f):
|
||||
name = f.stack.pop()
|
||||
def w_import(forth):
|
||||
name = forth.stack.pop()
|
||||
m = importlib.import_module(name)
|
||||
f.ns.import_native_module(m)
|
||||
forth.ns.import_native_module(m)
|
||||
|
||||
@word()
|
||||
def lexicon(f):
|
||||
name = f.stack.pop()
|
||||
f.ns.import_from_module(name)
|
||||
def lexicon(forth):
|
||||
name = forth.stack.pop()
|
||||
forth.ns.import_from_module(name)
|
||||
|
||||
@word('source')
|
||||
def w_source(f):
|
||||
path = f.stack.pop()
|
||||
f.eval_file(path)
|
||||
def w_source(forth):
|
||||
path = forth.stack.pop()
|
||||
forth.eval_file(path)
|
||||
|
||||
@word('alias')
|
||||
def w_alias(f):
|
||||
new_name = f.stack.pop()
|
||||
old_name = f.stack.pop()
|
||||
f.alias(new_name, old_name)
|
||||
def w_alias(forth):
|
||||
new_name = forth.stack.pop()
|
||||
old_name = forth.stack.pop()
|
||||
forth.alias(new_name, old_name)
|
||||
|
||||
@word()
|
||||
def rawdef(f):
|
||||
name = f.stack.pop()
|
||||
value = f.stack.pop()
|
||||
f.set(name, value)
|
||||
def rawdef(forth):
|
||||
name = forth.stack.pop()
|
||||
value = forth.stack.pop()
|
||||
forth.set(name, value)
|
||||
|
||||
@word("=!")
|
||||
def equal_bang(f):
|
||||
name = f.stack.pop()
|
||||
value = f.stack.pop()
|
||||
f.set_constant(name, value)
|
||||
def equal_bang(forth):
|
||||
name = forth.stack.pop()
|
||||
value = forth.stack.pop()
|
||||
forth.set_constant(name, value)
|
||||
|
||||
@word("*prompt*")
|
||||
def promptword(f):
|
||||
f.stack.push(">> ")
|
||||
def promptword(forth):
|
||||
forth.stack.push(">> ")
|
||||
|
||||
@word()
|
||||
def lookup(f):
|
||||
name = f.stack.pop()
|
||||
f.stack.push(f.ns[name])
|
||||
def lookup(forth):
|
||||
name = forth.stack.pop()
|
||||
forth.stack.push(forth.ns[name])
|
||||
|
||||
@word()
|
||||
def forget(f):
|
||||
name = f.stack.pop()
|
||||
del f.ns[name]
|
||||
def forget(forth):
|
||||
name = forth.stack.pop()
|
||||
del forth.ns[name]
|
||||
|
||||
@word()
|
||||
def p(f):
|
||||
print(f.stack.pop())
|
||||
def p(forth):
|
||||
print(forth.stack.pop())
|
||||
|
||||
@word()
|
||||
def nl(f):
|
||||
def nl(forth):
|
||||
print()
|
||||
|
||||
@word('.')
|
||||
def dot(f):
|
||||
print(f.stack.pop(), end='')
|
||||
def dot(forth):
|
||||
print(forth.stack.pop(), end='')
|
||||
|
||||
@word()
|
||||
def splat(f):
|
||||
l = f.stack.pop()
|
||||
def splat(forth):
|
||||
l = forth.stack.pop()
|
||||
l.reverse()
|
||||
for x in l:
|
||||
f.stack.push(x)
|
||||
forth.stack.push(x)
|
||||
|
||||
@word()
|
||||
def stack(f):
|
||||
print(f.stack)
|
||||
def stack(forth):
|
||||
print(forth.stack)
|
||||
|
||||
@word()
|
||||
def ns(f):
|
||||
print(f.ns.name)
|
||||
print(f.ns.contents)
|
||||
def ns(forth):
|
||||
print(forth.ns.name)
|
||||
print(forth.ns.contents)
|
||||
|
||||
@word(':', True)
|
||||
def colon(forth):
|
||||
|
|
|
@ -38,7 +38,7 @@ def compile_token(forth, t):
|
|||
return f
|
||||
|
||||
def compile_value(contents, v):
|
||||
print("compiling", v, v.__dict__)
|
||||
#print("compiling", v, v.__dict__)
|
||||
if v.forth_inline and v.forth_contents:
|
||||
contents.extend(v.forth_contents)
|
||||
else:
|
||||
|
|
|
@ -2,72 +2,72 @@ from util import word, get_attribute
|
|||
from unique import Unique
|
||||
|
||||
@word('[list]')
|
||||
def w_bounded_list(f):
|
||||
def w_bounded_list(forth):
|
||||
"""Create a list from delimted values on the stack.
|
||||
[list]
|
||||
(marker a b c marker -- [a b c]
|
||||
"""
|
||||
marker = f.stack.pop()
|
||||
marker = forth.stack.pop()
|
||||
l = []
|
||||
if f.stack.empty():
|
||||
if forth.stack.empty():
|
||||
raise ValueError("Stack underflow")
|
||||
x = f.stack.pop()
|
||||
x = forth.stack.pop()
|
||||
while x != marker:
|
||||
l.append(x)
|
||||
if f.stack.empty():
|
||||
if forth.stack.empty():
|
||||
raise ValueError("Stack underflow")
|
||||
x = f.stack.pop()
|
||||
x = forth.stack.pop()
|
||||
l.reverse()
|
||||
f.stack.push(l)
|
||||
forth.stack.push(l)
|
||||
|
||||
@word('->list')
|
||||
def w_list(f):
|
||||
n = f.stack.pop()
|
||||
def w_list(forth):
|
||||
n = forth.stack.pop()
|
||||
l = []
|
||||
for i in range(n):
|
||||
l.append(f.stack.pop())
|
||||
f.stack.push(l)
|
||||
l.append(forth.stack.pop())
|
||||
forth.stack.push(l)
|
||||
|
||||
@word()
|
||||
def w_map(f):
|
||||
word = f.stack.pop()
|
||||
l = f.stack.pop()
|
||||
def w_map(forth):
|
||||
word = forth.stack.pop()
|
||||
l = forth.stack.pop()
|
||||
|
||||
word_f = f.lookup(word)
|
||||
word_f = forth.lookup(word)
|
||||
print(word_f)
|
||||
result = []
|
||||
|
||||
for item in l:
|
||||
f.stack.push(item)
|
||||
word_f(f)
|
||||
result.append(f.stack.pop())
|
||||
forth.stack.push(item)
|
||||
word_f(forth)
|
||||
result.append(forth.stack.pop())
|
||||
|
||||
f.stack.push(result)
|
||||
forth.stack.push(result)
|
||||
|
||||
@word()
|
||||
def w_reduce(f):
|
||||
l = f.stack.pop()
|
||||
word = f.stack.pop()
|
||||
def w_reduce(forth):
|
||||
l = forth.stack.pop()
|
||||
word = forth.stack.pop()
|
||||
print(f'L: {l} word {word}')
|
||||
word_f = f.lookup(word)
|
||||
word_f = forth.lookup(word)
|
||||
|
||||
if len(l) <= 0:
|
||||
f.stack.push(None)
|
||||
forth.stack.push(None)
|
||||
elif len(l) == 1:
|
||||
f.stack.push(l[0])
|
||||
forth.stack.push(l[0])
|
||||
else:
|
||||
result = l[0]
|
||||
l = l[1::]
|
||||
for item in l:
|
||||
f.stack.push(result)
|
||||
f.stack.push(item)
|
||||
word_f(f)
|
||||
result = f.stack.pop()
|
||||
f.stack.push(result)
|
||||
forth.stack.push(result)
|
||||
forth.stack.push(item)
|
||||
word_f(forth)
|
||||
result = forth.stack.pop()
|
||||
forth.stack.push(result)
|
||||
|
||||
@word('@@')
|
||||
def w_thread(f):
|
||||
contents = f.stack.pop()
|
||||
def w_thread(forth):
|
||||
contents = forth.stack.pop()
|
||||
#print("Contents:", contents)
|
||||
result = contents[0]
|
||||
for field in contents[1::]:
|
||||
|
@ -76,26 +76,26 @@ def w_thread(f):
|
|||
result = getattr(result, field) # result.field
|
||||
else:
|
||||
result = result[field] # result[field]
|
||||
f.stack.push(result)
|
||||
forth.stack.push(result)
|
||||
|
||||
@word('list->map')
|
||||
def w_list_to_map(f): # list->map
|
||||
l = f.stack.pop()
|
||||
def w_list_to_map(forth): # list->map
|
||||
l = forth.stack.pop()
|
||||
result = {}
|
||||
for i in range(0, len(l), 2):
|
||||
result[l[i]] = l[i+1]
|
||||
f.stack.push(result)
|
||||
forth.stack.push(result)
|
||||
|
||||
@word()
|
||||
def w_get(f):
|
||||
name = f.stack.pop()
|
||||
m = f.stack.pop()
|
||||
def w_get(forth):
|
||||
name = forth.stack.pop()
|
||||
m = forth.stack.pop()
|
||||
result = m[name]
|
||||
f.stack.push(result)
|
||||
forth.stack.push(result)
|
||||
|
||||
@word()
|
||||
def w_getattribute(f):
|
||||
name = f.stack.pop()
|
||||
x = f.stack.pop()
|
||||
def w_getattribute(forth):
|
||||
name = forth.stack.pop()
|
||||
x = forth.stack.pop()
|
||||
result = x.__getattribute__(name)
|
||||
f.stack.push(result)
|
||||
forth.stack.push(result)
|
||||
|
|
|
@ -29,24 +29,20 @@ class Namespace:
|
|||
into this namespace. Removes the prefix.
|
||||
"""
|
||||
m = load_module(module_name)
|
||||
print(m)
|
||||
#print(m)
|
||||
names = dir(m)
|
||||
for name in names:
|
||||
value = getattr(m, name)
|
||||
print("IMP", name, value, '=>', getattr(value, 'ast', None))
|
||||
#print("IMP", name, value, '=>', getattr(value, 'ast', None))
|
||||
if get_attribute(value, 'forth_word'):
|
||||
forth_name = value.forth_name or name
|
||||
var = self.set(forth_name, value, False)
|
||||
#var.immediate = value.forth_immediate
|
||||
#print(var)
|
||||
#if var.immediate:
|
||||
# print(name, 'immediate')
|
||||
|
||||
def import_native_module(self, m, alias=None):
|
||||
if not alias:
|
||||
alias = m.__name__
|
||||
alias = alias.replace(".", "/")
|
||||
print(m, alias)
|
||||
#print(m, alias)
|
||||
|
||||
names = dir(m)
|
||||
for name in names:
|
||||
|
|
|
@ -3,13 +3,13 @@ import sys
|
|||
from util import word
|
||||
|
||||
@word('fork')
|
||||
def w_fork(f):
|
||||
parent_word = f.stack.pop()
|
||||
child_word = f.stack.pop()
|
||||
parent_f = f.namespace.get(parent_word, None).get_ivalue()
|
||||
child_f = f.namespace.get(child_word, None).get_ivalue()
|
||||
def w_fork(forth):
|
||||
parent_word = forth.stack.pop()
|
||||
child_word = forth.stack.pop()
|
||||
parent_f = forth.ns.get(parent_word, None).get_ivalue()
|
||||
child_f = forth.ns.get(child_word, None).get_ivalue()
|
||||
pid = os.fork()
|
||||
f.stack.push(pid)
|
||||
forth.stack.push(pid)
|
||||
if pid == 0:
|
||||
print("child:", pid)
|
||||
child_f(f, 0)
|
||||
|
@ -18,24 +18,24 @@ def w_fork(f):
|
|||
parent_f(f, 0)
|
||||
|
||||
@word('execvp')
|
||||
def w_execvp(f):
|
||||
args = f.stack.pop()
|
||||
def w_execvp(forth):
|
||||
args = forth.stack.pop()
|
||||
path = args[0]
|
||||
print(f"path {path} args: {args}")
|
||||
os.execvp(path, args)
|
||||
|
||||
@word('waitpid')
|
||||
def w_waitpid(f):
|
||||
pid = f.stack.pop()
|
||||
def w_waitpid(forth):
|
||||
pid = forth.stack.pop()
|
||||
result = os.waitpid(pid, 0)
|
||||
f.stack.push(result)
|
||||
forth.stack.push(result)
|
||||
|
||||
@word('exit')
|
||||
def w_exit(f):
|
||||
n = f.stack.pop()
|
||||
def w_exit(forth):
|
||||
n = forth.stack.pop()
|
||||
sys.exit(n)
|
||||
|
||||
@word('exit!')
|
||||
def w_exit_bang(f):
|
||||
n = f.stack.pop()
|
||||
def w_exit_bang(forth):
|
||||
n = forth.stack.pop()
|
||||
os._exit(n)
|
||||
|
|
|
@ -18,78 +18,78 @@ def dup(forth):
|
|||
forth.stack.push(a)
|
||||
|
||||
@word()
|
||||
def swap(f):
|
||||
a = f.stack.pop()
|
||||
b = f.stack.pop()
|
||||
f.stack.push(a)
|
||||
f.stack.push(b)
|
||||
def swap(forth):
|
||||
a = forth.stack.pop()
|
||||
b = forth.stack.pop()
|
||||
forth.stack.push(a)
|
||||
forth.stack.push(b)
|
||||
|
||||
@word()
|
||||
def t(f):
|
||||
dup(f)
|
||||
def t(forth):
|
||||
dup(forth)
|
||||
|
||||
@word()
|
||||
def m(f):
|
||||
f.stack.push(f.stack[-2])
|
||||
def m(forth):
|
||||
forth.stack.push(forth.stack[-2])
|
||||
|
||||
@word()
|
||||
def b(f):
|
||||
f.stack.push(f.stack[-3])
|
||||
def b(forth):
|
||||
forth.stack.push(forth.stack[-3])
|
||||
|
||||
@word()
|
||||
def tmb(f): # A noop
|
||||
def tmb(forth): # A noop
|
||||
pass
|
||||
|
||||
@word()
|
||||
def tbm(f):
|
||||
t = f.stack.pop()
|
||||
m = f.stack.pop()
|
||||
b = f.stack.pop()
|
||||
f.stack.push(m)
|
||||
f.stack.push(b)
|
||||
f.stack.push(t)
|
||||
def tbm(forth):
|
||||
t = forth.stack.pop()
|
||||
m = forth.stack.pop()
|
||||
b = forth.stack.pop()
|
||||
forth.stack.push(m)
|
||||
forth.stack.push(b)
|
||||
forth.stack.push(t)
|
||||
|
||||
@word()
|
||||
def bmt(f):
|
||||
t = f.stack.pop()
|
||||
m = f.stack.pop()
|
||||
b = f.stack.pop()
|
||||
f.stack.push(t)
|
||||
f.stack.push(m)
|
||||
f.stack.push(b)
|
||||
def bmt(forth):
|
||||
t = forth.stack.pop()
|
||||
m = forth.stack.pop()
|
||||
b = forth.stack.pop()
|
||||
forth.stack.push(t)
|
||||
forth.stack.push(m)
|
||||
forth.stack.push(b)
|
||||
|
||||
@word()
|
||||
def btm(f):
|
||||
t = f.stack.pop()
|
||||
m = f.stack.pop()
|
||||
b = f.stack.pop()
|
||||
f.stack.push(m)
|
||||
f.stack.push(t)
|
||||
f.stack.push(b)
|
||||
def btm(forth):
|
||||
t = forth.stack.pop()
|
||||
m = forth.stack.pop()
|
||||
b = forth.stack.pop()
|
||||
forth.stack.push(m)
|
||||
forth.stack.push(t)
|
||||
forth.stack.push(b)
|
||||
|
||||
@word()
|
||||
def mtb(f):
|
||||
t = f.stack.pop()
|
||||
m = f.stack.pop()
|
||||
b = f.stack.pop()
|
||||
f.stack.push(b)
|
||||
f.stack.push(t)
|
||||
f.stack.push(m)
|
||||
def mtb(forth):
|
||||
t = forth.stack.pop()
|
||||
m = forth.stack.pop()
|
||||
b = forth.stack.pop()
|
||||
forth.stack.push(b)
|
||||
forth.stack.push(t)
|
||||
forth.stack.push(m)
|
||||
|
||||
@word()
|
||||
def mbt(f):
|
||||
t = f.stack.pop()
|
||||
m = f.stack.pop()
|
||||
b = f.stack.pop()
|
||||
f.stack.push(t)
|
||||
f.stack.push(b)
|
||||
f.stack.push(m)
|
||||
def mbt(forth):
|
||||
t = forth.stack.pop()
|
||||
m = forth.stack.pop()
|
||||
b = forth.stack.pop()
|
||||
forth.stack.push(t)
|
||||
forth.stack.push(b)
|
||||
forth.stack.push(m)
|
||||
|
||||
@word()
|
||||
def rot(f):
|
||||
c = f.stack.pop()
|
||||
b = f.stack.pop()
|
||||
a = f.stack.pop()
|
||||
f.stack.push(b)
|
||||
f.stack.push(c)
|
||||
f.stack.push(a)
|
||||
def rot(forth):
|
||||
c = forth.stack.pop()
|
||||
b = forth.stack.pop()
|
||||
a = forth.stack.pop()
|
||||
forth.stack.push(b)
|
||||
forth.stack.push(c)
|
||||
forth.stack.push(a)
|
||||
|
|
Loading…
Reference in a new issue