• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

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

修訂1530ce8f9d6c265874e059a13bf32ebdfae834cf (tree)
時間2024-05-15 11:09:37
作者Corbin <cds@corb...>
CommiterCorbin

Log Message

reguix: Wire up basic inherits.

Change Summary

差異

--- a/regiux/heap.py
+++ b/regiux/heap.py
@@ -4,6 +4,10 @@ class WrongType(Exception):
44 "A runtime typecheck failed."
55 def __init__(self, message): self.message = message
66
7+class MissingAttr(Exception):
8+ "An attrset was missing an attr at runtime."
9+ def __init__(self, attr): self.attr = attr
10+
711 class HeapObject(object):
812 "A possibly-cyclic possibly-incomplete value."
913 # .resolve() is implementation-internal, always call .evaluate()
@@ -20,16 +24,16 @@ class HeapObject(object):
2024 self.__class__.__name__)
2125
2226 class MutableObject(HeapObject):
23- evaluated = loop = False
27+ loop = False
28+ cache = None
2429 def evaluate(self):
25- if not self.evaluated:
30+ if not self.cache:
2631 if self.loop: raise InfiniteLoop()
2732 try:
2833 self.loop = True
29- return self.resolve()
30- finally:
31- self.loop = False
32- self.evaluated = True
34+ self.cache = self.resolve()
35+ finally: self.loop = False
36+ return self.cache
3337
3438 class HeapInt(HeapObject):
3539 _immutable_ = True
@@ -57,19 +61,19 @@ class HeapAttrSet(MutableObject):
5761 return "{ <attrset with %d items> }" % len(self.attrs)
5862 return "{ %s }" % " ".join(["%s = %s;" % (name, obj) for (name, obj) in self.attrs.items()])
5963 def resolve(self):
60- # XXX update dict?
6164 for obj in self.attrs.values(): obj.evaluate()
6265 return self
63- def getAttr(self, name): return self.attrs.get(name)
66+ def getAttr(self, name):
67+ try: return self.attrs[name]
68+ except KeyError: raise MissingAttr(name)
6469
6570 class HeapList(MutableObject):
6671 def __init__(self, objs): self.objs = objs
6772 def asStr(self):
68- if self.evaluated:
73+ if self.cache:
6974 return "[ %s ]" % " ".join([obj.asStr() for obj in self.objs])
7075 else: return "[ <list of %d items> ]" % len(self.objs)
7176 def resolve(self):
72- # XXX update list?
7377 for obj in self.objs: obj.evaluate()
7478 return self
7579 def length(self): return len(self.objs)
@@ -81,9 +85,7 @@ class HeapSelect(MutableObject):
8185 def asStr(self): return self.obj.asStr() + "." + ".".join(self.path)
8286 def resolve(self):
8387 obj = self.obj
84- for segment in self.path:
85- obj = obj.evaluate().getAttr(segment)
86- if obj is None: raise ValueError("Missing attr!")
88+ for segment in self.path: obj = obj.evaluate().getAttr(segment)
8789 return obj
8890
8991 class HeapApp(MutableObject):
--- a/regiux/main.py
+++ b/regiux/main.py
@@ -1,7 +1,7 @@
11 import sys
22
3-from heap import defaultScope, WrongType
4-from parser import parse, NotAnExpr, ParseError
3+from heap import defaultScope, InfiniteLoop, MissingAttr, WrongType
4+from parser import parse, MissingVar, NotAnExpr, ParseError
55
66 def entryPoint(argv):
77 if len(argv) < 2:
@@ -31,6 +31,17 @@ def entryPoint(argv):
3131 print "Error while compiling"
3232 print "Input syntax was not an expr, but:", e.cls
3333 return 1
34+ except MissingVar as e:
35+ print "Error while compiling"
36+ print "Name was missing:", e.name
37+ return 1
38+ except InfiniteLoop as e:
39+ print "Error while executing: infinite loop"
40+ return 1
41+ except MissingAttr as e:
42+ print "Error while executing"
43+ print "Attr was missing:", e.attr
44+ return 1
3445 except WrongType as e:
3546 print "Error while executing"
3647 print "Type error:", e.message
--- a/regiux/parser.py
+++ b/regiux/parser.py
@@ -65,6 +65,9 @@ lexer = lg.build()
6565 class NotAnExpr(Exception):
6666 def __init__(self, cls): self.cls = cls
6767
68+class MissingVar(Exception):
69+ def __init__(self, name): self.name = name
70+
6871 class RegiuxBox(BaseBox):
6972 "A box for any syntax."
7073 cls = "unknown"
@@ -96,8 +99,9 @@ class BindsBox(EBox):
9699 def compile(self, scope):
97100 attrs = {}
98101 for bind in self.binds:
99- # XXX do actual codegen
100- for name in bind.getnames(): attrs[name] = heap.HeapStr("...")
102+ for name in bind.getnames():
103+ if name not in scope: raise MissingVar(name)
104+ attrs[name] = scope[name]
101105 return heap.HeapAttrSet(attrs)
102106
103107 class ListBox(EBox):
@@ -133,7 +137,7 @@ class VarBox(EBox):
133137 def pretty(self): return self.name
134138 def compile(self, scope):
135139 if self.name in scope: return scope[self.name]
136- raise ValueError("Name not in scope")
140+ raise MissingVar(self.name)
137141
138142 class AppBox(EBox):
139143 def __init__(self, func, arg):