Rationalize native code import.

This commit is contained in:
Russ Olsen 2020-04-24 09:10:18 -04:00
parent a4e2a1997a
commit e1b1c4ee24
3 changed files with 29 additions and 19 deletions

View file

@ -31,6 +31,5 @@
: rrest (list -- rest-of-rest) rest rest ;
: rrrest (list -- all-but-first) rest rest rest ;
: first (list -- first-item) 1 swap drop ;
: ffirst (list -- first-of-first) first first ;
: fffirst (list -- fff-irst) first first first ;

View file

@ -9,6 +9,7 @@
"os" require
"os.path" require
"io" require
"time" require
\ Basic aliases
@ -34,18 +35,32 @@
: *prompt* "Sally> " ;
\ Make a list.
'list-marker unique def
: [ list-marker ;
: ] list-marker [list] ;
: [] ( -- <empty list>) [ ] ;
\ Look up attributes on a value.
: <. [ ;
: .> ] thread ;
\ Call native functions with various # arguments.
: !!0 [] swap !! ;
: !!1 swap 1 ->list swap !! ;
: !!2 swap 2 ->list swap !! ;
\ Make a map.
'map-marker unique def
: { map-marker ;
: } map-marker [list] list->map ;
: {} ( -- <empty map>) { } ;
\ Make a set.
'set-marker unique def
: {{ set-marker ;
: }}
@ -59,17 +74,14 @@
: [: [ ;
: :] ] ->arglist ;
: <. [ ;
: .> ] thread ;
: !!0 [] swap call ;
: type builtins.type !!1 ;
: type 1 ->list builtins.type !! ;
: ctime time.ctime !!0 ;
: ctime [] time.ctime ;
: sleep 1 ->list stack time.sleep drop ;
: sleep time.sleep !!1 drop ;
: callable? 1 ->list builtins.callable ;
: callable? builtins.callable !!1 ;
: hello "Hello" . nl ;
@ -88,14 +100,17 @@
: neg? 0 < ;
: zero? 0 = ;
: exists? os.path.exists !!1 ;
: source-if-exists
(path --)
(path -- result-of-sourcing)
dup
1 ->list os.path.exists
exists?
if source else drop then
;
: getattr (x 'fieldname -- field) swap 2 ->list builtins.getattr ;
: getattr ( obj attr -- attr-value ) swap 2 ->list builtins.getattr !! ;
: .!! (obj args method-name -- result) tbm getattr !! ;
"string.sf" source

View file

@ -32,11 +32,7 @@ def import_native_module(forth, m, alias=None, excludes=[]):
names = [x for x in raw_names if x not in excludes]
for name in names:
localname = f'{alias}.{name}'
#print(localname)
val = m.__getattribute__(name)
if isfunction(val) or isbuiltin(val):
forth.namespace[localname] = native_function_handler(val)
else:
forth.namespace[localname] = const_f(val)
def w_eval(f, i):
@ -155,9 +151,9 @@ def w_import(f, i):
def w_call(f, i):
func = f.stack.pop()
args = f.stack.pop()
print('f', f, 'args', args)
# print('f', f, 'args', args)
result = func(*args)
print('result', result)
# print('result', result)
f.stack.push(result)
return i+1