mirror of
https://github.com/russolsen/sallyforth
synced 2024-12-26 21:58:32 +01:00
Rationalize native code import.
This commit is contained in:
parent
a4e2a1997a
commit
e1b1c4ee24
3 changed files with 29 additions and 19 deletions
|
@ -31,6 +31,5 @@
|
||||||
: rrest (list -- rest-of-rest) rest rest ;
|
: rrest (list -- rest-of-rest) rest rest ;
|
||||||
: rrrest (list -- all-but-first) 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 ;
|
: ffirst (list -- first-of-first) first first ;
|
||||||
: fffirst (list -- fff-irst) first first first ;
|
: fffirst (list -- fff-irst) first first first ;
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
"os" require
|
"os" require
|
||||||
"os.path" require
|
"os.path" require
|
||||||
"io" require
|
"io" require
|
||||||
|
"time" require
|
||||||
|
|
||||||
\ Basic aliases
|
\ Basic aliases
|
||||||
|
|
||||||
|
@ -34,18 +35,32 @@
|
||||||
|
|
||||||
: *prompt* "Sally> " ;
|
: *prompt* "Sally> " ;
|
||||||
|
|
||||||
|
\ Make a list.
|
||||||
'list-marker unique def
|
'list-marker unique def
|
||||||
: [ list-marker ;
|
: [ list-marker ;
|
||||||
: ] list-marker [list] ;
|
: ] list-marker [list] ;
|
||||||
|
|
||||||
: [] ( -- <empty 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 unique def
|
||||||
: { map-marker ;
|
: { map-marker ;
|
||||||
: } map-marker [list] list->map ;
|
: } map-marker [list] list->map ;
|
||||||
|
|
||||||
: {} ( -- <empty map>) { } ;
|
: {} ( -- <empty map>) { } ;
|
||||||
|
|
||||||
|
\ Make a set.
|
||||||
|
|
||||||
'set-marker unique def
|
'set-marker unique def
|
||||||
: {{ set-marker ;
|
: {{ set-marker ;
|
||||||
: }}
|
: }}
|
||||||
|
@ -59,17 +74,14 @@
|
||||||
: [: [ ;
|
: [: [ ;
|
||||||
: :] ] ->arglist ;
|
: :] ] ->arglist ;
|
||||||
|
|
||||||
: <. [ ;
|
|
||||||
: .> ] thread ;
|
|
||||||
|
|
||||||
: !!0 [] swap call ;
|
: type builtins.type !!1 ;
|
||||||
|
|
||||||
: type 1 ->list builtins.type !! ;
|
: ctime time.ctime !!0 ;
|
||||||
|
|
||||||
: ctime [] time.ctime ;
|
: sleep time.sleep !!1 drop ;
|
||||||
: sleep 1 ->list stack time.sleep drop ;
|
|
||||||
|
|
||||||
: callable? 1 ->list builtins.callable ;
|
: callable? builtins.callable !!1 ;
|
||||||
|
|
||||||
: hello "Hello" . nl ;
|
: hello "Hello" . nl ;
|
||||||
|
|
||||||
|
@ -88,14 +100,17 @@
|
||||||
: neg? 0 < ;
|
: neg? 0 < ;
|
||||||
: zero? 0 = ;
|
: zero? 0 = ;
|
||||||
|
|
||||||
|
: exists? os.path.exists !!1 ;
|
||||||
|
|
||||||
: source-if-exists
|
: source-if-exists
|
||||||
(path --)
|
(path -- result-of-sourcing)
|
||||||
dup
|
dup
|
||||||
1 ->list os.path.exists
|
exists?
|
||||||
if source else drop then
|
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 !! ;
|
: .!! (obj args method-name -- result) tbm getattr !! ;
|
||||||
|
|
||||||
"string.sf" source
|
"string.sf" source
|
||||||
|
|
|
@ -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]
|
names = [x for x in raw_names if x not in excludes]
|
||||||
for name in names:
|
for name in names:
|
||||||
localname = f'{alias}.{name}'
|
localname = f'{alias}.{name}'
|
||||||
#print(localname)
|
|
||||||
val = m.__getattribute__(name)
|
val = m.__getattribute__(name)
|
||||||
if isfunction(val) or isbuiltin(val):
|
|
||||||
forth.namespace[localname] = native_function_handler(val)
|
|
||||||
else:
|
|
||||||
forth.namespace[localname] = const_f(val)
|
forth.namespace[localname] = const_f(val)
|
||||||
|
|
||||||
def w_eval(f, i):
|
def w_eval(f, i):
|
||||||
|
@ -155,9 +151,9 @@ def w_import(f, i):
|
||||||
def w_call(f, i):
|
def w_call(f, i):
|
||||||
func = f.stack.pop()
|
func = f.stack.pop()
|
||||||
args = f.stack.pop()
|
args = f.stack.pop()
|
||||||
print('f', f, 'args', args)
|
# print('f', f, 'args', args)
|
||||||
result = func(*args)
|
result = func(*args)
|
||||||
print('result', result)
|
# print('result', result)
|
||||||
f.stack.push(result)
|
f.stack.push(result)
|
||||||
return i+1
|
return i+1
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue