X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;ds=sidebyside;f=source%2Fprogramcompiler.cpp;h=f30ccfa6233ad6b6dc8115488210c7f1dbd4c7a5;hb=HEAD;hp=1914831289a9dde32c77892cb3bfdc5411fd4900;hpb=73ce62f3b9c2bbfc1e655a9df343389a733dc795;p=libs%2Fgl.git diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp deleted file mode 100644 index 19148312..00000000 --- a/source/programcompiler.cpp +++ /dev/null @@ -1,268 +0,0 @@ -#include -#include -#include "error.h" -#include "program.h" -#include "programcompiler.h" -#include "shader.h" - -using namespace std; - -namespace Msp { -namespace GL { - -using namespace ProgramSyntax; - -ProgramCompiler::ProgramCompiler(): - module(0) -{ } - -void ProgramCompiler::compile(const string &source) -{ - module = &parser.parse(source); -} - -void ProgramCompiler::compile(IO::Base &io) -{ - module = &parser.parse(io); -} - -void ProgramCompiler::add_shaders(Program &program) -{ - if(!module) - throw invalid_operation("ProgramCompiler::add_shaders"); - - string global_source = "#version 150\n"+format_context(module->global_context); - if(module->vertex_context.present) - program.attach_shader_owned(new VertexShader(global_source+"\n"+format_context(module->vertex_context))); - if(module->geometry_context.present) - program.attach_shader_owned(new GeometryShader(global_source+"\n"+format_context(module->geometry_context))); - if(module->fragment_context.present) - program.attach_shader_owned(new FragmentShader(global_source+"\n"+format_context(module->fragment_context))); - - program.bind_attribute(VERTEX4, "vertex"); - program.bind_attribute(NORMAL3, "normal"); - program.bind_attribute(COLOR4_FLOAT, "color"); - program.bind_attribute(TEXCOORD4, "texcoord"); -} - -string ProgramCompiler::format_context(Context &context) -{ - Formatter formatter; - context.content.visit(formatter); - return formatter.formatted; -} - - -ProgramCompiler::Formatter::Formatter(): - indent(0), - parameter_list(false), - else_if(false) -{ } - -void ProgramCompiler::Formatter::visit(Literal &literal) -{ - formatted += literal.token; -} - -void ProgramCompiler::Formatter::visit(ParenthesizedExpression &parexpr) -{ - formatted += '('; - parexpr.expression->visit(*this); - formatted += ')'; -} - -void ProgramCompiler::Formatter::visit(VariableReference &var) -{ - formatted += var.name; -} - -void ProgramCompiler::Formatter::visit(MemberAccess &memacc) -{ - memacc.left->visit(*this); - formatted += format(".%s", memacc.member); -} - -void ProgramCompiler::Formatter::visit(UnaryExpression &unary) -{ - if(unary.prefix) - formatted += unary.oper; - unary.expression->visit(*this); - if(!unary.prefix) - formatted += unary.oper; -} - -void ProgramCompiler::Formatter::visit(BinaryExpression &binary) -{ - binary.left->visit(*this); - if(binary.assignment) - formatted += format(" %s ", binary.oper); - else - formatted += binary.oper; - binary.right->visit(*this); - formatted += binary.after; -} - -void ProgramCompiler::Formatter::visit(FunctionCall &call) -{ - formatted += format("%s(", call.name); - for(vector >::iterator i=call.arguments.begin(); i!=call.arguments.end(); ++i) - { - if(i!=call.arguments.begin()) - formatted += ", "; - (*i)->visit(*this); - } - formatted += ')'; -} - -void ProgramCompiler::Formatter::visit(ExpressionStatement &expr) -{ - expr.expression->visit(*this); - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(Block &block) -{ - if(block.use_braces) - { - if(else_if) - { - formatted += '\n'; - else_if = false; - } - formatted += format("%s{\n", string(indent*2, ' ')); - } - - bool change_indent = (!formatted.empty() && !else_if); - indent += change_indent; - string spaces(indent*2, ' '); - for(vector >::iterator i=block.body.begin(); i!=block.body.end(); ++i) - { - if(i!=block.body.begin()) - formatted += '\n'; - if(!else_if) - formatted += spaces; - (*i)->visit(*this); - } - indent -= change_indent; - - if(block.use_braces) - formatted += format("\n%s}", string(indent*2, ' ')); -} - -void ProgramCompiler::Formatter::visit(Layout &layout) -{ - formatted += "layout("; - for(vector::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i) - { - if(i!=layout.qualifiers.begin()) - formatted += ", "; - formatted += i->identifier; - if(!i->value.empty()) - formatted += format("=%s", i->value); - } - formatted += format(") %s;", layout.interface); -} - -void ProgramCompiler::Formatter::visit(StructDeclaration &strct) -{ - formatted += format("struct %s\n", strct.name); - strct.members.visit(*this); - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(VariableDeclaration &var) -{ - if(var.constant) - formatted += "const "; - if(!var.sampling.empty()) - formatted += format("%s ", var.sampling); - if(!var.interface.empty()) - formatted += format("%s ", var.interface); - formatted += format("%s %s", var.type, var.name); - if(var.array) - { - formatted += '['; - if(var.array_size) - var.array_size->visit(*this); - formatted += ']'; - } - if(var.init_expression) - { - formatted += " = "; - var.init_expression->visit(*this); - } - if(!parameter_list) - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(InterfaceBlock &iface) -{ - formatted += format("%s %s\n", iface.interface, iface.name); - iface.members.visit(*this); - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(FunctionDeclaration &func) -{ - formatted += format("%s %s(", func.return_type, func.name); - parameter_list = true; - for(vector >::iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) - { - if(i!=func.parameters.begin()) - formatted += ", "; - (*i)->visit(*this); - } - parameter_list = false; - formatted += ')'; - if(func.definition) - { - formatted += '\n'; - func.body.visit(*this); - } - else - formatted += ';'; -} - -void ProgramCompiler::Formatter::visit(Conditional &cond) -{ - if(else_if) - { - formatted += ' '; - else_if = false; - } - - formatted += "if("; - cond.condition->visit(*this); - formatted += ")\n"; - - cond.body.visit(*this); - if(!cond.else_body.body.empty()) - { - formatted += format("\n%selse", string(indent*2, ' ')); - else_if = true; - cond.else_body.visit(*this); - else_if = false; - } -} - -void ProgramCompiler::Formatter::visit(Iteration &iter) -{ - formatted += "for("; - iter.init_statement->visit(*this); - formatted += ' '; - iter.condition->visit(*this); - formatted += "; "; - iter.loop_expression->visit(*this); - formatted += ")\n"; - iter.body.visit(*this); -} - -void ProgramCompiler::Formatter::visit(Return &ret) -{ - formatted += "return "; - ret.expression->visit(*this); - formatted += ';'; -} - -} // namespace GL -} // namespace Msp