def parseFunction(parser): parser.check(expected="function defenition, def", lexeme=keywords['DEF']) lineo = parser.currentToken[2] ## parse identifier parser.next(tokensort=NAME) name = parser.currentToken[1] parser.next() ## parse formals if parser.matchLexeme('('): arguments = parseArguments(parser) function = Function(name, arguments, lineo=lineo) else: function = Function(name, lineo=lineo) ## parser statements. while (parser.hasnext()): if parser.matchLexeme(keywords['END']): parser.next() return function function.addStatement(parseStatement(parser)) if parser.matchLexeme(keywords['END']): #empty function! return function raise SyntaxError(parser.currentToken, expected="END, Missing function ending END")
def function(self): """ def[kw] fname[id] (arglist) {body} end[kw] """ if (self.parsing_function): raise ParseException(" Nested functions not allowed! ") self.parsing_function = True def_tok = self.dequeue() if (def_tok.kind != Token.DEF): raise ParseException("unmatched 'def' in function " + str(def_tok)) id_tok = self.dequeue() if (id_tok.kind != Token.ID): raise ParseException("expected identifier in function" + str(id_tok)) arglist = self.arglist() self.dbg_msg("finished parsing arglist") body = self.stmtlist() self.match(Token.END) [l, c] = def_tok.get_line_col() fval = Function(id_tok.val, arglist, body, l, c, self.debug) self.parsing_function = False self.dbg_msg("finished parsing function") return fval
def test(): """ Test Function """ stmt = Program([ Function("f", [], [ For("i", 1, 10, 1, [ Assign("x", IntValue(3)), Assign("x", IntValue(4)), IfThenElse(BoolValue(True), []), While(BoolValue(True), [Return(IntValue(3))]), Assign("x", IntValue(5)) ]) ]), Function("g", [], [Assign("x", IntValue(3))]), Assign("x", IntValue(1)) ]) nodes, edges = gen_cfg_main(stmt) gen_dot(nodes, edges)
def p_void_function_proto(self, p): '''function_def : void id_d LPAREN M formal_params RPAREN SEMICOLON''' p[0] = Function((p[1], 0), p[2].value, p[5], None) self.pop_tableptr()
def p_void_function_def(self, p): '''function_def : void id_d LPAREN M formal_params RPAREN LBRACKET statement_list RBRACKET''' p[0] = Function((p[1], 0), p[2].value, p[5], p[8]) self.pop_tableptr()
def p_function_proto(self, p): '''function_proto : type stars id_d LPAREN M formal_params RPAREN SEMICOLON''' p[0] = Function((p[1], len(p[2])), p[3].value, p[6], None) self.pop_tableptr()
def p_function_def(self, p): '''function_def : type stars id_d LPAREN M formal_params RPAREN LBRACKET statement_list RBRACKET''' p[0] = Function((p[1], len(p[2])), p[3].value, p[6], p[9]) self.pop_tableptr()
def p_main_function_def(self, p): '''main_function_def : void main LPAREN M RPAREN LBRACKET statement_list RBRACKET''' p[0] = Function((p[1], 0), p[2], [], p[7]) self.pop_tableptr()