From 32e82e68e86d55de6c716e95eed51f85f181141b Mon Sep 17 00:00:00 2001 From: Stefan Wehr Date: Thu, 25 Apr 2024 15:41:29 +0200 Subject: [PATCH 1/3] parse json from scripts/main.py --- src/main.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main.py b/src/main.py index fdad06a..7b900f9 100644 --- a/src/main.py +++ b/src/main.py @@ -7,6 +7,7 @@ import common.log as log import common.constants as constants import parsers.lang_simple.simple_parser as simple_parser +import parsers.tinyJson.tinyJson_parser as tinyJson_parser import importlib import shell import sys @@ -17,7 +18,7 @@ def parseArgs(): parser = argparse.ArgumentParser(description=f'Run the compiler or interpreter for some language') - parser.add_argument('--lang', choices=['simple', 'var', 'loop', 'array', 'fun'], + parser.add_argument('--lang', choices=['simple', 'var', 'loop', 'array', 'fun', 'tinyJson'], help='The language (guessed from path of input file if not given)') parser.add_argument('--level', help='The loglevel (debug, info, warn)') subparsers = parser.add_subparsers(help='Commands', dest='cmd') @@ -141,11 +142,14 @@ def main(): if x.startswith('lang_'): lang = x[len('lang_'):] if lang is None: - utils.abort(f'Language not given with --lang and input file does not allow guessing '\ - 'the language.') - ast = importModule(lang, 'ast') + if args.input.endswith('.json'): + lang = 'tinyJson' + else: + utils.abort(f'Language not given with --lang and input file does not allow guessing '\ + 'the language.') match args.cmd: case "compile" | "run": + ast = importModule(lang, 'ast') if args.cmd == "run" and not args.output.endswith('.wasm'): utils.abort("For mode=run, output file must be a .wasm file") compilerMod = importModule(lang, 'compile') @@ -156,6 +160,7 @@ def main(): if args.cmd == "run": runWasm(args.run_wasm, args.output) case "interp": + ast = importModule(lang, 'ast') interpMod = importModule(lang, 'interp') interpFun = getFun(interpMod, 'interpModule') interpArgs = genericInterp.Args(args.input) @@ -167,7 +172,13 @@ def main(): args.alg, args.png, args.grammar) if lang == 'simple': simple_parser.parse(parserArgs) + elif lang == 'tinyJson': + f = args.input + x = tinyJson_parser.parse(utils.readTextFile(f)) + print(f'Successfully parsed {f} as json.') + print(x) else: + ast = importModule(lang, 'ast') parseMod = importModule(lang, 'parse') parseFun = getFun(parseMod, 'parseModule') genericParser.parseWithOwnParser(args.input, parserArgs, ast, parseFun) From b13ff7d3cb28f481e3fdc2ba1bcc2d7dd332defa Mon Sep 17 00:00:00 2001 From: Stefan Wehr Date: Thu, 25 Apr 2024 15:46:19 +0200 Subject: [PATCH 2/3] fix --- src/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 7b900f9..a4a74f8 100644 --- a/src/main.py +++ b/src/main.py @@ -7,7 +7,6 @@ import common.log as log import common.constants as constants import parsers.lang_simple.simple_parser as simple_parser -import parsers.tinyJson.tinyJson_parser as tinyJson_parser import importlib import shell import sys @@ -173,6 +172,7 @@ def main(): if lang == 'simple': simple_parser.parse(parserArgs) elif lang == 'tinyJson': + tinyJson_parser = utils.importModuleNotInStudent('parsers.tinyJson.tinyJson_parser') f = args.input x = tinyJson_parser.parse(utils.readTextFile(f)) print(f'Successfully parsed {f} as json.') From d59594f63ecb44d210bf6d38f5bc43ad4cffbc7e Mon Sep 17 00:00:00 2001 From: Stefan Wehr Date: Thu, 25 Apr 2024 15:48:05 +0200 Subject: [PATCH 3/3] fix --- src/common/utils.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/common/utils.py b/src/common/utils.py index b5f5c03..f973185 100644 --- a/src/common/utils.py +++ b/src/common/utils.py @@ -88,8 +88,7 @@ def importModuleNotInStudent(modName: str) -> Any: m = importlib.import_module(modName) return m except ImportError as e: - e.add_note(f'Could not import {modName}. Are in the student repo? '\ - 'Then deactivate the test triggering this error.') + e.add_note(f'Could not import {modName}. Are in the student repo?') raise e def splitIf[T](l: list[T], pred: Callable[[T], bool],