diff --git a/sallyforth/sallyforth.py b/sallyforth/sallyforth.py index c9fa480..6831574 100644 --- a/sallyforth/sallyforth.py +++ b/sallyforth/sallyforth.py @@ -1,6 +1,19 @@ +import os from kernel import Forth +from lex import tokenize + + +source_dir = os.path.dirname(os.path.abspath(__file__)) +startup_file = f'{source_dir}/startup.sf' +print(startup_file) f = Forth() -while True: - f.process_line() +if os.path.exists(startup_file): + f.execute_file(startup_file) + +while True: + p = f.evaluate_token('*prompt*') + line = input(p) + tokens = tokenize(line) + f.execute_tokens(tokens) diff --git a/sallyforth/startup.sf b/sallyforth/startup.sf new file mode 100644 index 0000000..e33feaa --- /dev/null +++ b/sallyforth/startup.sf @@ -0,0 +1,10 @@ +"Executing " . *source* . nl + +: prompt "Yo> " ; + +: hello (simple greeting) "Hello" . nl ; + +: >0 0 > ; +: <0 0 < ; + +: p dup . nl ; diff --git a/sallyforth/words.py b/sallyforth/words.py index 8426fe7..02a8ce3 100644 --- a/sallyforth/words.py +++ b/sallyforth/words.py @@ -1,4 +1,5 @@ from compiler import Compiler +import importlib def execute_f(name, instructions): # print("execute_f:", len(instructions)) @@ -32,6 +33,17 @@ def const_f(value): return 1 return x +def w_import(f): + name = f.stack.pop() + m = importlib.import_module(name) + f.stack.push(m) + +def w_def(f): + value = f.stack.pop() + name = f.stack.pop() + f.defvar(name, value) + print('name', name, 'value', value) + def w_gt(f): a = f.stack.pop() b = f.stack.pop()