Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom filestream #11

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -141,7 +140,7 @@ int main(int argc, char *argv[]) {
wgc.setLookupDirectories(lookupDirs);

for(const std::string &filename: files)
wgc.addSource(std::make_shared<WGLSourceFile>(filename));
wgc.addFile(filename);

wgc.compile();
exports = wgc.construct(wgapi);
Expand Down
1 change: 0 additions & 1 deletion src/woglac/source/wglsource.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions src/woglac/source/wglsource.h

This file was deleted.

15 changes: 0 additions & 15 deletions src/woglac/source/wglsourcebuffer.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/woglac/source/wglsourcebuffer.h

This file was deleted.

34 changes: 0 additions & 34 deletions src/woglac/source/wglsourcefile.cpp

This file was deleted.

28 changes: 0 additions & 28 deletions src/woglac/source/wglsourcefile.h

This file was deleted.

3 changes: 2 additions & 1 deletion src/woglac/supp/wglimplementationpass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
29 changes: 24 additions & 5 deletions src/woglac/wglcompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,27 @@ class ANTLRErrorHandler : public antlr4::BaseErrorListener {
WGLCompiler::WGLCompiler() {
context_ = std::make_unique<WGLContext>();
context_->compiler = this;

openSteamFunction_ = [this](const std::string &filename, antlr4::ParserRuleContext *ctx) -> std::unique_ptr<std::istream> {
std::string file = lookupFile(filename, ctx);

auto f = std::make_unique<std::ifstream>();
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() {
modules_.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) {
Expand All @@ -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<std::istream> 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<WGLModule>();
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()));
Expand All @@ -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());
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/woglac/wglcompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

#include <vector>
#include <string>
#include <functional>

#include "pch.h"

#include "woglac/source/wglsourcefile.h"

// Woglac language parser and compiler, outputs a
class WGLCompiler {

public:
WGLCompiler();

Expand All @@ -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<std::istream> getFileStream(const std::string &filename, antlr4::ParserRuleContext *ctx);

inline void setStreamFunction(const std::function<std::unique_ptr<std::istream>(const std::string &filename, antlr4::ParserRuleContext *ctx)>& function) { openSteamFunction_ = function; }
public:
void compile();

Expand All @@ -33,9 +34,10 @@ class WGLCompiler {
std::unordered_map<std::string, WGA_Value*> construct(WorldGenAPI &api);

private:
std::vector<WGLSourcePtr> sources_;
std::vector<std::string> files_;
std::shared_ptr<WGLContext> context_;
std::vector<std::string> lookupDirectories_;
std::function<std::unique_ptr<std::istream>(const std::string &filename, antlr4::ParserRuleContext *ctx)> openSteamFunction_;

private:
std::vector<std::shared_ptr<WGLModule>> modules_;
Expand Down
22 changes: 5 additions & 17 deletions src/worldgen/util/voxparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,26 @@ T readPrimitive(std::basic_istream<char> &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<char> &stream) {
void VOXParser::parseData(std::unique_ptr<std::istream> 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<uint32_t>(stream);
const auto fileVersion = readPrimitive<uint32_t>(*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())
Expand Down
3 changes: 1 addition & 2 deletions src/worldgen/util/voxparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class VOXParser {
};

public:
void parseFile(const std::string &filePath);
void parseData(std::basic_istream<char> &stream);
void parseData(std::unique_ptr<std::istream> stream);

public:
inline const auto &voxels() const {
Expand Down