mirror of
https://github.com/russolsen/sallyforth
synced 2024-12-25 21:58:18 +01:00
Added nativeword word, which wraps a native function as a forth word.
This commit is contained in:
parent
9e9573b3dd
commit
9bad7b958f
6 changed files with 76 additions and 10 deletions
|
@ -1,10 +1,23 @@
|
|||
import tokenstream as ts
|
||||
from wrappers import noop
|
||||
from util import word
|
||||
from util import word, native_word
|
||||
from unique import Unique
|
||||
import importlib
|
||||
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')
|
||||
def w_raise(f):
|
||||
ex = f.stack.pop()
|
||||
|
@ -128,13 +141,8 @@ def colon(forth):
|
|||
@word()
|
||||
def inline(forth):
|
||||
name = forth.stack.pop()
|
||||
print("Word name:", name)
|
||||
var = forth.ns[name]
|
||||
print('var', var)
|
||||
print('value', var.value)
|
||||
print('value dict', var.value.__dict__)
|
||||
var.value.forth_inline = True
|
||||
print('moded value dict', var.value.__dict__)
|
||||
|
||||
@word()
|
||||
def current_stream(forth):
|
||||
|
@ -148,12 +156,15 @@ def debug(forth):
|
|||
pprint(var)
|
||||
pprint(var.value.__dict__)
|
||||
|
||||
@word()
|
||||
def fresult(forth, f):
|
||||
f(forth)
|
||||
result = forth.stack.pop()
|
||||
return result
|
||||
|
||||
@word()
|
||||
def compilenext(forth):
|
||||
forth.stack.push(forth.compile_next())
|
||||
|
||||
@word('while', True)
|
||||
def w_while(forth):
|
||||
cond = forth.compile_next()
|
||||
|
|
9
sallyforth/builtins.f
Normal file
9
sallyforth/builtins.f
Normal 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
|
||||
|
|
@ -68,10 +68,10 @@ def w_reduce(f):
|
|||
@word('@@')
|
||||
def w_thread(f):
|
||||
contents = f.stack.pop()
|
||||
print("Contents:", contents)
|
||||
#print("Contents:", contents)
|
||||
result = contents[0]
|
||||
for field in contents[1::]:
|
||||
print("Result:", result)
|
||||
#print("Result:", result)
|
||||
if isinstance(field, str) and hasattr(result, field):
|
||||
result = getattr(result, field) # result.field
|
||||
else:
|
||||
|
|
|
@ -46,3 +46,10 @@
|
|||
<. $? 'append .> !!1
|
||||
drop
|
||||
}
|
||||
|
||||
\ Execute a native function in a list.
|
||||
|
||||
: [! [
|
||||
: !] { ] dup rest swap first !! }
|
||||
|
||||
|
||||
|
|
|
@ -65,7 +65,7 @@ class Namespace:
|
|||
return self.contents.keys()
|
||||
|
||||
def __contains__(self, key):
|
||||
return self.contents.__contains(key)
|
||||
return self.contents.__contains__(key)
|
||||
|
||||
def __delattr__(self, key):
|
||||
return self.contents.__delattr__(key)
|
||||
|
|
|
@ -17,3 +17,42 @@ class word:
|
|||
f.forth_immediate = self.immediate
|
||||
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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue