2019-06-07 02:55:59 +00:00
|
|
|
import sys
|
2019-06-07 02:28:15 +00:00
|
|
|
import shlex
|
2019-06-07 02:55:59 +00:00
|
|
|
from .utils import load_file_path
|
|
|
|
from .error import InterpreterError
|
2019-06-07 02:28:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Interpreter:
|
|
|
|
"""Interpreter for scritcher instructions/statements."""
|
|
|
|
def __init__(self):
|
2019-06-07 02:55:59 +00:00
|
|
|
self.loaded_path = None
|
2019-06-07 02:28:15 +00:00
|
|
|
|
|
|
|
def _cmd_noop(self):
|
|
|
|
pass
|
|
|
|
|
2019-06-07 02:55:59 +00:00
|
|
|
def _cmd_load(self, loadpath: str):
|
|
|
|
path = load_file_path(loadpath)
|
|
|
|
|
2019-06-07 02:28:15 +00:00
|
|
|
def run(self, line: str):
|
|
|
|
"""Run a single line."""
|
|
|
|
print(f'running {line!r}')
|
|
|
|
|
|
|
|
args = shlex.split(line)
|
|
|
|
command = args[0]
|
|
|
|
|
2019-06-07 02:55:59 +00:00
|
|
|
if command != 'load' and self.loaded_path is None:
|
|
|
|
print('warn: no file loaded.')
|
|
|
|
|
2019-06-07 02:28:15 +00:00
|
|
|
try:
|
|
|
|
method = getattr(self, f'_cmd_{command}')
|
|
|
|
except AttributeError:
|
|
|
|
raise InterpreterError(f'Command {command!r} not found')
|
|
|
|
|
|
|
|
method(*args[1:])
|