Nix flake for RPython interpreters
Revision | e0d54da70c476451ae1ca7a48ccbbe8c6b8e1139 (tree) |
---|---|
Time | 2024-06-06 13:57:37 |
Author | Corbin <cds@corb...> |
Commiter | Corbin |
regiux: Stub out binary ops.
The provided addition op works fine, first try. But I'm not going to be
worried about that. Why wouldn't it work? The curry is so very natural.
I was expecting an RPython error but the translator is generous today.
@@ -25,6 +25,9 @@ class HeapObject(object): | ||
25 | 25 | def unwrapBool(self): |
26 | 26 | raise WrongType("Heap object type %s isn't a Boolean" % |
27 | 27 | self.__class__.__name__) |
28 | + def unwrapInt(self): | |
29 | + raise WrongType("Heap object type %s isn't an integer" % | |
30 | + self.__class__.__name__) | |
28 | 31 | |
29 | 32 | class HeapTrue(HeapObject): |
30 | 33 | _immutable_ = True |
@@ -42,6 +45,7 @@ class HeapInt(HeapObject): | ||
42 | 45 | _immutable_ = True |
43 | 46 | def __init__(self, i): self.i = i |
44 | 47 | def asStr(self): return str(self.i) |
48 | + def unwrapInt(self): return self.i | |
45 | 49 | |
46 | 50 | class HeapStr(HeapObject): |
47 | 51 | _immutable_ = True |
@@ -123,10 +127,23 @@ class HeapApp(MutableObject): | ||
123 | 127 | |
124 | 128 | class HeapAction(MutableObject): |
125 | 129 | def __init__(self, action): self.action = action |
126 | - def asStr(self): return "<internal lambda>" | |
130 | + def asStr(self): return "<internal lambda (unary)>" | |
127 | 131 | def apply(self, obj): return self.action(obj) |
128 | 132 | |
133 | +class HeapBinary(MutableObject): | |
134 | + def __init__(self, action): self.action = action | |
135 | + def asStr(self): return "<internal lambda (binary)>" | |
136 | + def apply(self, obj): return HeapCurry(self.action, obj) | |
137 | + | |
138 | +class HeapCurry(MutableObject): | |
139 | + def __init__(self, action, x): | |
140 | + self.action = action | |
141 | + self.x = x | |
142 | + def asStr(self): return "<internal lambda (curried)>" | |
143 | + def apply(self, y): return self.action(self.x, y) | |
144 | + | |
129 | 145 | builtins = HeapAttrSet({ |
146 | + "add": HeapBinary(lambda x, y: HeapInt(x.unwrapInt() + y.unwrapInt())), | |
130 | 147 | "length": HeapAction(lambda obj: HeapInt(obj.length())), |
131 | 148 | }) |
132 | 149 |
@@ -224,6 +224,10 @@ class ExprUnaryBox(BaseBox): | ||
224 | 224 | def pretty(self): |
225 | 225 | return "(%s%s)" % (self.op, self.expr.pretty()) |
226 | 226 | |
227 | +opDict = { | |
228 | + "+": "add", | |
229 | +} | |
230 | + | |
227 | 231 | class ExprBinaryBox(BaseBox): |
228 | 232 | def __init__(self, left, right, op): |
229 | 233 | self.left = left |
@@ -231,6 +235,14 @@ class ExprBinaryBox(BaseBox): | ||
231 | 235 | self.op = op.getstr() |
232 | 236 | def pretty(self): |
233 | 237 | return "(%s %s %s)" % (self.left.pretty(), self.op, self.right.pretty()) |
238 | + def compile(self, scope): | |
239 | + # XXX move to constructor or production? | |
240 | + assert self.op in opDict | |
241 | + verb = opDict[self.op] | |
242 | + return heap.HeapApp( | |
243 | + heap.HeapApp(heap.HeapSelect(scope["builtins"], [verb]), | |
244 | + self.left.compile(scope)), | |
245 | + self.right.compile(scope)) | |
234 | 246 | |
235 | 247 | class HasBox(BaseBox): |
236 | 248 | def __init__(self, value, path): |