add draft for load arg handling

This commit is contained in:
Luna 2019-06-06 23:55:59 -03:00
parent 92a3076b9b
commit 38e0a1a961
6 changed files with 31 additions and 8 deletions

View file

@ -1,2 +1,2 @@
load :1; load :0;
quicksave; quicksave;

View file

@ -1,3 +1,3 @@
load :1; load :0;
echo 3 0 1 0.2; echo 3 0 1 0.2;
quicksave; quicksave;

2
scritcher/error.py Normal file
View file

@ -0,0 +1,2 @@
class InterpreterError(Exception):
pass

View file

@ -1,18 +1,20 @@
import sys
import shlex import shlex
from .utils import load_file_path
from .error import InterpreterError
class InterpreterError(Exception):
pass
class Interpreter: class Interpreter:
"""Interpreter for scritcher instructions/statements.""" """Interpreter for scritcher instructions/statements."""
def __init__(self): def __init__(self):
pass self.loaded_path = None
def _cmd_noop(self): def _cmd_noop(self):
pass pass
def _cmd_load(self, loadpath: str):
path = load_file_path(loadpath)
def run(self, line: str): def run(self, line: str):
"""Run a single line.""" """Run a single line."""
print(f'running {line!r}') print(f'running {line!r}')
@ -20,6 +22,9 @@ class Interpreter:
args = shlex.split(line) args = shlex.split(line)
command = args[0] command = args[0]
if command != 'load' and self.loaded_path is None:
print('warn: no file loaded.')
try: try:
method = getattr(self, f'_cmd_{command}') method = getattr(self, f'_cmd_{command}')
except AttributeError: except AttributeError:

View file

@ -1,7 +1,8 @@
import sys import sys
from pathlib import Path from pathlib import Path
from .executer import Interpreter, InterpreterError from .executer import Interpreter
from .error import InterpreterError
def main(): def main():
try: try:

15
scritcher/utils.py Normal file
View file

@ -0,0 +1,15 @@
import sys
from pathlib import Path
from .error import InterpreterError
def load_file_path(arg: str) -> Path:
"""load given argument."""
try:
index = int(arg.split(':')[1])
except ValueError:
raise InterpreterError('Invalid argument index')
except IndexError:
return Path(arg).resolve()
return Path(sys.argv[2 + index])