Added start up file execution, configurable prompt.

This commit is contained in:
Russ Olsen 2020-04-16 09:03:08 -04:00
parent 4886e2de2a
commit c5c8cdfff2
3 changed files with 37 additions and 2 deletions

View file

@ -1,6 +1,19 @@
import os
from kernel import Forth 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() 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)

10
sallyforth/startup.sf Normal file
View file

@ -0,0 +1,10 @@
"Executing " . *source* . nl
: prompt "Yo> " ;
: hello (simple greeting) "Hello" . nl ;
: >0 0 > ;
: <0 0 < ;
: p dup . nl ;

View file

@ -1,4 +1,5 @@
from compiler import Compiler from compiler import Compiler
import importlib
def execute_f(name, instructions): def execute_f(name, instructions):
# print("execute_f:", len(instructions)) # print("execute_f:", len(instructions))
@ -32,6 +33,17 @@ def const_f(value):
return 1 return 1
return x 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): def w_gt(f):
a = f.stack.pop() a = f.stack.pop()
b = f.stack.pop() b = f.stack.pop()