def __bconnct(self, boolNode): if self.current_token.tokentype == token.AND: boolNode.bool_connector = self.current_token self.__advance() boolNode.rest = ast.BoolExpr() self.__bexpr(boolNode.rest) # if boolNode.rest.first_expr != None: # boolNode.first_expr = boolNode.rest elif self.current_token.tokentype == token.OR: boolNode.bool_connector = self.current_token self.__advance() boolNode.rest = ast.BoolExpr() self.__bexpr(boolNode.rest)
def __bconnct(self, bexpr_node): temp_bool_expr = ast.BoolExpr() if (self.current_token.tokentype == token.AND or self.current_token.tokentype == token.OR): bexpr_node.bool_connector = self.current_token self.__advance() self.__bexpr(temp_bool_expr) bexpr_node.rest = temp_bool_expr
def __while(self): while_node = ast.WhileStmt() while_node.bool_expr = ast.BoolExpr() self.__bexpr(while_node.bool_expr) while_node.stmt_list = ast.StmtList() self.__eat(token.DO, "expecting do") self.__bstmts(while_node.stmt_list) self.__eat(token.END, "expecting end") return while_node
def __cond(self): if_node = ast.IfStmt() basic_if_node = ast.BasicIf() basic_if_node.bool_expr = ast.BoolExpr() basic_if_node.stmt_list = ast.StmtList() self.__bexpr(basic_if_node.bool_expr) self.__eat(token.THEN, "expecting then") self.__bstmts(basic_if_node.stmt_list) if_node.if_part = basic_if_node self.__condt(if_node) self.__eat(token.END, "expecting end") return if_node
def __bexpr(self, boolNode): if self.current_token.tokentype == token.NOT: boolNode.negated = True self.__advance() self.__bexpr(boolNode) self.__bexprt(boolNode) elif self.current_token.tokentype == token.LPAREN: self.__advance() boolNode.first_expr = ast.BoolExpr() self.__bexpr(boolNode.first_expr) self.__eat(token.RPAREN, 'expecting )') self.__bconnct(boolNode) else: boolNode.first_expr = self.__expr() self.__bexprt(boolNode)
def __bexpr(self): bool_expr_node = ast.BoolExpr() if self.current_token.tokentype == token.NOT: bool_expr_node.negated = True self.__advance() bool_expr_node = self.__bexpr() self.__bexprt(bool_expr_node) elif self.current_token.tokentype == token.LPAREN: self.__advance() bool_expr_node.first_expr = self.__bexpr() self.__eat(token.RPAREN, 'expecting rparren') self.__bconnct(bool_expr_node) else: bool_expr_node.first_expr = self.__expr() self.__bexprt(bool_expr_node) return bool_expr_node
def __condt(self, if_node): if_node.else_stmts = ast.StmtList() basic_if_node = ast.BasicIf() basic_if_node.bool_expr = ast.BoolExpr() if self.current_token.tokentype == token.ELIF: basic_if_node.stmt_list = ast.StmtList() self.__advance() self.__bexpr(basic_if_node.bool_expr) self.__eat(token.THEN, "expecting then") self.__bstmts(basic_if_node.stmt_list) if_node.elseifs.append(basic_if_node) self.__condt(if_node) elif self.current_token.tokentype == token.ELSE: if_node.has_else = True self.__advance() self.__bstmts(if_node.else_stmts)
def __bexpr(self, bexpr_node): if self.current_token.tokentype == token.NOT: bexpr_node.negated = True self.__advance() self.__bexpr(bexpr_node) self.__bexprt(bexpr_node) elif self.current_token.tokentype == token.LPAREN: bexpr_node.first_expr = ast.BoolExpr() self.__advance() # in this specific instance bexpr_node.first_expr must be a Bool expr self.__bexpr(bexpr_node.first_expr) self.__eat(token.RPAREN, "expecting ')'") self.__bconnct(bexpr_node) else: bexpr_node.first_expr = self.__expr() self.__bexprt(bexpr_node)
def __bexpr(self): """<bexpt> ::= <expr> <bexprt> | NOT <bexpr> <bexprt> |""" """ LPAREN <bexpr> RPAREN <bconnct>""" bool_expr_node = ast.BoolExpr() if self.current_token.tokentype == token.LPAREN: self.__advance() bool_expr_node.first_expr = self.__bexpr() self.__eat(token.RPAREN, 'expecting a ")"') self.__bconnct(bool_expr_node) elif self.current_token.tokentype == token.NOT: self.__advance() bool_expr_node.first_expr = self.__bexpr() bool_expr_node.negated = True self.__bexprt(bool_expr_node) else: bool_expr_node.first_expr = self.__expr() self.__bexprt(bool_expr_node) return bool_expr_node
def __bexpr(self): boolStmt = ast.BoolExpr() """<bexpr> ::= <expr><bexprt>| NOT <bexpr><bexprt> | LPAREN <bexpr> RPAREN <bconnct>""" temp = self.current_token.tokentype if temp == token.NOT: self.__advance() boolStmt = self.__bexpr() boolStmt.negated = True self.__bexprt(boolStmt) elif temp == token.LPAREN: self.__advance() boolStmt = self.__bexpr() self.__eat(token.RPAREN, "expecting a ')'") self.__bconnct(boolStmt) else: boolStmt.first_expr = self.__expr() self.__bexprt(boolStmt) return boolStmt
def __bexpr(self): # print("bexpr: " + str(self.current_token)) bool_expr_node = ast.BoolExpr() if self.current_token.tokentype == token.NOT: bool_expr_node.negated = True self.__advance() # eat NOT (we already know from 2 lines up) bool_expr_node.first_expr = self.__bexpr() bool_expr_node = self.__bexprt(bool_expr_node) elif self.current_token.tokentype == token.LPAREN: self.__advance() # eat LPAREN (we already know from 1 line up) bool_expr_node.first_expr = self.__bexpr() self.__eat(token.RPAREN, 'expected ")"') # fine to not fill bool_rel and second_expr, they're optional bool_expr_node = self.__bconnct(bool_expr_node) else: bool_expr_node.first_expr = self.__expr() bool_expr_node = self.__bexprt(bool_expr_node) return bool_expr_node
def __bexpr(self): """<bexpr> ::= <expr> <bexprt> | NOT <bexpr> <bexprt> | LPAREN <bexpr> RPAREN <bconnct>""" bexpr = ast.BoolExpr() if self.current_token.tokentype == token.LPAREN: self.__eat(token.LPAREN, "Expected expression") bexpr = self.__bexpr() self.__eat(token.RPAREN, "Expected RPAREN token") self.__bconnct(bexpr) return bexpr elif self.current_token.tokentype == token.NOT: self.__eat(token.NOT, "Expected NOT token") bexpr = self.__bexpr() self.__bexprt(bexpr) bexpr.negated = True return bexpr else: bexpr.first_expr = self.__expr() self.__bexprt(bexpr) return bexpr
def __bexpr(self): """<bexpr> ::= <expr> <bexprt> | NOT <bexpr> <bexprt> | LPAREN <bexpr> RPAREN <bconnct> """ bexpr = ast.BoolExpr() if self.__tokenIs(token.NOT): bexpr.negated = True self.__advance() bexpr.first_expr = self.__bexpr() self.__bexprt(bexpr) elif self.__tokenIs(token.LPAREN): bexpr.negated = False self.__advance() bexpr.first_expr = self.__bexpr() self.__eat(token.RPAREN, "expected ')'") self.__bconnct(bexpr) else: bexpr.negated = False bexpr.first_expr = self.__expr() self.__bexprt(bexpr) return bexpr