module(0)
{ }
+ProgramCompiler::~ProgramCompiler()
+{
+ delete module;
+}
+
void ProgramCompiler::compile(const string &source, const string &src_name)
{
resources = 0;
- module = &parser.parse(source, src_name);
+ delete module;
+ module = new Module();
+ ProgramParser parser;
+ imported_names.insert(src_name);
+ append_module(parser.parse(source, src_name));
process();
}
void ProgramCompiler::compile(IO::Base &io, Resources *res, const string &src_name)
{
resources = res;
- module = &parser.parse(io, src_name);
+ delete module;
+ module = new Module();
+ ProgramParser parser;
+ imported_names.insert(src_name);
+ append_module(parser.parse(io, src_name));
process();
}
return 0;
}
-void ProgramCompiler::process()
+void ProgramCompiler::append_module(ProgramSyntax::Module &mod)
{
- list<Import *> imports = apply<NodeGatherer<Import> >(module->shared);
- for(list<Import *>::iterator i=imports.end(); i!=imports.begin(); )
- import((*--i)->module);
- apply<NodeRemover>(module->shared, set<Node *>(imports.begin(), imports.end()));
+ list<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
+ for(list<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
+ import((*i)->module);
+ apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
+
+ append_stage(mod.shared);
+ for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
+ append_stage(*i);
+}
+
+void ProgramCompiler::append_stage(Stage &stage)
+{
+ Stage *target = 0;
+ if(stage.type==SHARED)
+ target = &module->shared;
+ else
+ {
+ list<Stage>::iterator i;
+ for(i=module->stages.begin(); (i!=module->stages.end() && i->type<stage.type); ++i) ;
+ if(i==module->stages.end() || i->type>stage.type)
+ {
+ list<Stage>::iterator j = module->stages.insert(i, stage.type);
+ if(i!=module->stages.end())
+ i->previous = &*j;
+ i = j;
+ if(i!=module->stages.begin())
+ i->previous = &*--j;
+ }
+
+ target = &*i;
+ }
+
+ for(NodeList<Node>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
+ target->content.body.push_back(*i);
+ apply<DeclarationCombiner>(*target);
+}
+void ProgramCompiler::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(); )
void ProgramCompiler::import(const string &name)
{
string fn = name+".glsl";
+ if(imported_names.count(fn))
+ return;
+ imported_names.insert(fn);
+
RefPtr<IO::Seekable> io = (resources ? resources->open_raw(fn) : Resources::get_builtins().open(fn));
if(!io)
throw runtime_error(format("module %s not found", name));
ProgramParser import_parser;
- Module &imported_module = import_parser.parse(*io, fn);
-
- inject_block(module->shared.content, imported_module.shared.content);
- apply<DeclarationCombiner>(module->shared);
- for(list<Stage>::iterator i=imported_module.stages.begin(); i!=imported_module.stages.end(); ++i)
- {
- list<Stage>::iterator j;
- for(j=module->stages.begin(); (j!=module->stages.end() && j->type<i->type); ++j) ;
- if(j==module->stages.end() || j->type>i->type)
- {
- j = module->stages.insert(j, *i);
- list<Stage>::iterator k = j;
- if(++k!=module->stages.end())
- k->previous = &*j;
- if(j!=module->stages.begin())
- j->previous = &*--(k=j);
- }
- else
- {
- inject_block(j->content, i->content);
- apply<DeclarationCombiner>(*j);
- }
- }
+ append_module(import_parser.parse(*io, fn));
}
void ProgramCompiler::generate(Stage &stage)
};
Resources *resources;
- ProgramParser parser;
ProgramSyntax::Module *module;
+ std::set<std::string> imported_names;
public:
ProgramCompiler();
+ ~ProgramCompiler();
void compile(const std::string &, const std::string & = "<string>");
void compile(IO::Base &, Resources * = 0, const std::string & = "<file>");
static ProgramSyntax::Module *create_builtins_module();
static ProgramSyntax::Module &get_builtins_module();
static ProgramSyntax::Stage *get_builtins(ProgramSyntax::StageType);
+ void append_module(ProgramSyntax::Module &);
+ void append_stage(ProgramSyntax::Stage &);
void process();
void import(const std::string &);
void generate(ProgramSyntax::Stage &);