mirror of
https://github.com/russolsen/sallyforth
synced 2024-12-25 21:58:18 +01:00
Added start up file execution, configurable prompt.
This commit is contained in:
parent
4886e2de2a
commit
c5c8cdfff2
3 changed files with 37 additions and 2 deletions
|
@ -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)
|
||||
|
|
10
sallyforth/startup.sf
Normal file
10
sallyforth/startup.sf
Normal file
|
@ -0,0 +1,10 @@
|
|||
"Executing " . *source* . nl
|
||||
|
||||
: prompt "Yo> " ;
|
||||
|
||||
: hello (simple greeting) "Hello" . nl ;
|
||||
|
||||
: >0 0 > ;
|
||||
: <0 0 < ;
|
||||
|
||||
: p dup . nl ;
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue