mirror of
https://github.com/russolsen/sallyforth
synced 2024-12-25 21:58:18 +01:00
Reorganize code to make it a bit more modular.
This commit is contained in:
parent
6925755432
commit
8785b97f03
1 changed files with 48 additions and 39 deletions
|
@ -1,22 +1,13 @@
|
|||
import os
|
||||
import sys
|
||||
import atexit
|
||||
from kernel import Forth
|
||||
from lex import tokenize
|
||||
import readline
|
||||
|
||||
HistoryFile=".sallyforth"
|
||||
|
||||
histfile = os.path.join(os.path.expanduser("~"), HistoryFile)
|
||||
|
||||
try:
|
||||
readline.read_history_file(histfile)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
source_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
startup_file = f'{source_dir}/startup.sf'
|
||||
|
||||
f = Forth()
|
||||
hist_file = os.path.join(os.path.expanduser("~"), HistoryFile)
|
||||
|
||||
class Completer:
|
||||
def __init__(self, f):
|
||||
|
@ -28,16 +19,31 @@ class Completer:
|
|||
except IndexError:
|
||||
return None
|
||||
|
||||
def setup_readline(history_path, f):
|
||||
completer = Completer(f)
|
||||
|
||||
try:
|
||||
readline.read_history_file(history_path)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
readline.parse_and_bind("tab: complete")
|
||||
readline.set_completer(completer.complete)
|
||||
def save_history():
|
||||
readline.write_history_file(history_path)
|
||||
atexit.register(save_history)
|
||||
|
||||
def setup_forth():
|
||||
f = Forth()
|
||||
f.defvar("argv", sys.argv[1::])
|
||||
|
||||
source_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
startup_file = f'{source_dir}/startup.sf'
|
||||
|
||||
if os.path.exists(startup_file):
|
||||
f.execute_file(startup_file)
|
||||
|
||||
return f
|
||||
|
||||
def repl(f):
|
||||
while True:
|
||||
p = f.evaluate_token('*prompt*')
|
||||
try:
|
||||
|
@ -57,6 +63,9 @@ while True:
|
|||
print("Error:", exc_value)
|
||||
print("Error:", exc_traceback)
|
||||
|
||||
readline.write_history_file(histfile)
|
||||
|
||||
if __name__ == "__main__":
|
||||
f = setup_forth()
|
||||
setup_readline(hist_file, f)
|
||||
repl(f)
|
||||
print("Bye!")
|
||||
|
|
Loading…
Reference in a new issue