WhileExprAST *Parser::handleWhile() { ExprAST *expr, *whileBlock = nullptr; int whileLine = currentToken.line; getNextToken(); if (currentToken.type != TOK_OPARENTHESES) { return ErrorW("Expecting '(' for 'while' loop", whileLine); } getNextToken(); expr = handleExpression(); if (currentToken.type != TOK_CPARENTHESES) { return ErrorW("Expecting ')' for 'while' loop", whileLine); } getNextToken(); if(currentToken.type!=TOK_OBRACES) { return ErrorW("Expecting '{' for 'while' loop", whileLine); } getNextToken(); whileBlock = handleExpression(); return new WhileExprAST(expr,whileBlock); }
ExprAST *Parser::handleIdentifierExpression() { std::string identifier = currentToken.value; getNextToken(); if(currentToken.type != TOK_OPARENTHESES) { auto var = _variables[identifier]; return new IdentifierExprAST(var); } getNextToken(); std::vector<ExprAST *> Args; if(currentToken.type!=TOK_CPARENTHESES){ while(1){ if(auto Arg = handleExpression()) Args.push_back(Arg); else return nullptr; if(currentToken.type==TOK_CPARENTHESES) break; getNextToken(); } } return new CallExprAST(identifier); }
ExprAST *Parser::handleReturn() { if(currentToken.type == TOK_RETURN) getNextToken(); return new ReturnExprAST(handleExpression()); }
ExprAST *Parser::handlePrintf() { getNextToken(); if(currentToken.type==TOK_OPARENTHESES){ getNextToken(); return new PrintExprAST(handleExpression()); } return nullptr; }
IfExprAST *Parser::handleIf() { ExprAST *Cond, *Then, *Else = nullptr; int ifLine = currentToken.line; getNextToken(); if(currentToken.type!=TOK_OPARENTHESES){ printf("Exp ("); parsed = false; } getNextToken(); Cond = handleExpression(); if(currentToken.type!=TOK_CPARENTHESES){ return ErrorI("Expecting ) for 'if' statement", ifLine); } getNextToken(); if(currentToken.type!=TOK_OBRACES) { return ErrorI("Expecting { for If statement", ifLine); } getNextToken(); Then = handleExpression(); getNextToken(); if(currentToken.type==TOK_ELSE) { getNextToken(); if (currentToken.type == TOK_OBRACES) { getNextToken(); Else = handleExpression(); } getNextToken(); } return new IfExprAST(Cond,Then,Else); }
ExprAST *Parser::handleParenthesesExpression() { ExprAST *expr = nullptr; getNextToken(); expr = handleExpression(); if(currentToken.type!=TOK_CPARENTHESES){ return Error("missing )", currentToken.line); } getNextToken(); return expr; }
void Parser::parse() { getNextToken(); while(currentToken.type != TOK_EOF){ switch (currentToken.type){ case TOK_CBRACES: getNextToken(); break; default: _expressions.push_back(handleExpression()); getNextToken(); break; } } }
FunctionAST *Parser::handleFunction(std::string identifier, std::string type) { getNextToken(); PrototypeAST *prototype = handlePrototype(identifier,type); ExprAST *body = nullptr; getNextToken(); body = handleExpression(); return new FunctionAST(prototype, body); }
void handleArray(string line){ if(contains(line, "<callArray>")){ if (contains(getNextLine(), "<identifier>")) { string id = getCurrLine(); handleExpression(getNextLine());//looks inside [...] handleIdentifier(id);//pushes array name on stack writeArithmetic("+");//offset array //writePop("pointer", 1); //set point to correct place in memory // writePush("that", 0); //place array[offset] on stack while ( !contains(getCurrLine(), "</callArray>")) { incFilePtr(); } }else{ error("expected to get array name <identifier> at this point"); } }else{ error("expected callArray"); } }