Added nativeword word, which wraps a native function as a forth word.

This commit is contained in:
Russ Olsen 2020-05-11 12:55:15 -04:00
parent 9e9573b3dd
commit 9bad7b958f
6 changed files with 76 additions and 10 deletions

View file

@ -1,10 +1,23 @@
import tokenstream as ts import tokenstream as ts
from wrappers import noop from wrappers import noop
from util import word from util import word, native_word
from unique import Unique from unique import Unique
import importlib import importlib
from pprint import pprint from pprint import pprint
@word()
def native(forth):
has_return = forth.stack.pop()
n = forth.stack.pop()
native_f = forth.stack.pop()
name = forth.stack.pop()
print('has_return', has_return)
print('n', n)
print('native_f', native_f)
print('name', name)
wrapped_f = native_word(native_f, name, n, has_return)
forth.set(name, wrapped_f)
@word('raise') @word('raise')
def w_raise(f): def w_raise(f):
ex = f.stack.pop() ex = f.stack.pop()
@ -128,13 +141,8 @@ def colon(forth):
@word() @word()
def inline(forth): def inline(forth):
name = forth.stack.pop() name = forth.stack.pop()
print("Word name:", name)
var = forth.ns[name] var = forth.ns[name]
print('var', var)
print('value', var.value)
print('value dict', var.value.__dict__)
var.value.forth_inline = True var.value.forth_inline = True
print('moded value dict', var.value.__dict__)
@word() @word()
def current_stream(forth): def current_stream(forth):
@ -148,12 +156,15 @@ def debug(forth):
pprint(var) pprint(var)
pprint(var.value.__dict__) pprint(var.value.__dict__)
@word()
def fresult(forth, f): def fresult(forth, f):
f(forth) f(forth)
result = forth.stack.pop() result = forth.stack.pop()
return result return result
@word()
def compilenext(forth):
forth.stack.push(forth.compile_next())
@word('while', True) @word('while', True)
def w_while(forth): def w_while(forth):
cond = forth.compile_next() cond = forth.compile_next()

9
sallyforth/builtins.f Normal file
View file

@ -0,0 +1,9 @@
: doc { <. $? '__doc__ .> }
'power builtins/pow 2 true native
'abs builtins/abs 1 true native
'round builtins/round 1 true native
\ 'open builtins/open 2 true native

View file

@ -68,10 +68,10 @@ def w_reduce(f):
@word('@@') @word('@@')
def w_thread(f): def w_thread(f):
contents = f.stack.pop() contents = f.stack.pop()
print("Contents:", contents) #print("Contents:", contents)
result = contents[0] result = contents[0]
for field in contents[1::]: for field in contents[1::]:
print("Result:", result) #print("Result:", result)
if isinstance(field, str) and hasattr(result, field): if isinstance(field, str) and hasattr(result, field):
result = getattr(result, field) # result.field result = getattr(result, field) # result.field
else: else:

View file

@ -46,3 +46,10 @@
<. $? 'append .> !!1 <. $? 'append .> !!1
drop drop
} }
\ Execute a native function in a list.
: [! [
: !] { ] dup rest swap first !! }

View file

@ -65,7 +65,7 @@ class Namespace:
return self.contents.keys() return self.contents.keys()
def __contains__(self, key): def __contains__(self, key):
return self.contents.__contains(key) return self.contents.__contains__(key)
def __delattr__(self, key): def __delattr__(self, key):
return self.contents.__delattr__(key) return self.contents.__delattr__(key)

View file

@ -17,3 +17,42 @@ class word:
f.forth_immediate = self.immediate f.forth_immediate = self.immediate
return f return f
def wrap_native_f(f, n, hasreturn):
if n > 0 and hasreturn:
def wrapper(forth):
print("both")
args = []
for i in range(n):
args.append(forth.stack.pop())
result = f(*args)
forth.stack.push(result)
elif n > 0:
def wrapper(forth):
args = []
for i in range(n):
args.append(forth.stack.pop())
f(*args)
elif hasreturn:
def wrapper(forth):
forth.stack.push(f(*args))
else:
def wrapper(forth):
print("nothing")
f()
return wrapper
def determine_nargs(f, n):
if n != None:
return n
a = inspect.getargs(f.__code__)
return len(a.args)
def native_word(f, name=None, nargs=None, hasreturn=False):
nargs = determine_nargs(f, nargs)
f = wrap_native_f(f, nargs, hasreturn)
f.forth_type = 'wrapped_primitive'
f.forth_inline = False
f.forth_immediate = False
return f