diff --git a/src/main.cpp b/src/main.cpp index 6b96a4a..dbe3dc3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,6 @@ #include "worldgen/cpu/worldgenapi_cpu.h" #include "worldgen/cpu/supp/wga_valuewrapper_cpu.h" #include "woglac/wglcompiler.h" -#include "woglac/source/wglsourcefile.h" std::mutex stdoutMutex; @@ -141,7 +140,7 @@ int main(int argc, char *argv[]) { wgc.setLookupDirectories(lookupDirs); for(const std::string &filename: files) - wgc.addSource(std::make_shared(filename)); + wgc.addFile(filename); wgc.compile(); exports = wgc.construct(wgapi); diff --git a/src/woglac/source/wglsource.cpp b/src/woglac/source/wglsource.cpp deleted file mode 100644 index 9c980ea..0000000 --- a/src/woglac/source/wglsource.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "wglsource.h" diff --git a/src/woglac/source/wglsource.h b/src/woglac/source/wglsource.h deleted file mode 100644 index c6ab515..0000000 --- a/src/woglac/source/wglsource.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include - -class WGLSource { - -public: - virtual ~WGLSource() {} - -public: - /// Returns an open stream for the source - virtual std::unique_ptr openStream() const = 0; - - virtual std::string sourceName() const = 0; - -}; - -using WGLSourcePtr = std::shared_ptr; \ No newline at end of file diff --git a/src/woglac/source/wglsourcebuffer.cpp b/src/woglac/source/wglsourcebuffer.cpp deleted file mode 100644 index 9b2f8a1..0000000 --- a/src/woglac/source/wglsourcebuffer.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "wglsourcebuffer.h" - -#include - -WGLSourceBuffer::WGLSourceBuffer(const std::string &data) : data_(data) { - -} - -std::unique_ptr WGLSourceBuffer::openStream() const { - return std::make_unique(data_); -} - -std::string WGLSourceBuffer::sourceName() const { - return std::format("(buffer {})", reinterpret_cast(data_.data())); -} diff --git a/src/woglac/source/wglsourcebuffer.h b/src/woglac/source/wglsourcebuffer.h deleted file mode 100644 index 621a81b..0000000 --- a/src/woglac/source/wglsourcebuffer.h +++ /dev/null @@ -1,18 +0,0 @@ -#pragma once - -#include "wglsource.h" - -class WGLSourceBuffer : public WGLSource { - -public: - WGLSourceBuffer(const std::string &data); - -public: - virtual std::unique_ptr openStream() const override; - virtual std::string sourceName() const override; - -private: - const std::string data_; - -}; - diff --git a/src/woglac/source/wglsourcefile.cpp b/src/woglac/source/wglsourcefile.cpp deleted file mode 100644 index 94d213a..0000000 --- a/src/woglac/source/wglsourcefile.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "wglsourcefile.h" - -#include -#include - -#include "woglac/supp/wglerror.h" - -WGLSourceFile::WGLSourceFile() { - -} - -WGLSourceFile::WGLSourceFile(const std::string &fileName) { - setFileName(fileName); -} - -void WGLSourceFile::setFileName(const std::string &fileName) { - if(fileName_ == fileName) - return; - - fileName_ = fileName; -} - -std::unique_ptr WGLSourceFile::openStream() const { - auto r = std::make_unique(); - r->open(fileName_, std::ifstream::in); - if(!r->is_open()) - throw WGLError(std::format("Error opening file '{}'", fileName_), nullptr); - - return r; -} - -std::string WGLSourceFile::sourceName() const { - return fileName_; -} diff --git a/src/woglac/source/wglsourcefile.h b/src/woglac/source/wglsourcefile.h deleted file mode 100644 index c0d8ebb..0000000 --- a/src/woglac/source/wglsourcefile.h +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include -#include - -#include "wglsource.h" - -class WGLSourceFile : public WGLSource { - -public: - WGLSourceFile(); - WGLSourceFile(const std::string &fileName); - -public: - inline const std::string &fileName() const { - return fileName_; - } - - void setFileName(const std::string &fileName); - -public: - virtual std::unique_ptr openStream() const override; - virtual std::string sourceName() const override; - -private: - std::string fileName_; - -}; diff --git a/src/woglac/supp/wglimplementationpass.cpp b/src/woglac/supp/wglimplementationpass.cpp index b6418aa..20422c0 100644 --- a/src/woglac/supp/wglimplementationpass.cpp +++ b/src/woglac/supp/wglimplementationpass.cpp @@ -232,7 +232,8 @@ void WGLImplementationPass::enterComponentIncludeStatement(WoglacParser::Compone WGLSymbol *sym = ctx_->astSymbolMapping[ctx]; ASSERT(voxParser_.isEmpty()); - voxParser_.parseFile(ctx_->compiler->lookupFile(WGLUtils::stringLiteral(ctx->file), ctx)); + + voxParser_.parseData(ctx_->compiler->getFileStream(WGLUtils::stringLiteral(ctx->file), ctx)); currentScope_.push(sym); } diff --git a/src/woglac/wglcompiler.cpp b/src/woglac/wglcompiler.cpp index 53a1abd..3a50192 100644 --- a/src/woglac/wglcompiler.cpp +++ b/src/woglac/wglcompiler.cpp @@ -24,6 +24,18 @@ class ANTLRErrorHandler : public antlr4::BaseErrorListener { WGLCompiler::WGLCompiler() { context_ = std::make_unique(); context_->compiler = this; + + openSteamFunction_ = [this](const std::string &filename, antlr4::ParserRuleContext *ctx) -> std::unique_ptr { + std::string file = lookupFile(filename, ctx); + + auto f = std::make_unique(); + f->open(file, std::ios::in | std::ios::binary); + + if(!f->good()) + throw std::exception(std::format("Could not open VOX file '{}' for reading.", file).c_str()); + + return f; + }; } void WGLCompiler::clear() { @@ -31,8 +43,8 @@ void WGLCompiler::clear() { context_->clear(); } -void WGLCompiler::addSource(const WGLSourcePtr &file) { - sources_.push_back(file); +void WGLCompiler::addFile(const std::string &file) { + files_.push_back(file); } std::string WGLCompiler::lookupFile(const std::string &filename, antlr4::ParserRuleContext *ctx) { @@ -45,16 +57,23 @@ std::string WGLCompiler::lookupFile(const std::string &filename, antlr4::ParserR throw WGLError(std::format("Failed to lookup file '{}' in directories:\n{}", filename, iterator(lookupDirectories_).join('\n')), ctx); } +std::unique_ptr WGLCompiler::getFileStream(const std::string &filename, antlr4::ParserRuleContext *ctx) { + if (openSteamFunction_ == nullptr) + throw std::exception("Stream function not set !"); + + return openSteamFunction_(filename, ctx); +} + void WGLCompiler::compile() { clear(); try { // Parse files - for(const WGLSourcePtr &s: sources_) { + for(const std::string &s: files_) { try { auto m = std::make_shared(); - m->stream = s->openStream(); + m->stream = getFileStream(s, nullptr); m->input.reset(new antlr4::ANTLRInputStream(*m->stream)); m->lexer.reset(new WoglacLexer(m->input.get())); @@ -72,7 +91,7 @@ void WGLCompiler::compile() { modules_.push_back(m); } catch(const WGLError &e) { - throw std::exception(std::format("Error when compiling WOGLAC source '{}': {}", s->sourceName(), e.message()).c_str()); + throw std::exception(std::format("Error when compiling WOGLAC source '{}': {}", s, e.message()).c_str()); } } diff --git a/src/woglac/wglcompiler.h b/src/woglac/wglcompiler.h index ef29100..571dc53 100644 --- a/src/woglac/wglcompiler.h +++ b/src/woglac/wglcompiler.h @@ -2,14 +2,12 @@ #include #include +#include #include "pch.h" -#include "woglac/source/wglsourcefile.h" - // Woglac language parser and compiler, outputs a class WGLCompiler { - public: WGLCompiler(); @@ -20,11 +18,14 @@ class WGLCompiler { } public: - void addSource(const WGLSourcePtr &file); + void addFile(const std::string &file); // Tries to locate a specified file, throws if failed std::string lookupFile(const std::string &filename, antlr4::ParserRuleContext *ctx); + std::unique_ptr getFileStream(const std::string &filename, antlr4::ParserRuleContext *ctx); + + inline void setStreamFunction(const std::function(const std::string &filename, antlr4::ParserRuleContext *ctx)>& function) { openSteamFunction_ = function; } public: void compile(); @@ -33,9 +34,10 @@ class WGLCompiler { std::unordered_map construct(WorldGenAPI &api); private: - std::vector sources_; + std::vector files_; std::shared_ptr context_; std::vector lookupDirectories_; + std::function(const std::string &filename, antlr4::ParserRuleContext *ctx)> openSteamFunction_; private: std::vector> modules_; diff --git a/src/worldgen/util/voxparser.cpp b/src/worldgen/util/voxparser.cpp index d3097bb..d9faabd 100644 --- a/src/worldgen/util/voxparser.cpp +++ b/src/worldgen/util/voxparser.cpp @@ -15,38 +15,26 @@ T readPrimitive(std::basic_istream &stream) { return result; } -void VOXParser::parseFile(const std::string &file) { - std::ifstream f; - f.open(file, std::ios::in | std::ios::binary); - - if(!f.good()) - throw std::exception(std::format("Could not open VOX file '{}' for reading.", file).c_str()); - - fileName_ = file; - - parseData(f); -} - -void VOXParser::parseData(std::basic_istream &stream) { +void VOXParser::parseData(std::unique_ptr stream) { clear(); std::string prefix; prefix.resize(4); - stream.read(prefix.data(), prefix.size()); + stream->read(prefix.data(), prefix.size()); if(prefix != "VOX ") throw std::exception("Provided file is not of the VOX file format"); - const auto fileVersion = readPrimitive(stream); + const auto fileVersion = readPrimitive(*stream); if(fileVersion != 150 && fileVersion != 200) throw std::exception(std::format("Unsupported vox file format version ({})", fileVersion).c_str()); // Process main chunk { - const Chunk mainChunk = readChunk(stream); + const Chunk mainChunk = readChunk(*stream); if(mainChunk.name != "MAIN") throw std::exception("MAIN chunk expected"); - if(stream.peek() != EOF) + if(stream->peek() != EOF) throw std::exception("There should be nothing left after the main chunk"); if(!mainChunk.data.empty()) diff --git a/src/worldgen/util/voxparser.h b/src/worldgen/util/voxparser.h index 973ab12..81bdb93 100644 --- a/src/worldgen/util/voxparser.h +++ b/src/worldgen/util/voxparser.h @@ -14,8 +14,7 @@ class VOXParser { }; public: - void parseFile(const std::string &filePath); - void parseData(std::basic_istream &stream); + void parseData(std::unique_ptr stream); public: inline const auto &voxels() const {