def test_visitor(self): class EvalVisitor(ExpressionVisitor): def __init__(self): self.stack = [] def visit_NumericLiteral(self, numericLiteral): self.stack.append(numericLiteral.value) def visit_NEQ(self, binaryExpr): right = self.stack.pop() left = self.stack.pop() self.stack.append(left != right) def visit_AND(self, binaryExpr): right = self.stack.pop() left = self.stack.pop() self.stack.append(left and right) def visit_OR(self, binaryExpr): right = self.stack.pop() left = self.stack.pop() self.stack.append(left or right) def visit_NOT(self, unaryExpr): input = self.stack.pop() self.stack.append(not input) def visit_GTEQ(self, binaryExpr): pass def visit_UnnamedAttributeRef(self, unnamed): pass def visit_StringLiteral(self, stringLiteral): pass def visit_EQ(self, binaryExpr): pass def visit_GT(self, binaryExpr): pass def visit_LT(self, binaryExpr): pass def visit_LTEQ(self, binaryExpr): pass def visit_NamedAttributeRef(self, named): pass def visit_NamedStateAttributeRef(self, named): pass def visit_Case(self, caseExpr): pass def visit_DIVIDE(self, binaryExpr): pass def visit_IDIVIDE(self, binaryExpr): pass def visit_MOD(self, binaryExpr): right = self.stack.pop() left = self.stack.pop() self.stack.append(left % right) def visit_MINUS(self, binaryExpr): pass def visit_NEG(self, binaryExpr): pass def visit_PLUS(self, binaryExpr): pass def visit_TIMES(self, binaryExpr): pass def visit_BinaryFunction(self, expr): pass def visit_LIKE(self, binaryExpr): pass def visit_UnaryFunction(self, expr): pass def visit_CAST(self, expr): pass def visit_NaryFunction(self, expr): pass v = EvalVisitor() ex = e.AND(e.NEQ(e.NumericLiteral(1), e.NumericLiteral(2)), e.NEQ(e.NumericLiteral(4), e.NumericLiteral(5))) ex.accept(v) self.assertEqual(v.stack.pop(), True) v = EvalVisitor() ex = e.AND(e.NEQ(e.NumericLiteral(1), e.NumericLiteral(2)), e.NEQ(e.NumericLiteral(4), e.NumericLiteral(4))) ex.accept(v) self.assertEqual(v.stack.pop(), False) v = EvalVisitor() ex = e.AND(e.NEQ(e.NumericLiteral(1), e.NumericLiteral(2)), e.NOT(e.NEQ(e.NumericLiteral(4), e.NumericLiteral(4)))) ex.accept(v) self.assertEqual(v.stack.pop(), True) v = EvalVisitor() ex = e.MOD(e.NumericLiteral(7), e.NumericLiteral(4)) ex.accept(v) self.assertEqual(v.stack.pop(), 3) v = EvalVisitor() ex = e.NumericLiteral(0xC0FFEE) ex.accept(v) self.assertEqual(v.stack.pop(), 0xC0FFEE)
def p_sexpr_integer_literal(p): 'sexpr : INTEGER_LITERAL' p[0] = sexpr.NumericLiteral(p[1])
def p_sexpr_uminus(p): 'sexpr : MINUS sexpr %prec UMINUS' p[0] = sexpr.TIMES(sexpr.NumericLiteral(-1), p[2])
def p_sexpr_float_literal(p): 'sexpr : FLOAT_LITERAL' p[0] = sexpr.NumericLiteral(p[1])
"parse action for binary operators" left, opstr, right = opexpr for opclass in binopclasses: if opstr in opclass.literals: return opclass(left, right) binop = oneOf(binopstr) arithSign = Word("+-", exact=1) realNum = Combine( Optional(arithSign) + (Word(nums) + "." + Optional(Word(nums)) | ("." + Word(nums))) # noqa + Optional(E + Optional(arithSign) + Word(nums))) realNum.setParseAction(lambda x: expression.NumericLiteral(float(x[0]))) intNum = Combine( Optional(arithSign) + Word(nums) + Optional(E + Optional("+") + Word(nums))) intNum.setParseAction(lambda x: expression.NumericLiteral(int(x[0]))) number = realNum | intNum variable = ident.copy() variable.setParseAction(lambda x: model.Var(x[0])) quotedString.setParseAction(lambda x: expression.StringLiteral(x[0][1:-1])) literal = quotedString | number
def fire(self, expr): if isinstance(expr, algebra.CrossProduct): return algebra.Join(expression.EQ(expression.NumericLiteral(1), expression.NumericLiteral(1)), expr.left, expr.right) return expr