#include "generate.h"
#include "optimize.h"
#include "output.h"
+#include "resources.h"
#include "shader.h"
#undef interface
namespace SL {
Compiler::Compiler():
- resources(0),
module(0)
{ }
delete module;
}
-void Compiler::compile(const string &source, const string &src_name)
+void Compiler::clear()
{
- resources = 0;
delete module;
module = new Module();
+ imported_names.clear();
+}
+
+void Compiler::set_source(const string &source, const string &src_name)
+{
+ clear();
Parser parser;
imported_names.push_back(src_name);
- append_module(parser.parse(source, src_name, 1));
- process();
+ append_module(parser.parse(source, src_name, 1), 0);
}
-void Compiler::compile(IO::Base &io, Resources *res, const string &src_name)
+void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
{
- resources = res;
- delete module;
- module = new Module();
+ clear();
Parser parser;
imported_names.push_back(src_name);
- append_module(parser.parse(io, src_name, 1));
- process();
+ append_module(parser.parse(io, src_name, 1), res);
}
-void Compiler::compile(IO::Base &io, const string &src_name)
+void Compiler::load_source(IO::Base &io, const string &src_name)
{
- compile(io, 0, src_name);
+ load_source(io, 0, src_name);
+}
+
+void Compiler::compile()
+{
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+ generate(*i);
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
+ {
+ if(optimize(*i))
+ i = module->stages.begin();
+ else
+ ++i;
+ }
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+ finalize(*i);
}
void Compiler::add_shaders(Program &program)
}
}
-void Compiler::append_module(Module &mod)
+void Compiler::append_module(Module &mod, DataFile::Collection *res)
{
vector<Import *> imports = NodeGatherer<Import>().apply(mod.shared);
for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
- import((*i)->module);
+ import(res, (*i)->module);
NodeRemover(set<Node *>(imports.begin(), imports.end())).apply(mod.shared);
append_stage(mod.shared);
DeclarationCombiner().apply(*target);
}
-void Compiler::process()
-{
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- generate(*i);
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
- {
- if(optimize(*i))
- i = module->stages.begin();
- else
- ++i;
- }
- for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
- finalize(*i);
-}
-
-void Compiler::import(const string &name)
+void Compiler::import(DataFile::Collection *resources, const string &name)
{
string fn = name+".glsl";
if(find(imported_names, fn)!=imported_names.end())
if(!io)
throw runtime_error(format("module %s not found", name));
Parser import_parser;
- append_module(import_parser.parse(*io, fn, imported_names.size()));
+ append_module(import_parser.parse(*io, fn, imported_names.size()), resources);
}
void Compiler::generate(Stage &stage)
#include <vector>
#include "parser.h"
#include "program.h"
-#include "resources.h"
#include "syntax.h"
namespace Msp {
class Compiler
{
private:
- Resources *resources;
Module *module;
std::vector<std::string> imported_names;
Compiler();
~Compiler();
- void compile(const std::string &, const std::string & = "<string>");
- void compile(IO::Base &, Resources * = 0, const std::string & = "<file>");
- void compile(IO::Base &, const std::string &);
+private:
+ void clear();
+public:
+ void set_source(const std::string &, const std::string & = "<string>");
+ void load_source(IO::Base &, DataFile::Collection * = 0, const std::string & = "<file>");
+ void load_source(IO::Base &, const std::string &);
+ void compile();
void add_shaders(Program &);
private:
- void append_module(Module &);
+ void append_module(Module &, DataFile::Collection *);
void append_stage(Stage &);
- void process();
- void import(const std::string &);
+ void import(DataFile::Collection *, const std::string &);
void generate(Stage &);
bool optimize(Stage &);
void finalize(Stage &);