[Formed-commits] r435 - trunk/formed/formed/model
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Tue Jan 11 15:06:43 CET 2011
Author: torsten
Date: 2011-01-11 15:06:42 +0100 (Tue, 11 Jan 2011)
New Revision: 435
Modified:
trunk/formed/formed/model/exprtree.py
Log:
* formed/formed/model/exprtree.py: Added List-Node and Contains
function.
Modified: trunk/formed/formed/model/exprtree.py
===================================================================
--- trunk/formed/formed/model/exprtree.py 2010-12-27 09:42:39 UTC (rev 434)
+++ trunk/formed/formed/model/exprtree.py 2011-01-11 14:06:42 UTC (rev 435)
@@ -93,6 +93,9 @@
self.pretty(out)
if not atom: out.append(')')
+class Mark(Node):
+ pass
+
class Unary(Node):
def __init__(self, a = None):
@@ -109,6 +112,14 @@
self.a.pretty(out)
out.append(")")
+class ListNode(Unary):
+
+ def __init__(self, a):
+ Unary.__init__(self, a)
+
+ def eval(self, ctx):
+ return self.a
+
class Binary(Unary):
def __init__(self, a = None, b = None):
@@ -315,6 +326,10 @@
def NOT(a): return not a
def ISSET(a): return not a is None
def TYPE(a): return type(a)
+def MARK(a): return Mark(a)
+def CONTAINS(a, b):
+ c = [x.eval() for x in b]
+ return a in c
def KNOWN(a):
return not (
@@ -345,6 +360,17 @@
td = end - start
return td.days
+def BUILDLIST(s):
+ l = []
+ s.pop
+ for x in s[::-1]:
+ if isinstance(x, Mark):
+ s.pop()
+ break;
+ else:
+ l.append(s.pop())
+ return l
+
NODE_FACTORIES = {
'+': lambda s: OperatorBinary(s.pop(), s.pop(), ADD),
'-': lambda s: OperatorBinary(s.pop(-2), s.pop(), MINUS),
@@ -376,6 +402,8 @@
'today': lambda s: Today(),
'date': lambda s: DateNode(s.pop(), s.pop(), s.pop()),
'.': lambda s: Out(s.pop()),
+ ']': lambda s: ListNode(BUILDLIST(s)),
+ 'contains': lambda s: OperatorBinary(s.pop(), s.pop(), CONTAINS),
'?': lambda s: IThenElse(s.pop(), s.pop(), s.pop())
}
@@ -393,7 +421,11 @@
def evaluate(self, vars=None):
if self.prog is None:
- self.compile()
+ try:
+ self.compile()
+ except Exception, e:
+ print e
+
ctx = EvalContext(vars)
try:
@@ -410,8 +442,12 @@
# XXX: shlex does not support unicode -> ascii
encoder = codecs.getencoder("ascii")
- for token in lexsplit(encoder(self.expr)[0]):
+ tokens = lexsplit(encoder(self.expr)[0])
+ for token in tokens:
token = unicode(token)
+ if token == "[":
+ stack.append(Mark())
+ continue
m = VAR_RE.match(token)
if m:
var = m.group(1)
@@ -427,8 +463,9 @@
stack.append(Const(float(m.group(1))))
continue
try:
- stack.append(NODE_FACTORIES[token](stack))
- except KeyError:
+ factory = NODE_FACTORIES[token](stack)
+ stack.append(factory)
+ except KeyError, e:
stack.append(Const(token))
L = len(stack)
More information about the Formed-commits
mailing list