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