X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramparser.cpp;h=11c9a9fe4060acd391b390cbb36749b82f998d6f;hb=5945ad9b63bbc55c3ed21f0c023d17f73aaac370;hp=d1826ad69e160a6b7aa30168d1daef3b02a58eb6;hpb=fd103d76d7546f7e22aefc18c090a844fc67409f;p=libs%2Fgl.git diff --git a/source/programparser.cpp b/source/programparser.cpp index d1826ad6..11c9a9fe 100644 --- a/source/programparser.cpp +++ b/source/programparser.cpp @@ -58,11 +58,20 @@ ProgramParser::Operator ProgramParser::operators[] = { { 0 }, 18, NO_OPERATOR, LEFT_TO_RIGHT } }; +ProgramParser::ProgramParser(): + module(0) +{ } + +ProgramParser::~ProgramParser() +{ + delete module; +} + Module &ProgramParser::parse(const string &s) { source = s; - parse_source(main_module); - return main_module; + parse_source(); + return *module; } Module &ProgramParser::parse(IO::Base &io) @@ -74,37 +83,37 @@ Module &ProgramParser::parse(IO::Base &io) unsigned len = io.read(buffer, sizeof(buffer)); source.append(buffer, len); } - parse_source(main_module); - return main_module; + parse_source(); + return *module; } -void ProgramParser::parse_source(Module &module) +void ProgramParser::parse_source() { - cur_module = &module; - cur_stage = &module.shared; + delete module; + module = new Module; + cur_stage = &module->shared; iter = source.begin(); while(1) { while(Node *statement = parse_global_declaration()) cur_stage->content.body.push_back(statement); - cur_stage->present = !cur_stage->content.body.empty(); - Stage *prev_stage = cur_stage; parse_token(); string token = parse_token(); if(token.empty()) break; - else if(token=="global") - cur_stage = &module.shared; else if(token=="vertex") - cur_stage = &module.vertex_stage; + module->stages.push_back(VERTEX); else if(token=="geometry") - cur_stage = &module.geometry_stage; + module->stages.push_back(GEOMETRY); else if(token=="fragment") - cur_stage = &module.fragment_stage; + module->stages.push_back(FRAGMENT); else throw runtime_error(format("Parse error at '%s': expected stage identifier", token)); - cur_stage->previous = prev_stage; + + if(cur_stage->type!=SHARED) + module->stages.back().previous = cur_stage; + cur_stage = &module->stages.back(); for(; (iter!=source.end() && *iter!='\n'); ++iter) ; } @@ -209,7 +218,6 @@ bool ProgramParser::skip_comment_and_whitespace() unsigned slashes = 0; while(iter!=source.end()) { - //IO::print("%d '%c'\n", comment, *iter); if(comment==0) { if(*iter=='/') @@ -605,7 +613,18 @@ FunctionDeclaration *ProgramParser::parse_function_declaration() func->return_type = expect_type(); func->name = expect_identifier(); - parse_function_parameter_list(*func); + expect("("); + while(peek_token()!=")") + { + if(!func->parameters.empty()) + expect(","); + + RefPtr var = new VariableDeclaration; + var->type = expect_type(); + var->name = expect_identifier(); + func->parameters.push_back(var.release()); + } + expect(")"); string token = peek_token(); if(token=="{") @@ -621,22 +640,6 @@ FunctionDeclaration *ProgramParser::parse_function_declaration() return func.release(); } -void ProgramParser::parse_function_parameter_list(FunctionDeclaration &func) -{ - expect("("); - while(peek_token()!=")") - { - if(!func.parameters.empty()) - expect(","); - - RefPtr var = new VariableDeclaration; - var->type = expect_type(); - var->name = expect_identifier(); - func.parameters.push_back(var.release()); - } - expect(")"); -} - InterfaceBlock *ProgramParser::parse_interface_block() { RefPtr iface = new InterfaceBlock; @@ -647,7 +650,16 @@ InterfaceBlock *ProgramParser::parse_interface_block() iface->name = expect_identifier(); parse_block(iface->members, true); - expect(";"); + if(!check(";")) + { + iface->instance_name = expect_identifier(); + if(check("[")) + { + iface->array = true; + expect("]"); + } + expect(";"); + } return iface.release(); } @@ -715,7 +727,8 @@ Return *ProgramParser::parse_return() { expect("return"); RefPtr ret = new Return; - ret->expression = parse_expression(); + if(peek_token()!=";") + ret->expression = parse_expression(); expect(";"); return ret.release(); }