Skip to content

Commit b7f36e8

Browse files
committed
Compile more
1 parent f3186a8 commit b7f36e8

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

scrapscript.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,19 +1183,18 @@ def improve_closure(closure: Closure) -> Closure:
11831183
return Closure(env, closure.func)
11841184

11851185

1186-
def lift_simple(x: object) -> Object:
1186+
def as_record(x: object) -> Object:
11871187
if isinstance(x, int):
11881188
return Int(x)
11891189
if isinstance(x, str):
11901190
return String(x)
1191+
if isinstance(x, list):
1192+
return List([as_record(item) for item in x])
1193+
if isinstance(x, dict):
1194+
return Record({key: as_record(value) for key, value in x.items()})
11911195
raise NotImplementedError(type(x))
11921196

11931197

1194-
def as_record(obj: dict[str, object]) -> Record:
1195-
conv: Callable[[object], Object] = lambda x: as_record(x) if isinstance(x, dict) else lift_simple(x)
1196-
return Record({key: conv(value) for key, value in obj.items()})
1197-
1198-
11991198
# pylint: disable=redefined-builtin
12001199
def eval_exp(env: Env, exp: Object) -> Object:
12011200
logger.debug(exp)
@@ -4414,6 +4413,20 @@ def int_as_str(obj: Object) -> String:
44144413
PRELUDE = """
44154414
id = x -> x
44164415
4416+
. compile =
4417+
| {type = "Int", value = value} -> $$int_as_str value
4418+
| {type = "Var", name = name} -> name
4419+
| {type = "Binop", op = op, left = left, right = right} -> (compile left) ++ op ++ (compile right)
4420+
| {type = "List", items = items} -> "[" ++ (join ", " (map compile items)) ++ "]"
4421+
| {type = "Assign", name = name, value = value} -> "((" ++ name ++ ") =>" ++ (compile value) ++ ")("
4422+
| {type = "Where", binding={type="Assign", name=name, value=value}, body=body} ->
4423+
"(" ++ (compile name) ++ " => " ++ (compile body) ++ ")(" ++ (compile value) ++ ")"
4424+
4425+
. join = sep ->
4426+
| [] -> ""
4427+
| [x] -> x
4428+
| [x, ...xs] -> x ++ sep ++ (join sep xs)
4429+
44174430
. quicksort =
44184431
| [] -> []
44194432
| [p, ...xs] -> (concat ((quicksort (ltp xs p)) +< p) (quicksort (gtp xs p))
@@ -4454,11 +4467,6 @@ def int_as_str(obj: Object) -> String:
44544467
. any = f ->
44554468
| [] -> #false
44564469
| [x, ...xs] -> f x || any f xs
4457-
4458-
. compile =
4459-
| {type = "Int", value = value} -> $$int_as_str value
4460-
| {type = "Var", name = name} -> name
4461-
| {type = "Binop", op = op, left = left, right = right } -> (compile left) ++ op ++ (compile right)
44624470
"""
44634471

44644472

0 commit comments

Comments
 (0)