• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

Nix flake for RPython interpreters


Commit MetaInfo

Revisione0d54da70c476451ae1ca7a48ccbbe8c6b8e1139 (tree)
Time2024-06-06 13:57:37
AuthorCorbin <cds@corb...>
CommiterCorbin

Log Message

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.

Change Summary

Incremental Difference

--- a/regiux/heap.py
+++ b/regiux/heap.py
@@ -25,6 +25,9 @@ class HeapObject(object):
2525 def unwrapBool(self):
2626 raise WrongType("Heap object type %s isn't a Boolean" %
2727 self.__class__.__name__)
28+ def unwrapInt(self):
29+ raise WrongType("Heap object type %s isn't an integer" %
30+ self.__class__.__name__)
2831
2932 class HeapTrue(HeapObject):
3033 _immutable_ = True
@@ -42,6 +45,7 @@ class HeapInt(HeapObject):
4245 _immutable_ = True
4346 def __init__(self, i): self.i = i
4447 def asStr(self): return str(self.i)
48+ def unwrapInt(self): return self.i
4549
4650 class HeapStr(HeapObject):
4751 _immutable_ = True
@@ -123,10 +127,23 @@ class HeapApp(MutableObject):
123127
124128 class HeapAction(MutableObject):
125129 def __init__(self, action): self.action = action
126- def asStr(self): return "<internal lambda>"
130+ def asStr(self): return "<internal lambda (unary)>"
127131 def apply(self, obj): return self.action(obj)
128132
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+
129145 builtins = HeapAttrSet({
146+ "add": HeapBinary(lambda x, y: HeapInt(x.unwrapInt() + y.unwrapInt())),
130147 "length": HeapAction(lambda obj: HeapInt(obj.length())),
131148 })
132149
--- a/regiux/parser.py
+++ b/regiux/parser.py
@@ -224,6 +224,10 @@ class ExprUnaryBox(BaseBox):
224224 def pretty(self):
225225 return "(%s%s)" % (self.op, self.expr.pretty())
226226
227+opDict = {
228+ "+": "add",
229+}
230+
227231 class ExprBinaryBox(BaseBox):
228232 def __init__(self, left, right, op):
229233 self.left = left
@@ -231,6 +235,14 @@ class ExprBinaryBox(BaseBox):
231235 self.op = op.getstr()
232236 def pretty(self):
233237 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))
234246
235247 class HasBox(BaseBox):
236248 def __init__(self, value, path):