JSON-Lang/genir.py
2024-03-15 20:21:42 -04:00

28 lines
767 B
Python

from emis_funky_funktions import *
from typing import *
from pattern import lex_and_parse_pattern
from ir import Expression, Function, Application, Int, Variable, LetBinding, ReplHole
import json
JsonType: TypeAlias = 'Mapping[str, JsonType] | Sequence[JsonType] | int | str'
def json_to_ir(j: JsonType) -> Expression:
if isinstance(j, Mapping):
return Function(tuple(
#TODO handle parse errors
(unwrap_r(lex_and_parse_pattern(k)), json_to_ir(v))
for (k, v) in j.items()
))
elif isinstance(j, str):
return Variable(j)
elif isinstance(j, Sequence):
match j:
case [first, *rest]:
return Application(json_to_ir(first), [json_to_ir(a) for a in rest])
case []:
return ReplHole()
raise Exception('Unreachable')
else:
return Int(j)