def if_statement(self, level): """ IfStatement -> if '(' Expression ')' Statement [ else Statement ] :param level: indicates level of statements; type(level) - int :return: An ast.IfStatement object :raise CliteSyntaxError if an unexpected token is seen """ # Consume the keyword if identifying the start of an if statement self.curr_tok = self.lex.__next__() # Match left opening left parenthesis if self.curr_tok[self.CODE] != tokens.SINGLE_TOKENS[tokens.LPAREN][0]: raise errors.CliteSyntaxError("'(' expected!", self.curr_tok[self.LINE]) # Consume opening left parenthesis self.curr_tok = self.lex.__next__() expression = self.expression() # Match right closing left parenthesis if self.curr_tok[self.CODE] != tokens.SINGLE_TOKENS[tokens.RPAREN][0]: raise errors.CliteSyntaxError("')' expected!", self.curr_tok[self.LINE]) # Consume closing right parenthesis self.curr_tok = self.lex.__next__() if_stmt = self.statement(level + 1) else_stmt = None # If there is an 'else', consume it if self.curr_tok[self.CODE] == tokens.KEYWORDS[tokens.ELSE]: self.curr_tok = self.lex.__next__() else_stmt = self.statement(level + 1) return ast.IfStatement(expression, if_stmt, else_stmt, level)
def generateSimpleFieldCall(self, cls, basecls, basefunc, field, invokeFuncName): callinfo = ast.Call( ast.AttrRef(ast.Identifier(field.name), invokeFuncName), [ast.Identifier(param.name) for param in basefunc.spec.params]) # teststmts = [ast.CallStatement(ast.Call(ast.Identifier('println'), [ast.makeLiteral('GenerateTreeFunc check ' + basefunc.name + ' ' + field.name), # ast.Identifier(field.name), # ast.Call(ast.Identifier('toString'), [])]))] teststmts = [] isStatement = basefunc.spec.returnType is None or ( isinstance(basefunc.spec.returnType, ast.UserType) and basefunc.spec.returnType.fullpath == 'void') if isStatement: body = ast.StatementBody([ # ast.Call(ast.Identifier('println'), [ast.makeLiteral('GenerateTreeFunc ' + basefunc.name + ' ' + field.name), # ast.Call(ast.AttrRef(ast.Identifier(field.name), 'toString'), []), # ast.Call(ast.Identifier('toString'), [])]), callinfo ]) branch = ast.IfBranch( ast.BinaryOp('!=', ast.Identifier(field.name), ast.Nil()), body) return ast.StatementBody(teststmts + [ast.IfStatement([branch], None)]) return ast.IfElseExpr( ast.BinaryOp('!=', ast.Identifier(field.name), ast.Nil()), callinfo, ast.Nil())
def generateInitializeOwnerCall(self, cls, basecls, basefunc, field): # print('generateInitializeOwnerCall', basefunc.name, cls, field, basefunc) # msgstmt = ast.Call(ast.Identifier('println'), [ast.makeLiteral('generate initializeOwner start ' + field.name), # ast.Identifier(field.name), # ast.Call(ast.Identifier('toString'), [])]) body = ast.StatementBody([ # ast.Call(ast.Identifier('println'), [ast.makeLiteral('generate initializeOwner ' + field.name), # ast.Identifier(field.name), # ast.Call(ast.Identifier('toString'), [])]), ast.Call(ast.AttrRef(ast.Identifier(field.name), 'setOwner'), [ast.This()]), self.generateSimpleFieldCall(cls, basecls, basefunc, field, 'initializeOwner') ]) branch = ast.IfBranch( ast.BinaryOp('!=', ast.Identifier(field.name), ast.Nil()), body) # return ast.StatementBody([msgstmt, ast.IfStatement([branch], None)]) return ast.IfStatement([branch], None)
def ifStatement(self): ''' IfStatement --> if "(" Expression ")" Statement [ else Statement ] :return: ast.IfStatement ''' # need keyword 'if' if self.curr_tok[0] != lexer.Lexer.IF: print("Error: 'if' expected found:", self.curr_tok[1], "on line", self.curr_tok[2]) sys.exit(1) self.curr_tok = next(self.lex) # need '(' if self.curr_tok[0] != lexer.Lexer.LPAREN: print("Error: '(' expected found:", self.curr_tok[1], "on line", self.curr_tok[2]) sys.exit(1) self.curr_tok = next(self.lex) # need Expression expr = self.expr() # need ')' if self.curr_tok[0] != lexer.Lexer.RPAREN: print("Error: ')' expected found:", self.curr_tok[1], "on line", self.curr_tok[2]) sys.exit(1) self.curr_tok = next(self.lex) # need Statement stmt = self.statement() elsstmt = None # else statements if self.curr_tok[0] == lexer.Lexer.ELSE: # if we have 'else' self.curr_tok[0] = next(self.lex) elsstmt = self.statement() return ast.IfStatement(expr, stmt, elsstmt)
def ifstatement(self): """ IfStatement ⇒ if ( Expression ) Statement [ else Statement ] :return: """ # Check for a keyword If if self.currtok[0] == lexer.Lexer.KWDIF: self.currtok = next(self.tokens) # Check for opening parenthesis if self.currtok[0] == lexer.Lexer.OPENP: self.currtok = next(self.tokens) left_tree = self.expression() # Check for a closing parenthesis if self.currtok[0] == lexer.Lexer.CLOSEDP: self.currtok = next(self.tokens) right_tree = self.statement() # Check for a keyword Else if self.currtok[0] == lexer.Lexer.KWDELSE: left_tree1 = self.currtok[1] self.currtok = next(self.tokens) right_tree1 = self.statement() tree = ast.IfStatement(left_tree, right_tree, left_tree1, right_tree1) return tree tree = ast.IfStatement2(left_tree, right_tree) return tree # If there is no closing parenthesis, throw syntax error elif self.currtok[0] != lexer.Lexer.CLOSEDP: raise CliteSyntaxError("Parenthesis expected on line " + str(self.currtok[2])) # If there is no opening parenthesis, throw syntax error elif self.currtok[0] != lexer.Lexer.OPENP: raise CliteSyntaxError("Parenthesis expected on line " + str(self.currtok[2]))
def stmt_if(p): return ast.IfStatement(p[1], p[3], None)
def stmt_elif(p): return ast.IfStatement(p[1], p[3], p[5])
def p_if_statement_02(t): '''if_statement : IF LPAR assignment_expression RPAR statement ELSE statement''' t[0] = ast.IfStatement(t[3], t[5], t[7]) t[5].isStatement = True t[7].isStatement = True