def factor(self): if self.currtok[0] == 'MINUS' or self.currtok[0] == "NOT": self.currtok = next(self.tokens) tree = self.primary() return ast.UnaryExpr(tree) else: return self.primary()
def factor(self): """ Factor → [UnaryOp] Primary :return: """ # Check if the token is either a minus or a "not" aka an UnaryOp if self.currtok[0] in [lexer.Lexer.MINUS, lexer.Lexer.NOT]: tmp = self.currtok[1] self.currtok = next(self.tokens) tree = self.primary() return ast.UnaryExpr(tmp, tree) # If not, call primary method else: return self.primary()
def factor(self): """ Factor ⇒ [ UnaryOp ] Primary """ # Check for optional UnaryOp operator. if self.currToken[0] in (lexer.Lexer.SUBTRACT, lexer.Lexer.NOT): # Capture the operator to create a UnaryExpr object. op = self.currToken[0] # Move to the next token. self.currToken = next(self.tokens) # Check for a factor expression and capture it to create a Unary object. There could be more than one '-'. expr = self.factor() # Return a UnaryExpr object. return ast.UnaryExpr(expr, op) # There is no UnaryOp. else: # Check for a primary expression and return it. return self.primary()
def p_expr_unop_not(p): 'expr : NOT expr' p[0] = ast.UnaryExpr('neg', p[2], p.lineno(1))
def p_expr_unop_minus(p): 'expr : MINUS expr %prec UMINUS' p[0] = ast.UnaryExpr('uminus', p[2], p.lineno(1))
def p_num_expr_unary(p): '''num_expr : MINUS expr %prec UMINUS''' p[0] = ast.UnaryExpr(p[2])