Esempio n. 1
0
    def __fdecl(self):
        """<fdecl> ::= FUN ( <type> | NIL ) ID LPAREN <params> RPAREN <bstmts> END"""
        fun_decl_stmt = ast.FunDeclStmt()

        self.__eat(token.FUN, "expected FUN token")

        fun_decl_stmt.return_type = self.current_token

        types = [
            token.ID, token.INTTYPE, token.FLOATTYPE, token.BOOLTYPE,
            token.STRINGTYPE
        ]

        if self.current_token.tokentype in types:
            self.__type()
        elif self.__tokenIs(token.NIL):
            self.__advance()
        else:
            self.__error("Expected type or nil")

        fun_decl_stmt.fun_name = self.current_token
        self.__eat(token.ID, "expected ID")
        self.__eat(token.LPAREN, "expected '('")
        fun_decl_stmt.params = self.__params()
        self.__eat(token.RPAREN, "expected ')'")

        self.__bstmts(fun_decl_stmt.stmt_list.stmts)
        self.__eat(token.END, "expected END")

        return fun_decl_stmt
Esempio n. 2
0
 def __fdecl(self):
     """<fdecl> ::= FUN ( <type>| NIL ) ID LPAREN <params> RPAREN <bstmts> END"""
     funStmt = ast.FunDeclStmt()
     self.__eat(token.FUN, "expecting an fun")
     if self.current_token.tokentype == token.NIL:
         funStmt.return_type = self.current_token
         self.__advance()
     else:
         funStmt.return_type = self.__type()
     funStmt.fun_name = self.current_token
     self.__eat(token.ID, "expecting an id")
     self.__eat(token.LPAREN, "expecting an '('")
     self.__params(funStmt)
     self.__eat(token.RPAREN, "expecting an ')'")
     self.__bstmts(funStmt.stmt_list)
     self.__eat(token.END, "expecting an end")
     return funStmt
Esempio n. 3
0
    def __fdecl(self, stmt_list_node):
        fun_node = ast.FunDeclStmt()
        fun_node.stmt_list = ast.StmtList()
        if self.current_token.tokentype == token.NIL:
            fun_node.return_type = self.current_token
            self.__advance()
        else:
            fun_node.return_type = self.__type()
        fun_node.fun_name = self.current_token
        self.__eat(token.ID, "expecting an ID")
        self.__eat(token.LPAREN, "expecting '('")
        fun_node.params = self.__params()
        self.__eat(token.RPAREN, "expecting ')'")

        self.__bstmts(fun_node.stmt_list)
        self.__eat(token.END, "expecting end")
        stmt_list_node.stmts.append(fun_node)  # check
Esempio n. 4
0
 def __fdecl(self):
     """<fdecl> ::= FUN ( <type> | NIL ) ID LPAREN <params> RPAREN <bstmts> END"""
     fun_decl_node = ast.FunDeclStmt()
     self.__eat(token.FUN, 'expecting "fun"')
     fun_decl_node.return_type = self.current_token
     if (self.current_token.tokentype == token.NIL):
         self.__advance()
     else:
         self.__type()
     fun_decl_node.fun_name = self.current_token
     self.__eat(token.ID, 'expecting "ID"')
     self.__eat(token.LPAREN, 'expecting "("')
     fun_decl_node.params = self.__params()
     self.__eat(token.RPAREN, 'expecting ")"')
     fun_decl_node.stmt_list = self.__bstmts(ast.StmtList())
     self.__eat(token.END, 'expecting "end"')
     return fun_decl_node
Esempio n. 5
0
 def __fdecl(self):
     # print("fdecl: " + str(self.current_token))
     self.__advance() # eat FUN (we already know from stmt)
     fun_decl_stmt_node = ast.FunDeclStmt()
     fun_decl_stmt_node.return_type = self.current_token # FunDeclStmt return type
     if self.current_token.tokentype == token.NIL:
         self.__advance() # eat NIL (we already know from 1 line up)
     else:
         self.__type()
     fun_decl_stmt_node.fun_name = self.current_token # FunDeclStmt function name
     self.__eat(token.ID, 'expected ID')
     self.__eat(token.LPAREN, 'expected "("')
     fun_decl_stmt_node.params = self.__params()
     self.__eat(token.RPAREN, 'expected ")"')
     # passing in a StmtList node because __bstmts() recurses with a StmtList parameter
     fun_decl_stmt_node.stmt_list = self.__bstmts(ast.StmtList())
     self.__eat(token.END, 'expected "end"')
     return fun_decl_stmt_node
Esempio n. 6
0
 def __fdecl(self):
     """<fdecl> ::= FUN (<type>|NIL) ID LPAREN <params> RPAREN <bstmts> END"""
     fun_decl_node = ast.FunDeclStmt()
     self.__eat(token.FUN, "expecting fun keyword")
     if self.current_token.tokentype == token.NIL:
         self.__advance()
     else:
         fun_decl_node.return_type = self.__type()
     fun_decl_node.fun_name = self.current_token
     self.__eat(token.ID, "expecting an identifyer")
     self.__eat(token.LPAREN, 'expecting a "("')
     fun_decl_node.params = self.__params()
     self.__eat(token.RPAREN, 'expecting a ")"')
     stmt_list_node = ast.StmtList()
     self.__bstmts(stmt_list_node)
     fun_decl_node.stmt_list = stmt_list_node
     self.__eat(token.END, "expecting end")
     return fun_decl_node
Esempio n. 7
0
 def __fdecl(self, stmt_list_node):
     functionNode = ast.FunDeclStmt()
     self.__eat(token.FUN, 'expecting fun')
     typelist = [
         token.ID, token.INTTYPE, token.FLOATTYPE, token.BOOLTYPE,
         token.STRINGTYPE
     ]
     functionNode.return_type = self.current_token
     if self.current_token.tokentype in typelist:
         self.__type()
     else:
         self.__eat(token.NIL, 'expecting nil')
     functionNode.fun_name = self.current_token
     self.__eat(token.ID, "expecting id")
     self.__eat(token.LPAREN, "expecting (")
     self.__params(functionNode)
     self.__eat(token.RPAREN, 'expecting )')
     self.__bstmts(functionNode.stmt_list)
     self.__eat(token.END, 'expecting end')
     stmt_list_node.stmts.append(functionNode)
Esempio n. 8
0
 def __fdecl(self, stmt_list_node):
     self.__eat(token.FUN, 'expecting fun')
     fun_node = ast.FunDeclStmt()
     type = [
         token.ID, token.INTTYPE, token.FLOATTYPE, token.BOOLTYPE,
         token.STRINGTYPE
     ]
     if self.current_token.tokentype in type:
         fun_node.return_type = self.current_token
         self.__advance()
     else:
         fun_node.return_type = self.current_token
         self.__eat(token.NIL, 'expecting nil')
     fun_node.fun_name = self.current_token
     self.__eat(token.ID, 'expecting ID')
     self.__eat(token.LPAREN, 'expecting (')
     self.__params(fun_node)
     self.__eat(token.RPAREN, 'expecting rparren')
     self.__bstmts(fun_node.stmt_list)
     stmt_list_node.stmts.append(fun_node)
     self.__eat(token.END, 'expecting end')