void WASMModuleParser::parseFunctionDefinitionSection() { for (size_t i = 0; i < m_module->functionDeclarations().size(); ++i) { parseFunctionDefinition(); PROPAGATE_ERROR(); } }
void Parser::parseDefinitions() { Token t = lex.peek(); switch(t.type) { case Token::FLOAT: case Token::STRING: case Token::COLOR: case Token::POINT: case Token::VECTOR: case Token::NORMAL: case Token::MATRIX: case Token::VOID: parseFunctionDefinition(); parseDefinitions(); break; case Token::LIGHT: case Token::SURFACE: case Token::VOLUME: case Token::DISPLACEMENT: case Token::IMAGER: parseShaderDefinition(); parseDefinitions(); break; case Token::ENDOFFILE: return; default: assert(0 && "Unexpected token!"); } }
void WASMModuleParser::parseFunctionDefinitionSection() { for (size_t functionIndex = 0; functionIndex < m_module->functionDeclarations().size(); ++functionIndex) { parseFunctionDefinition(functionIndex); PROPAGATE_ERROR(); } for (WASMFunctionPointerTable& functionPointerTable : m_module->functionPointerTables()) { for (size_t i = 0; i < functionPointerTable.functionIndices.size(); ++i) functionPointerTable.functions.uncheckedAppend(m_module->functions()[functionPointerTable.functionIndices[i]].get()); } }
TreeContainer *Parser::ParseTrees(const std::vector<Token*> &tokens) { _tokenIndex = 0; _tokens = tokens; next(); // setup first token. TreeContainer *trees = new TreeContainer();; while (_curTokenType != EOF) { if (_curTokenType == ';') { next(); } else if (_curTokenType == tok_class) { ClassAst *class_ = parseClassDefinition(); if (class_ == nullptr) { delete trees; return nullptr; } trees->ClassDefinitions.push_back(class_); } else if (_curTokenType == tok_func) { FunctionAst *func = parseFunctionDefinition(); if (func == nullptr) { delete trees; return nullptr; } trees->FunctionDefinitions.push_back(func); } else if (_curTokenType == tok_extern) { PrototypeAst *declaration = parseExternDeclaration(); if (declaration == nullptr) { delete trees; return nullptr; } trees->ExternalDeclarations.push_back(declaration); } else { IAstExpression *toplevel = parseTopLevelExpression(); if (toplevel == nullptr) { delete trees; return nullptr; } trees->TopLevelExpressions.push_back(toplevel); } } return trees; }
Expression *ASTBuilder::parseTopLevelStatement() { // get the type Token *type = getNextToken(); if(type == NULL) return NULL; Token *symbolName = getCurrentToken(); if(symbolName == NULL) return NULL; if(!isSymbol(symbolName->getData())) { cout << "Error: astbuilder.cpp:33" << endl; } // next need to check what comes after, // an equals => variable assigment, // an argument list => function definition, // another typename => variable definition w/out assigment Token *next = peekNextToken(); // there's not another token if(next == NULL) { return new VarExpression(symbolName->getData()); }else if(!next->getData().compare("=")) { getNextToken(); // eat the name Token *t = getNextToken(); // and the '=' return new BinaryExpression('=', new VarExpression(symbolName->getData()), parseExpression()); } else if(!next->getData().compare("|")) { return parseFunctionDefinition(); } else if(isBuiltinTypeName(next->getData())) { getNextToken(); return new VarExpression(symbolName->getData()); } return NULL; }
void TreeWalker::parseDeclaration(DeclarationAST *node) { switch (node->nodeType()) { case NodeType_AccessDeclaration: parseAccessDeclaration(static_cast<AccessDeclarationAST*>(node)); break; case NodeType_LinkageSpecification: parseLinkageSpecification(static_cast<LinkageSpecificationAST*>(node)); break; case NodeType_Namespace: parseNamespace(static_cast<NamespaceAST*>(node)); break; case NodeType_NamespaceAlias: parseNamespaceAlias(static_cast<NamespaceAliasAST*>(node)); break; case NodeType_Using: parseUsing(static_cast<UsingAST*>(node)); break; case NodeType_UsingDirective: parseUsingDirective(static_cast<UsingDirectiveAST*>(node)); break; case NodeType_Typedef: parseTypedef(static_cast<TypedefAST*>(node)); break; case NodeType_TemplateDeclaration: parseTemplateDeclaration(static_cast<TemplateDeclarationAST*>(node)); break; case NodeType_SimpleDeclaration: parseSimpleDeclaration(static_cast<SimpleDeclarationAST*>(node)); break; case NodeType_FunctionDefinition: parseFunctionDefinition(static_cast<FunctionDefinitionAST*>(node)); break; default: break; } }
ClassAst *Parser::parseClassDefinition() { if (_curTokenType != tok_class) { return Error("Expected 'class'."); } next(); // eat 'class' ClassAst *classAst = new ClassAst(); if (_curTokenType != tok_identifier) { delete classAst; return Error("Expected class identifier."); } classAst->setName(_curToken->Value()); next(); // eat identifier if (_curTokenType != '{') { delete classAst; return Error("Expected '{'."); } next(); // eat '{' while (_curTokenType != '}') { bool isPublic = _curTokenType == tok_public; if (_curTokenType == tok_private || isPublic) { next(); // eat 'public' or 'private' } if (_curTokenType == tok_func) { // Member function FunctionAst *func = parseFunctionDefinition(); if (isPublic) { classAst->pushPublicFunction(func); } else { // Assume that the default member type is 'private' even when not stated. classAst->pushPrivateFunction(func); } } else if (_curTokenType == tok_var) { // Member field AstVarExpr *varExpr = dynamic_cast<AstVarExpr*>(parseVarExpression()); if (_curTokenType == ';') { next(); } if (isPublic) { classAst->pushPublicField(varExpr); } else { // Assume that the default member type is 'private' even when not stated. classAst->pushPrivateField(varExpr); } } else if (_curTokenType == tok_identifier) { // Possibly a constructor. if (_curToken->Value() != classAst->getName()) { delete classAst; return Error("Class constructor name must match the class name."); } next(); // eat identifier if (_curTokenType != '(') { return Error("Expected '('."); } next(); // eat '(' std::vector<std::pair<std::string, AstTypeNode*>> args; while (_curTokenType != ')') { // this while loop short circuits zero parameter functions if (_curTokenType != tok_identifier) { return Error("Expected identifier in parameter list."); } std::string argName = _curToken->Value(); next(); // eat identifier if (_curTokenType != ':') { return Error("Expected ':' for parameter type definition."); } next(); // eat ':' AstTypeNode *argType = parseTypeNode(); auto pair = std::make_pair(argName, argType); args.push_back(pair); if (_curTokenType != ',') { break; } next(); // eat ',' } if (_curTokenType != ')') { return Error("Expected ')'."); } next(); // eat ')' if (_curTokenType != '{') { return Error("Expected '{'."); } next(); // eat '{' std::vector<IAstExpression*> functionBody; while (_curTokenType != '}') { IAstExpression *blockExpr = parseBlockExpression(); if (blockExpr == nullptr) { return Error("Unexpected token."); } functionBody.push_back(blockExpr); } next(); // eat '}' PrototypeAst *proto = new PrototypeAst(classAst->getName(), nullptr, args, false, _curToken->Line(), _curToken->Column()); FunctionAst *ctor = new FunctionAst(proto, functionBody, _curToken->Line(), _curToken->Column()); classAst->setConstructor(ctor); } } if (_curTokenType != '}') { delete classAst; return Error("Expected '}'."); } next(); // eat '}' return classAst; }