JSON-Lang/genir.py

24 lines
633 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
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):
return Application([json_to_ir(e) for e in j])
else:
return Int(j)