2023-03-08 13:35:21 +00:00
|
|
|
from emis_funky_funktions import *
|
|
|
|
from typing import *
|
|
|
|
|
|
|
|
from pattern import lex_and_parse_pattern
|
2023-03-08 16:50:03 +00:00
|
|
|
from ir import Expression, Function, Application, Int, Variable, LetBinding, ReplHole
|
2023-03-08 13:35:21 +00:00
|
|
|
|
|
|
|
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):
|
2023-03-08 16:03:36 +00:00
|
|
|
match j:
|
|
|
|
case [first, *rest]:
|
|
|
|
return Application(json_to_ir(first), [json_to_ir(a) for a in rest])
|
|
|
|
case []:
|
2023-03-08 16:50:03 +00:00
|
|
|
return ReplHole()
|
2023-03-08 16:03:36 +00:00
|
|
|
raise Exception('Unreachable')
|
2023-03-08 13:35:21 +00:00
|
|
|
else:
|
|
|
|
return Int(j)
|