JSON-Lang/lab2.py
2024-03-15 20:21:40 -04:00

36 lines
903 B
Python

from emis_funky_funktions import *
from typing import Sequence
from grammar import lex_and_parse
from lex import Lexeme
from resolution import derives_false
def main(args: Sequence[str]) -> Tuple[str, int]:
match args[1:]:
case [file]:
with open(file) as input:
match lex_and_parse(input.read()):
case Ok(Ok(ir_kb)):
return (
'no'
if derives_false(ir_kb) else
'yes',
0
)
case Ok(Err(gen_ir_err)):
return (str(gen_ir_err), 102)
case Err((Lexeme(token, text, line, col_start, col_end), expected)):
return (
f'Parse error! Line {line}:{col_start}-{col_end}\n\nGot: {repr(text)}\nExpected: {expected}',
101
)
case _:
return (f'Usage: python {args[0]} some_file.cnf', 100)
raise Exception('Unreachable')
if __name__ == '__main__':
from sys import argv
out, code = main(argv)
print(out)
exit(code)