1 #include <msp/strings/format.h>
2 #include <msp/strings/utils.h>
5 #include "programcompiler.h"
13 using namespace ProgramSyntax;
15 ProgramCompiler::ProgramCompiler():
19 void ProgramCompiler::compile(const string &source)
21 module = &parser.parse(source);
24 void ProgramCompiler::compile(IO::Base &io)
26 module = &parser.parse(io);
29 void ProgramCompiler::add_shaders(Program &program)
32 throw invalid_operation("ProgramCompiler::add_shaders");
34 string global_source = "#version 150\n"+format_context(module->global_context);
35 if(module->vertex_context.present)
36 program.attach_shader_owned(new VertexShader(global_source+"\n"+format_context(module->vertex_context)));
37 if(module->geometry_context.present)
38 program.attach_shader_owned(new GeometryShader(global_source+"\n"+format_context(module->geometry_context)));
39 if(module->fragment_context.present)
40 program.attach_shader_owned(new FragmentShader(global_source+"\n"+format_context(module->fragment_context)));
42 program.bind_attribute(VERTEX4, "vertex");
43 program.bind_attribute(NORMAL3, "normal");
44 program.bind_attribute(COLOR4_FLOAT, "color");
45 program.bind_attribute(TEXCOORD4, "texcoord");
48 string ProgramCompiler::format_context(Context &context)
51 context.content.visit(formatter);
52 return formatter.formatted;
56 ProgramCompiler::Formatter::Formatter():
58 parameter_list(false),
62 string ProgramCompiler::Formatter::format_expression(Expression &expr)
64 return join(expr.tokens.begin(), expr.tokens.end(), string());
67 void ProgramCompiler::Formatter::visit(ExpressionStatement &expr)
69 formatted += format("%s;", format_expression(expr.expression));
72 void ProgramCompiler::Formatter::visit(Block &block)
81 formatted += format("%s{\n", string(indent*2, ' '));
84 bool change_indent = (!formatted.empty() && !else_if);
85 indent += change_indent;
86 string spaces(indent*2, ' ');
87 for(vector<Node *>::const_iterator i=block.body.begin(); i!=block.body.end(); ++i)
89 if(i!=block.body.begin())
95 indent -= change_indent;
98 formatted += format("\n%s}", string(indent*2, ' '));
101 void ProgramCompiler::Formatter::visit(Layout &layout)
103 formatted += "layout(";
104 for(vector<Layout::Qualifier>::const_iterator i=layout.qualifiers.begin(); i!=layout.qualifiers.end(); ++i)
106 if(i!=layout.qualifiers.begin())
108 formatted += i->identifier;
109 if(!i->value.empty())
110 formatted += format("=%s", i->value);
112 formatted += format(") %s;", layout.interface);
115 void ProgramCompiler::Formatter::visit(StructDeclaration &strct)
117 formatted += format("struct %s\n", strct.name);
118 strct.members.visit(*this);
122 void ProgramCompiler::Formatter::visit(VariableDeclaration &var)
125 formatted += "const ";
126 if(!var.sampling.empty())
127 formatted += format("%s ", var.sampling);
128 if(!var.interface.empty())
129 formatted += format("%s ", var.interface);
130 formatted += format("%s %s", var.type, var.name);
132 formatted += format("[%s]", format_expression(var.array_size));
133 if(!var.init_expression.empty())
134 formatted += format(" = %s", format_expression(var.init_expression));
139 void ProgramCompiler::Formatter::visit(InterfaceBlock &iface)
141 formatted += format("%s %s\n", iface.interface, iface.name);
142 iface.members.visit(*this);
146 void ProgramCompiler::Formatter::visit(FunctionDeclaration &func)
148 formatted += format("%s %s(", func.return_type, func.name);
149 parameter_list = true;
150 for(vector<VariableDeclaration *>::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i)
152 if(i!=func.parameters.begin())
156 parameter_list = false;
161 func.body.visit(*this);
167 void ProgramCompiler::Formatter::visit(Conditional &cond)
174 formatted += format("if(%s)\n", format_expression(cond.condition));
175 cond.body.visit(*this);
176 if(!cond.else_body.body.empty())
178 formatted += format("\n%selse", string(indent*2, ' '));
180 cond.else_body.visit(*this);
185 void ProgramCompiler::Formatter::visit(Iteration &iter)
188 iter.init_statement->visit(*this);
189 formatted += format(" %s; %s)\n", format_expression(iter.condition), format_expression(iter.loop_expression));
190 iter.body.visit(*this);
193 void ProgramCompiler::Formatter::visit(Return &ret)
195 formatted += format("return %s;", format_expression(ret.expression));