X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fprogramparser.cpp;h=f003a835b56b0352e7abe2387bcaff24792d87ce;hb=2b073e0a3808f8ece4b93669395e4b812214cf5d;hp=58649ec0d77cfb189aa352b003f65e6637109231;hpb=961715848c111907b5f443c5b545a429b40583e6;p=libs%2Fgl.git diff --git a/source/programparser.cpp b/source/programparser.cpp index 58649ec0..f003a835 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_context = &module.global_context; + delete module; + module = new Module; + cur_stage = &module->shared; iter = source.begin(); while(1) { while(Node *statement = parse_global_declaration()) - cur_context->content.body.push_back(statement); - cur_context->present = !cur_context->content.body.empty(); + cur_stage->content.body.push_back(statement); - Context *prev_context = cur_context; parse_token(); string token = parse_token(); if(token.empty()) break; - else if(token=="global") - cur_context = &module.global_context; else if(token=="vertex") - cur_context = &module.vertex_context; + module->stages.push_back(VERTEX); else if(token=="geometry") - cur_context = &module.geometry_context; + module->stages.push_back(GEOMETRY); else if(token=="fragment") - cur_context = &module.fragment_context; + module->stages.push_back(FRAGMENT); else - throw runtime_error(format("Parse error at '%s': expected context identifier", token)); - cur_context->previous = prev_context; + throw runtime_error(format("Parse error at '%s': expected stage identifier", token)); + + 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=='/') @@ -514,7 +522,7 @@ Expression *ProgramParser::parse_expression(unsigned precedence) BinaryExpression *ProgramParser::parse_binary(Expression *left, const Operator *oper) { - RefPtr binary = new BinaryExpression; + RefPtr binary = (oper->precedence==16 ? new Assignment : new BinaryExpression); binary->left = left; binary->oper = parse_token(); if(binary->oper=="[") @@ -525,7 +533,6 @@ BinaryExpression *ProgramParser::parse_binary(Expression *left, const Operator * } else binary->right = parse_expression(oper->precedence+(oper->assoc==RIGHT_TO_LEFT)); - binary->assignment = (oper->precedence==16); return binary.release(); } @@ -605,7 +612,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 +639,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 +649,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(); } @@ -701,7 +712,7 @@ Passthrough *ProgramParser::parse_passthrough() { expect("passthrough"); RefPtr pass = new Passthrough; - if(cur_context->type==GEOMETRY) + if(cur_stage->type==GEOMETRY) { expect("["); pass->subscript = parse_expression(); @@ -715,7 +726,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(); }