SymProc* Parser::ParseProcDecl(string name, vector<string>* argNames, SymTable* argTable, bool isFunc, SymType* Type) { SymTable* locTab = new SymTable; TableStack.PushTable(locTab); table = TableStack.GetTopTable(); SymProc* proc = NULL; if (isFunc) { argTable->insert_vo(pair<string, Symbol*>("result", new SymVarParam("result", Type))); proc = new SymFunc(name, argNames, argTable, NULL, false, Type); } else proc = new SymProc(name, argNames, argTable, NULL, false); proc->print(os, false); if (mainTable->find(name) != mainTable->end()) mainTable->delete_element(name); mainTable->insert(pair<string, SymProc*> (name, proc)); ParseDecl(); locTab->MakeLocals(); proc->SetBody(ParseBlock()); proc->SetLocTab(locTab); TableStack.PopTable(); TableStack.PopTable(); table = TableStack.GetTopTable(); return proc; }
void ParseStatement() { sInt elsemod; if(CheckType()) { NewLine(); ParseDecl(); return; } switch(Token) { case TOK_PRE: Out("\n"); Match(); break; case TOK_WHILE: NewLine(); Match(); Match(TOK_OPEN); ParseAssignExpr(); Match(TOK_CLOSE); ParseCondBlock(); //ParseStatement(); break; case TOK_SWITCH: NewLine(); Match(); Match(TOK_OPEN); ParseAssignExpr(); Match(TOK_CLOSE); ParseSwitchBlock(); break; /* case TOK_CASE: NewLine(-1); Match(); Out(" "); if(Token==TOK_VALUE) { Match(); Match(TOK_COLON); } else if(Token==TOK_LABEL) { Match(); } else { Error("case label"); } break; */ case TOK_BREAK: NewLine(); Match(); Match(TOK_SEMI); break; case TOK_RETURN: NewLine(); Match(); Out(" "); if(Token!=TOK_SEMI) ParseAssignExpr(); Match(TOK_SEMI); break; case TOK_IF: NewLine(); Match(TOK_IF); Match(TOK_OPEN); ParseAssignExpr(); Match(TOK_CLOSE); ParseCondBlock(&elsemod,sFALSE); if(Token==TOK_ELSE) { NewLine(); Match(TOK_ELSE); ParseCondBlock(&elsemod,sTRUE); } else { if(Mode == 1) { NewLine(); OutF("else"); OutBOpen(); NewLine(); OutF("sLekktor_%s.Set(%d);",LekktorName,elsemod); OutBClose(); } } break; case TOK_ASM: NewLine(); Match(TOK_ASM); InlineAssembly(); if(Token==TOK_SEMI) Match(); break; case TOK_FOR: NewLine(); Match(TOK_FOR); Match(TOK_OPEN); if(Token!=TOK_SEMI) { if(CheckType()) { ParseDecl(); } else { ParseAssignExpr(); Match(TOK_SEMI); } } else Match(); if(Token!=TOK_SEMI) ParseAssignExpr(); Match(TOK_SEMI); if(Token!=TOK_CLOSE) ParseAssignExpr(); Match(TOK_CLOSE); ParseCondBlock(); break; case TOK_DO: NewLine(); Match(TOK_DO); ParseCondBlock(); NewLine(); Match(TOK_WHILE); Match(TOK_OPEN); ParseAssignExpr(); Match(TOK_CLOSE); Match(TOK_SEMI); break; case TOK_BOPEN: ParseBlock(); break; case TOK_DELETEA: case TOK_DELETE: NewLine(); Match(); Out(" "); ParseExpr(); Match(TOK_SEMI); break; case TOK_GOTO: NewLine(); Match(TOK_GOTO); Out(" "); Match(TOK_NAME); Match(TOK_SEMI); break; case TOK_SEMI: NewLine(); Match(); break; case TOK_TYPEDEF: NewLine(); Match(); Out(" "); intypedef = sTRUE; gottype = sFALSE; ParseFunctionPtrType(); intypedef = sFALSE; Out(" "); if(Token == TOK_NAME) { AddType(Value); Match(); } else if(!gottype) Error("typedef error"); Match(TOK_SEMI); break; case TOK_NAMESPACE: NewLine(); Match(); Out(" "); if(Token==TOK_NAME) // name may be omitted Match(TOK_NAME); ParseBlock(); break; case TOK_USING: NewLine(); Match(); Out(" "); if(Token==TOK_NAMESPACE) { Match(); Out(" "); } ParseScopedName(); Match(TOK_SEMI); break; case TOK_VCTRY: NewLine(); Match(); ParseStatement(); if(Token == TOK_VCEXCEPT) { NewLine(); Match(); Match(TOK_OPEN); ParseExpr(); Match(TOK_CLOSE); ParseStatement(); } break; case TOK_NAME: if(Peek() == TOK_COLON) { NewLine(); Match(); Match(); break; } // fall-through /*case TOK_LABEL: NewLine(); Match(); break;*/ default: NewLine(); ParseAssignExpr(); Match(TOK_SEMI); break; } }
void ParseType1(sInt withname) { while(Token==TOK_STATIC||Token==TOK_EXTERN||Token==TOK_AUTO||Token==TOK_REGISTER) { if(Token!=TOK_EXTERN) { Match(); Out(" "); } else { Match(); Out(" "); if(Token==TOK_VALUE) { Match(); Out(" "); if(Token==TOK_BOPEN) ParseBlock(); } } } if(Token==TOK_CONST||Token==TOK_VOLATILE) { Match(); Out(" "); } if(Token==TOK_STRUCT||Token==TOK_CLASS||Token==TOK_UNION) { Match(); Out(" "); if(Token==TOK_NAME) { AddType(Value); Match(TOK_NAME); } else { Match(TOK_TYPE); } if(Token==TOK_COLON) { Match(); if(Token==TOK_PROTECTED||Token==TOK_PRIVATE||Token==TOK_PUBLIC) { Match(); Out(" "); } /*else Match(TOK_PUBLIC); // provoke error*/ Match(TOK_TYPE); } if(Token==TOK_BOPEN) { MatchBOpen(); while(Token!=TOK_BCLOSE) { switch(Token) { case TOK_PROTECTED: case TOK_PRIVATE: case TOK_PUBLIC: Out("\n"); Match(); Match(TOK_COLON); break; case TOK_PRE: Out("\n"); Match(); break; default: NewLine(); ParseDecl(); break; } } MatchBClose(); } } else if(Token==TOK_ENUM) { Match(); Out(" "); if(Token==TOK_NAME) { AddType(Value); Match(TOK_NAME); } else { Match(TOK_TYPE); } if(Token==TOK_BOPEN) { MatchBOpen(); ParseInitialiser(); while(Token==TOK_COMMA) { Match(TOK_COMMA); ParseInitialiser(); } MatchBClose(); } } else { sInt gotspec=0; while(Token==TOK_LONG || Token==TOK_SHORT || Token==TOK_SIGNED || Token==TOK_UNSIGNED) { Match(); Out(" "); gotspec=1; } if(Token==TOK_INT||Token==TOK_FLOAT||Token==TOK_CHAR||Token==TOK_FLOAT||Token==TOK_DOUBLE||Token==TOK_TYPE) { Match(); } else if(!gotspec) Error("type error"); } if(withname) Out(" "); while(Token==TOK_MUL) Match(); if(Token==TOK_AND) Match(); if(withname==1) { ParseVarDecl(); } while(Token==TOK_SOPEN) { Match(); if(Token!=TOK_SCLOSE) ParseExpr(); Match(TOK_SCLOSE); } if(withname==2 && Token==TOK_OPEN) { Match(); ParseVarDecl(); Match(TOK_CLOSE); Match(TOK_OPEN); ParseParamList(); Match(TOK_CLOSE); } }
void Parser::ParseDeclaration() { t = sc.GetNextToken(); ParseDecl(); }