diff --git a/sallyforth/kernel.py b/sallyforth/kernel.py index 4340b08..7e7be81 100644 --- a/sallyforth/kernel.py +++ b/sallyforth/kernel.py @@ -26,7 +26,7 @@ class Forth: '*source*': const_f(__file__), 'true': const_f(True), 'false': const_f(False), - 'nil': const_f(None), + 'None': const_f(None), '0': const_f(0), '1': const_f(1), '2': const_f(2)} @@ -81,12 +81,16 @@ class Forth: self.compile_token(token) def macro_expand_token(self, token): - if len(token) <= 0: - return [token] - if token[0] != '#': + if len(token) <= 0 or token[0] != '#': return [token] + tag = token[1:] - return self.py_evaluate('macroexpand', tag) + parts = tag.split('.') + result = [ '<.', parts[0] ] + for part in parts[1::]: + result.append("'" + part) + result.append('.>') + return result def macro_expand_tokens(self, tokens): results = [] @@ -101,10 +105,10 @@ class Forth: raise ValueError(f'No such namespace: {ns_name}') def make_namespace(self, ns_name, initial_defs={}, refers=[]): - print(f'New namespace {ns_name} {refers}') + #print(f'New namespace {ns_name} {refers}') result = Namespace(ns_name, initial_defs, refers) self.namespaces[ns_name] = result - print(f'Returning {result}') + #print(f'Returning {result}') return result def execute_file(self, fpath): diff --git a/sallyforth/lex.py b/sallyforth/lex.py index e9338e7..a2c9198 100644 --- a/sallyforth/lex.py +++ b/sallyforth/lex.py @@ -11,7 +11,6 @@ def is_space(ch): class Tokenizer: def __init__(self, forth): self.forth = forth - print("Tokenizer:", self.forth) def tokenize(self, s): raw_tokens = self.raw_tokenize(s) diff --git a/sallyforth/list.sf b/sallyforth/list.sf index 689b362..3e31b92 100644 --- a/sallyforth/list.sf +++ b/sallyforth/list.sf @@ -1,5 +1,3 @@ -"List" p - \ Index into the x'th item. : [x] (col key -- value) 1 ->list '__getitem__ .!! ; @@ -26,6 +24,10 @@ swap nil slice \ Make the n..None slice. [x] ; + +: repeat (n x -- list-of-x-repeated-n-times) + 1 ->list * +; : rest (list -- all-but-first) 1 swap drop ; : rrest (list -- rest-of-rest) rest rest ; diff --git a/sallyforth/namespace.py b/sallyforth/namespace.py index 13acbb0..2850b3e 100644 --- a/sallyforth/namespace.py +++ b/sallyforth/namespace.py @@ -1,9 +1,5 @@ class Namespace: def __init__(self, name, initial_contents={}, refers=[]): - print('name', name) - print('initial contents', initial_contents) - print('refers', refers) - print('===') self.name = name self.contents = initial_contents.copy() self.refers = refers.copy() diff --git a/sallyforth/stack.py b/sallyforth/stack.py index c7e654a..b68431b 100644 --- a/sallyforth/stack.py +++ b/sallyforth/stack.py @@ -19,9 +19,12 @@ class Stack: return result def __iter__(self): - for i in range(self.top, -1, -1): + for i in range(0, self.top+1): yield self.stack[i] + def empty(self): + return self.top == -1 + def peek(self): return self.stack[self.top] diff --git a/sallyforth/startup.sf b/sallyforth/startup.sf index 7f92667..908c74d 100644 --- a/sallyforth/startup.sf +++ b/sallyforth/startup.sf @@ -1,4 +1,4 @@ -"Executing " dot *source* dot nl +\ "Executing " dot *source* dot nl \ Pull in libs. @@ -13,6 +13,7 @@ \ Basic aliases +'None 'nil alias 'dot '. alias 'colon ': alias 'semi '; alias @@ -112,6 +113,10 @@ : getattr ( obj attr -- attr-value ) swap 2 ->list builtins.getattr !! ; : .!! (obj args method-name -- result) tbm getattr !! ; +: .!!0 (obj method-name -- result ) [] swap .!! ; +: .!!1 (obj arg method-name -- result ) swap 1 ->list swap .!! ; +: .!!2 (obj a1 a2 method-name -- result ) swap 2 ->list swap .!! ; +: .!!3 (obj a1 a2 a3 method-name -- result ) swap 3 ->list swap .!! ; "string.sf" source "list.sf" source diff --git a/sallyforth/string.sf b/sallyforth/string.sf index e8c445d..dd4271a 100644 --- a/sallyforth/string.sf +++ b/sallyforth/string.sf @@ -1,5 +1,3 @@ -"String" p - : split (delimit str -- tokens) 2 ->list <. builtins.str 'split .> !! ; : dot-split (str -- tokens) "." swap split ; diff --git a/sallyforth/words.py b/sallyforth/words.py index c1665c6..8479d62 100644 --- a/sallyforth/words.py +++ b/sallyforth/words.py @@ -44,9 +44,9 @@ def w_no_op(f, i): return i+1 def w_enlist(f, i): - print("Enlist!") + # print("Enlist!") x = f.stack.pop() - print("Popped", x) + # print("Popped", x) f.stack.push([x]) return i+1 @@ -313,7 +313,7 @@ def w_def(f, i): value = f.stack.pop() name = f.stack.pop() f.defvar(name, value) - print('name', name, 'value', value) + # print('name', name, 'value', value) return i+1 def w_gt(f, i): @@ -379,6 +379,13 @@ def w_dot(f, i): print(a, end='') return i+1 +def w_splat(f, i): + l = f.stack.pop() + l.reverse() + for x in l: + f.stack.push(x) + return i+1 + def w_dup(f, i): x = f.stack.peek() f.stack.push(x) @@ -569,8 +576,8 @@ def w_idump(f, i): w_idump.__dict__['immediate'] = True def w_stack(f, i): - print("::top::") + print("Stack:", end=' ') for x in f.stack: - print(f'{x}') - print("::bottom::") + print(f'{repr(x)}', end=' ') + print() return i+1