From 627b49b418716bf9c739cc52393c938ff62d9ca0 Mon Sep 17 00:00:00 2001 From: Luna Date: Thu, 6 Jun 2019 23:28:15 -0300 Subject: [PATCH] add line-running and noop cmd --- examples/hello.scri | 2 +- scritcher/executer.py | 28 ++++++++++++++++++++++++++++ scritcher/main.py | 21 +++++++++++++++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 scritcher/executer.py diff --git a/examples/hello.scri b/examples/hello.scri index e804f19..3583c5a 100644 --- a/examples/hello.scri +++ b/examples/hello.scri @@ -1 +1 @@ -noop +noop; diff --git a/scritcher/executer.py b/scritcher/executer.py new file mode 100644 index 0000000..b1bd63b --- /dev/null +++ b/scritcher/executer.py @@ -0,0 +1,28 @@ +import shlex + + +class InterpreterError(Exception): + pass + + +class Interpreter: + """Interpreter for scritcher instructions/statements.""" + def __init__(self): + pass + + def _cmd_noop(self): + pass + + def run(self, line: str): + """Run a single line.""" + print(f'running {line!r}') + + args = shlex.split(line) + command = args[0] + + try: + method = getattr(self, f'_cmd_{command}') + except AttributeError: + raise InterpreterError(f'Command {command!r} not found') + + method(*args[1:]) diff --git a/scritcher/main.py b/scritcher/main.py index f62051a..96d1b6f 100644 --- a/scritcher/main.py +++ b/scritcher/main.py @@ -1,11 +1,28 @@ import sys from pathlib import Path +from .executer import Interpreter, InterpreterError + def main(): try: scri_path = Path(sys.argv[1]).resolve() except IndexError: print(f'usage: {sys.argv[0]} path/to/file.scri') return - print('path', scri_path) - #run(scri_path) + + full_text = scri_path.read_text() + full_text = full_text.replace('\n', '') + stmts = full_text.split(';') + + interp = Interpreter() + + try: + for stmt in stmts: + if not stmt: + continue + + interp.run(stmt) + except InterpreterError as err: + print(f'Interpreter error. {err.args[0]!r}') + + print('OK')