]> git.tdb.fi Git - libs/gl.git/blobdiff - source/programcompiler.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / programcompiler.cpp
diff --git a/source/programcompiler.cpp b/source/programcompiler.cpp
deleted file mode 100644 (file)
index 1da80ba..0000000
+++ /dev/null
@@ -1,292 +0,0 @@
-#include <msp/strings/format.h>
-#include <msp/strings/utils.h>
-#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);
-       process();
-}
-
-void ProgramCompiler::compile(IO::Base &io)
-{
-       module = &parser.parse(io);
-       process();
-}
-
-void ProgramCompiler::add_shaders(Program &program)
-{
-       if(!module)
-               throw invalid_operation("ProgramCompiler::add_shaders");
-
-       string head = "#version 150\n";
-       if(module->vertex_context.present)
-               program.attach_shader_owned(new VertexShader(head+format_context(module->vertex_context)));
-       if(module->geometry_context.present)
-               program.attach_shader_owned(new GeometryShader(head+format_context(module->geometry_context)));
-       if(module->fragment_context.present)
-               program.attach_shader_owned(new FragmentShader(head+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");
-}
-
-void ProgramCompiler::process()
-{
-       if(module->vertex_context.present)
-               process(module->vertex_context);
-       if(module->geometry_context.present)
-               process(module->geometry_context);
-       if(module->fragment_context.present)
-               process(module->fragment_context);
-}
-
-void ProgramCompiler::process(Context &context)
-{
-       inject_block(context.content, module->global_context.content);
-}
-
-void ProgramCompiler::inject_block(Block &target, const Block &source)
-{
-       list<NodePtr<Node> >::iterator insert_point = target.body.begin();
-       for(list<NodePtr<Node> >::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
-               target.body.insert(insert_point, (*i)->clone());
-}
-
-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<NodePtr<Expression> >::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(list<NodePtr<Node> >::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<Layout::Qualifier>::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<NodePtr<VariableDeclaration> >::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