• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
Aucun tag

Frequently used words (click to add to your profile)

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

A fast implementation of the Nix expression language


Commit MetaInfo

Révision4127f061908401ed22b880862dfb62ba6be22fd7 (tree)
l'heure2024-05-25 11:28:14
AuteurCorbin <cds@corb...>
CommiterCorbin

Message de Log

regiux: Implement Booleans and if-expressions.

Change Summary

Modification

--- a/heap.py
+++ b/heap.py
@@ -22,18 +22,21 @@ class HeapObject(object):
2222 def getAttr(self, name):
2323 raise WrongType("Heap object type %s doesn't have attributes" %
2424 self.__class__.__name__)
25+ def unwrapBool(self):
26+ raise WrongType("Heap object type %s isn't a Boolean" %
27+ self.__class__.__name__)
2528
26-class MutableObject(HeapObject):
27- loop = False
28- cache = None
29- def evaluate(self):
30- if not self.cache:
31- if self.loop: raise InfiniteLoop()
32- try:
33- self.loop = True
34- self.cache = self.resolve()
35- finally: self.loop = False
36- return self.cache
29+class HeapTrue(HeapObject):
30+ _immutable_ = True
31+ def asStr(self): return "true"
32+ def unwrapBool(self): return True
33+true = HeapTrue()
34+
35+class HeapFalse(HeapObject):
36+ _immutable_ = True
37+ def asStr(self): return "false"
38+ def unwrapBool(self): return False
39+false = HeapFalse()
3740
3841 class HeapInt(HeapObject):
3942 _immutable_ = True
@@ -49,11 +52,34 @@ class HeapStr(HeapObject):
4952
5053 class InfiniteLoop(Exception): pass
5154
55+class MutableObject(HeapObject):
56+ loop = False
57+ cache = None
58+ def evaluate(self):
59+ if not self.cache:
60+ if self.loop: raise InfiniteLoop()
61+ try:
62+ self.loop = True
63+ self.cache = self.resolve()
64+ finally: self.loop = False
65+ return self.cache
66+
5267 # XXX currently unused, but ready to go
5368 # class HeapThunk(MutableObject):
5469 # def __init__(self, obj): self.obj = obj
5570 # def resolve(self): return self.obj
5671
72+class HeapCond(MutableObject):
73+ def __init__(self, b, cons, alt):
74+ self.b = b
75+ self.cons = cons
76+ self.alt = alt
77+ def asStr(self):
78+ return "if %s then %s else %s" % (self.b.asStr(), self.cons.asStr(), self.alt.asStr())
79+ def resolve(self):
80+ rv = self.cons if self.b.unwrapBool() else self.alt
81+ return rv.evaluate()
82+
5783 class HeapAttrSet(MutableObject):
5884 def __init__(self, attrs): self.attrs = attrs
5985 def asStr(self):
@@ -106,4 +132,5 @@ builtins = HeapAttrSet({
106132
107133 defaultScope = {
108134 "builtins": builtins,
135+ "false": false, "true": true,
109136 }
--- a/parser.py
+++ b/parser.py
@@ -90,6 +90,18 @@ class StrBox(EBox):
9090 def pretty(self): return '"%s"' % self.value
9191 def compile(self, scope): return heap.HeapStr(self.value)
9292
93+class IfBox(EBox):
94+ def __init__(self, cond, seq, alt):
95+ self.cond = cond
96+ self.seq = seq
97+ self.alt = alt
98+ def pretty(self):
99+ return "if %s then %s else %s" % (
100+ self.cond.pretty(), self.seq.pretty(), self.alt.pretty())
101+ def compile(self, scope):
102+ return heap.HeapCond(self.cond.compile(scope), self.seq.compile(scope),
103+ self.alt.compile(scope))
104+
93105 class BindsBox(EBox):
94106 def __init__(self, binds): self.binds = binds
95107 def pretty(self):
@@ -249,15 +261,6 @@ class LambdaBox(BaseBox):
249261 elif self.params: return "%s: %s" % (self.params.pretty(), body)
250262 else: return "_: " + body
251263
252-class IfBox(BaseBox):
253- def __init__(self, cond, seq, alt):
254- self.cond = cond
255- self.seq = seq
256- self.alt = alt
257- def pretty(self):
258- return "if %s then %s else %s" % (
259- self.cond.pretty(), self.seq.pretty(), self.alt.pretty())
260-
261264 pg = rply.ParserGenerator(KEYWORDS + [
262265 "ID", "INT", "SPATH", "URI",
263266 "AND", "IMPL", "OR_OP",