Skip to content

Commit 01a1b71

Browse files
committed
rec and str
1 parent 26a7e7b commit 01a1b71

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
@@ -4394,6 +4394,18 @@ def int_as_str(obj: Object) -> String:
43944394
return String(str(obj.value))
43954395

43964396

4397+
def str_as_str(obj: Object) -> String:
4398+
if not isinstance(obj, String):
4399+
raise TypeError(f"str_as_str expected String, but got {type(obj).__name__}")
4400+
return String(repr(obj.value))
4401+
4402+
4403+
def record_each(obj: Object) -> Object:
4404+
if not isinstance(obj, Record):
4405+
raise TypeError(f"record_each expected Record, but got {type(obj).__name__}")
4406+
return List([Record({"key": String(key), "value": value}) for key, value in obj.data.items()])
4407+
4408+
43974409
STDLIB = {
43984410
"$$add": Closure({}, Function(Var("x"), Function(Var("y"), Binop(BinopKind.ADD, Var("x"), Var("y"))))),
43994411
"$$fetch": NativeFunction("$$fetch", fetch),
@@ -4402,6 +4414,8 @@ def int_as_str(obj: Object) -> String:
44024414
"$$listlength": NativeFunction("$$listlength", listlength),
44034415
"$$asrecord": NativeFunction("$$asrecord", lambda exp: as_record(exp.serialize())),
44044416
"$$int_as_str": NativeFunction("$$int_as_str", int_as_str),
4417+
"$$str_as_str": NativeFunction("$$str_as_str", str_as_str),
4418+
"$$record_each": NativeFunction("$$record_each", record_each),
44054419
}
44064420

44074421

@@ -4411,9 +4425,13 @@ def int_as_str(obj: Object) -> String:
44114425
. compile =
44124426
| {type="Int", value=value} -> $$int_as_str value
44134427
| {type="Var", name=name} -> name
4428+
| {type="String", value=value} -> $$str_as_str value
44144429
| {type="Binop", op="++", left=left, right=right} -> (compile left) ++ "+" ++ (compile right)
44154430
| {type="Binop", op=op, left=left, right=right} -> (compile left) ++ op ++ (compile right)
44164431
| {type="List", items=items} -> "[" ++ (join ", " (map compile items)) ++ "]"
4432+
| {type="Record", data=data} -> ("{" ++ (join ", " (map compile_pair ($$record_each data))) ++ "}"
4433+
. compile_pair = | {key=key, value=value} -> key ++ ":" ++ (compile value)
4434+
)
44174435
| {type="Assign", name=name, value=value} -> "((" ++ name ++ ") =>" ++ (compile value) ++ ")("
44184436
| {type="Where", binding={type="Assign", name=name, value=value}, body=body} ->
44194437
"(" ++ (compile name) ++ " => " ++ (compile body) ++ ")(" ++ (compile value) ++ ")"

0 commit comments

Comments
 (0)