def _ThreeArgs(argv): """Returns an expression tree to be evaluated.""" a0, a1, a2 = argv # NOTE: Order is important here. binary_id = _BINARY_LOOKUP.get(a1) if binary_id is not None: left = ast.StringWord(Id.Word_Compound, a0) right = ast.StringWord(Id.Word_Compound, a2) return ast.BoolBinary(binary_id, left, right) if a1 == '-a': left = _StringWordTest(a0) right = _StringWordTest(a2) return ast.LogicalAnd(left, right) if a1 == '-o': left = _StringWordTest(a0) right = _StringWordTest(a2) return ast.LogicalOr(left, right) if a0 == '!': child = _TwoArgs(argv[1:]) return ast.LogicalNot(child) if a0 == '(' and a2 == ')': return _StringWordTest(a1) util.p_die('Syntax error: binary operator expected')
def ReadWord(self, unused_lex_mode): if self.i == self.n: # NOTE: Could define something special return ast.StringWord(Id.Eof_Real, '') #log('ARGV %s i %d', self.argv, self.i) s = self.argv[self.i] self.i += 1 # default is an operand word id_ = (_UNARY_LOOKUP.get(s) or _BINARY_LOOKUP.get(s) or _OTHER_LOOKUP.get(s) or Id.Word_Compound) return ast.StringWord(id_, s)
def _TwoArgs(argv): """Returns an expression tree to be evaluated.""" a0, a1 = argv if a0 == '!': return ast.LogicalNot(_StringWordTest(a1)) unary_id = _UNARY_LOOKUP.get(a0) if unary_id is None: # TODO: # - syntax error # - separate lookup by unary util.p_die('Expected unary operator, got %r', a0) child = ast.StringWord(Id.Word_Compound, a1) return ast.BoolUnary(unary_id, child)
def _StringWordTest(s): # TODO: Could be Word_String return ast.WordTest(ast.StringWord(Id.Word_Compound, s))