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;

View file

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

View file

@ -1,7 +1,8 @@
import sys
from pathlib import Path
from .executer import Interpreter, InterpreterError
from .executer import Interpreter
from .error import InterpreterError
def main():
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])