Reorganize code to make it a bit more modular.

This commit is contained in:
Russ Olsen 2020-04-17 13:42:41 -04:00
parent 6925755432
commit 8785b97f03

View file

@ -1,22 +1,13 @@
import os import os
import sys import sys
import atexit
from kernel import Forth from kernel import Forth
from lex import tokenize from lex import tokenize
import readline import readline
HistoryFile=".sallyforth" HistoryFile=".sallyforth"
histfile = os.path.join(os.path.expanduser("~"), HistoryFile) hist_file = 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()
class Completer: class Completer:
def __init__(self, f): def __init__(self, f):
@ -28,35 +19,53 @@ class Completer:
except IndexError: except IndexError:
return None return None
completer = Completer(f) def setup_readline(history_path, f):
completer = Completer(f)
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
f.defvar("argv", sys.argv[1::])
if os.path.exists(startup_file):
f.execute_file(startup_file)
while True:
p = f.evaluate_token('*prompt*')
try: try:
line = input(p) readline.read_history_file(history_path)
except KeyboardInterrupt: except FileNotFoundError:
print("<<interrupt>>") pass
line = '' readline.parse_and_bind("tab: complete")
except EOFError: readline.set_completer(completer.complete)
break def save_history():
readline.write_history_file(history_path)
atexit.register(save_history)
tokens = tokenize(line) def setup_forth():
try: f = Forth()
f.execute_tokens(tokens) f.defvar("argv", sys.argv[1::])
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
print("Error:", exc_type)
print("Error:", exc_value)
print("Error:", exc_traceback)
readline.write_history_file(histfile) source_dir = os.path.dirname(os.path.abspath(__file__))
startup_file = f'{source_dir}/startup.sf'
print("Bye!") if os.path.exists(startup_file):
f.execute_file(startup_file)
return f
def repl(f):
while True:
p = f.evaluate_token('*prompt*')
try:
line = input(p)
except KeyboardInterrupt:
print("<<interrupt>>")
line = ''
except EOFError:
break
tokens = tokenize(line)
try:
f.execute_tokens(tokens)
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
print("Error:", exc_type)
print("Error:", exc_value)
print("Error:", exc_traceback)
if __name__ == "__main__":
f = setup_forth()
setup_readline(hist_file, f)
repl(f)
print("Bye!")