diff --git a/sallyforth/list.sf b/sallyforth/list.sf index 3b2c313..689b362 100644 --- a/sallyforth/list.sf +++ b/sallyforth/list.sf @@ -23,7 +23,7 @@ ; : drop (n list -- all-but-first-n-items) - swap nil slice \ Make the n..None slice. + swap nil slice \ Make the n..None slice. [x] ; @@ -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 ; diff --git a/sallyforth/startup.sf b/sallyforth/startup.sf index 775df3d..7f92667 100644 --- a/sallyforth/startup.sf +++ b/sallyforth/startup.sf @@ -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] ; : [] ( -- ) [ ] ; + +\ 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 ; : {} ( -- ) { } ; +\ 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 diff --git a/sallyforth/words.py b/sallyforth/words.py index e80a0a9..c1665c6 100644 --- a/sallyforth/words.py +++ b/sallyforth/words.py @@ -32,12 +32,8 @@ 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) + forth.namespace[localname] = const_f(val) def w_eval(f, i): token = f.stack.pop() @@ -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