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

29 lines
773 B
Python

from emis_funky_funktions import *
from typing import *
from silly_thing import *
from pattern import lex_and_parse_pattern
from ir import Function, Application, Int, Variable, LetBinding, Unit
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 Unit()
raise Exception('Unreachable')
else:
return Int(j)