Skip to content

Commit 3bc09d1

Browse files
committed
rec and str
1 parent 4e4bcf5 commit 3bc09d1

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

scrapscript.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4399,6 +4399,18 @@ def int_as_str(obj: Object) -> String:
43994399
return String(str(obj.value))
44004400

44014401

4402+
def str_as_str(obj: Object) -> String:
4403+
if not isinstance(obj, String):
4404+
raise TypeError(f"str_as_str expected String, but got {type(obj).__name__}")
4405+
return String(repr(obj.value))
4406+
4407+
4408+
def record_each(obj: Object) -> Object:
4409+
if not isinstance(obj, Record):
4410+
raise TypeError(f"record_each expected Record, but got {type(obj).__name__}")
4411+
return List([Record({"key": String(key), "value": value}) for key, value in obj.data.items()])
4412+
4413+
44024414
STDLIB = {
44034415
"$$add": Closure({}, Function(Var("x"), Function(Var("y"), Binop(BinopKind.ADD, Var("x"), Var("y"))))),
44044416
"$$fetch": NativeFunction("$$fetch", fetch),
@@ -4407,6 +4419,8 @@ def int_as_str(obj: Object) -> String:
44074419
"$$listlength": NativeFunction("$$listlength", listlength),
44084420
"$$asrecord": NativeFunction("$$asrecord", lambda exp: as_record(exp.serialize())),
44094421
"$$int_as_str": NativeFunction("$$int_as_str", int_as_str),
4422+
"$$str_as_str": NativeFunction("$$str_as_str", str_as_str),
4423+
"$$record_each": NativeFunction("$$record_each", record_each),
44104424
}
44114425

44124426

@@ -4416,9 +4430,13 @@ def int_as_str(obj: Object) -> String:
44164430
. compile =
44174431
| {type="Int", value=value} -> $$int_as_str value
44184432
| {type="Var", name=name} -> name
4433+
| {type="String", value=value} -> $$str_as_str value
44194434
| {type="Binop", op="++", left=left, right=right} -> (compile left) ++ "+" ++ (compile right)
44204435
| {type="Binop", op=op, left=left, right=right} -> (compile left) ++ op ++ (compile right)
44214436
| {type="List", items=items} -> "[" ++ (join ", " (map compile items)) ++ "]"
4437+
| {type="Record", data=data} -> ("{" ++ (join ", " (map compile_pair ($$record_each data))) ++ "}"
4438+
. compile_pair = | {key=key, value=value} -> key ++ ":" ++ (compile value)
4439+
)
44224440
| {type="Assign", name=name, value=value} -> "((" ++ name ++ ") =>" ++ (compile value) ++ ")("
44234441
| {type="Where", binding={type="Assign", name=name, value=value}, body=body} ->
44244442
"(" ++ (compile name) ++ " => " ++ (compile body) ++ ")(" ++ (compile value) ++ ")"

0 commit comments

Comments
 (0)