Skip to content

Commit 2a1f59b

Browse files
committed
Write compiler in scrapscript
1 parent 7886f32 commit 2a1f59b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

scrapscript.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ class Binop(Object):
743743
def serialize(self) -> Dict[str, object]:
744744
return {
745745
"type": "Binop",
746-
"op": self.op.name,
746+
"op": BinopKind.to_str(self.op),
747747
"left": self.left.serialize(),
748748
"right": self.right.serialize(),
749749
}
@@ -4389,13 +4389,20 @@ def listlength(obj: Object) -> Object:
43894389
return Int(len(obj.items))
43904390

43914391

4392+
def int_as_str(obj: Object) -> String:
4393+
if not isinstance(obj, Int):
4394+
raise TypeError(f"int_as_str expected Int, but got {type(obj).__name__}")
4395+
return String(str(obj.value))
4396+
4397+
43924398
STDLIB = {
43934399
"$$add": Closure({}, Function(Var("x"), Function(Var("y"), Binop(BinopKind.ADD, Var("x"), Var("y"))))),
43944400
"$$fetch": NativeFunction("$$fetch", fetch),
43954401
"$$jsondecode": NativeFunction("$$jsondecode", jsondecode),
43964402
"$$serialize": NativeFunction("$$serialize", lambda obj: Bytes(serialize(obj))),
43974403
"$$listlength": NativeFunction("$$listlength", listlength),
43984404
"$$asrecord": NativeFunction("$$asrecord", lambda exp: as_record(exp.serialize())),
4405+
"$$int_as_str": NativeFunction("$$int_as_str", int_as_str),
43994406
}
44004407

44014408

@@ -4442,6 +4449,11 @@ def listlength(obj: Object) -> Object:
44424449
. any = f ->
44434450
| [] -> #false
44444451
| [x, ...xs] -> f x || any f xs
4452+
4453+
. compile =
4454+
| {type = "Int", value = value} -> $$int_as_str value
4455+
| {type = "Var", name = name} -> name
4456+
| {type = "Binop", op = op, left = left, right = right } -> (compile left) ++ op ++ (compile right)
44454457
"""
44464458

44474459

0 commit comments

Comments
 (0)