context wrappers

This commit is contained in:
xensik
2022-03-15 12:39:04 +01:00
parent 3098dacc9e
commit 096316ea67
87 changed files with 805 additions and 309 deletions

View File

@ -20,29 +20,27 @@ void compiler::compile(const std::string& file, std::vector<std::uint8_t>& data)
{
filename_ = file;
auto prog = parse_buffer(filename_, data);
auto prog = parse_buffer(filename_, reinterpret_cast<char*>(data.data()), data.size());
compile_program(prog);
}
void compiler::read_callback(std::function<std::vector<std::uint8_t>(const std::string&)> func)
void compiler::mode(build mode)
{
read_callback_ = func;
mode_ = mode;
}
auto compiler::parse_buffer(const std::string& file, std::vector<std::uint8_t>& data) -> ast::program::ptr
auto compiler::parse_buffer(const std::string& file, char* data, size_t size) -> ast::program::ptr
{
ast::program::ptr result(nullptr);
resolver::set_reader(read_callback_);
lexer lexer(file, reinterpret_cast<char*>(data.data()), data.size());
lexer lexer(mode_, file, data, size);
parser parser(lexer, result);
if (parser.parse() || result == nullptr)
{
throw comp_error(xsk::gsc::location(&file), "An unknown error ocurred while parsing gsc file.");
throw comp_error(location(&file), "An unknown error ocurred while parsing gsc file.");
}
return result;
@ -50,8 +48,8 @@ auto compiler::parse_buffer(const std::string& file, std::vector<std::uint8_t>&
auto compiler::parse_file(const std::string& file) -> ast::program::ptr
{
auto buffer = read_callback_(file);
auto result = parse_buffer(file, buffer);
auto data = resolver::file_data(file);
auto result = parse_buffer(file, std::get<1>(data), std::get<2>(data));
return result;
}

View File

@ -24,7 +24,6 @@ class compiler : public gsc::compiler
std::vector<include_t> includes_;
std::vector<animtree_t> animtrees_;
std::unordered_map<std::string, ast::expr> constants_;
std::function<std::vector<std::uint8_t>(const std::string&)> read_callback_;
std::vector<block*> break_blks_;
std::vector<block*> continue_blks_;
bool can_break_;
@ -32,13 +31,12 @@ class compiler : public gsc::compiler
bool developer_thread_;
public:
compiler(build mode) : mode_(mode) {}
auto output() -> std::vector<function::ptr>;
void compile(const std::string& file, std::vector<std::uint8_t>& data);
void read_callback(std::function<std::vector<std::uint8_t>(const std::string&)> func);
void mode(build mode);
private:
auto parse_buffer(const std::string& file, std::vector<std::uint8_t>& data) -> ast::program::ptr;
auto parse_buffer(const std::string& file, char* data, size_t size) -> ast::program::ptr;
auto parse_file(const std::string& file) -> ast::program::ptr;
void compile_program(const ast::program::ptr& program);
void emit_include(const ast::include::ptr& include);

23
src/s1/xsk/context.cpp Normal file
View File

@ -0,0 +1,23 @@
// Copyright 2022 xensik. All rights reserved.
//
// Use of this source code is governed by a GNU GPLv3 license
// that can be found in the LICENSE file.
#include "stdafx.hpp"
#include "s1.hpp"
namespace xsk::gsc::s1
{
void context::init(build mode, read_cb_type callback)
{
compiler_.mode(mode);
resolver::init(callback);
}
void context::cleanup()
{
resolver::cleanup();
}
} // namespace xsk::gsc::s1

28
src/s1/xsk/context.hpp Normal file
View File

@ -0,0 +1,28 @@
// Copyright 2022 xensik. All rights reserved.
//
// Use of this source code is governed by a GNU GPLv3 license
// that can be found in the LICENSE file.
#pragma once
namespace xsk::gsc::s1
{
class context : public gsc::context
{
s1::assembler assembler_;
s1::disassembler disassembler_;
s1::compiler compiler_;
s1::decompiler decompiler_;
public:
void init(build mode, read_cb_type callback);
void cleanup();
auto assembler() -> gsc::assembler& { return assembler_; }
auto disassembler() -> gsc::disassembler& { return disassembler_; }
auto compiler() -> gsc::compiler& { return compiler_; }
auto decompiler() -> gsc::decompiler& { return decompiler_; }
};
} // namespace xsk::gsc::s1

View File

@ -127,8 +127,8 @@ void reader::advance()
}
}
lexer::lexer(const std::string& name, const char* data, size_t size) : indev_(false), clean_(true), loc_(location(&name)),
mode_(build::dev), header_top_(0), locs_(std::stack<location>()), readers_(std::stack<reader>())
lexer::lexer(build mode, const std::string& name, const char* data, size_t size) : indev_(false), clean_(true), loc_(location(&name)),
mode_(mode), header_top_(0), locs_(std::stack<location>()), readers_(std::stack<reader>())
{
reader_.init(data, size);
}

View File

@ -58,7 +58,7 @@ class lexer
bool clean_;
public:
lexer(const std::string& name, const char* data, size_t size);
lexer(build mode, const std::string& name, const char* data, size_t size);
auto lex() -> parser::symbol_type;
void push_header(const std::string& file);
void pop_header();

View File

@ -19,6 +19,19 @@ std::unordered_map<std::string_view, std::uint16_t> function_map_rev;
std::unordered_map<std::string_view, std::uint16_t> method_map_rev;
std::unordered_map<std::string_view, std::uint16_t> file_map_rev;
std::unordered_map<std::string_view, std::uint16_t> token_map_rev;
std::unordered_map<std::string, std::vector<std::uint8_t>> files;
read_cb_type read_callback = nullptr;
std::set<std::string> string_map;
void resolver::init(read_cb_type callback)
{
read_callback = callback;
}
void resolver::cleanup()
{
files.clear();
}
auto resolver::opcode_id(const std::string& name) -> std::uint8_t
{
@ -206,9 +219,6 @@ auto resolver::make_token(std::string_view str) -> std::string
return data;
}
std::function<std::vector<std::uint8_t>(const std::string&)> read_callback = nullptr;
std::unordered_map<std::string, std::vector<std::uint8_t>> files;
auto resolver::file_data(const std::string& name) -> std::tuple<const std::string*, char*, size_t>
{
const auto& itr = files.find(name);
@ -230,11 +240,6 @@ auto resolver::file_data(const std::string& name) -> std::tuple<const std::strin
throw error("couldn't open gsc file '" + name + "'");
}
void resolver::set_reader(std::function<std::vector<std::uint8_t>(const std::string&)> callback)
{
read_callback = callback;
}
const std::array<std::pair<std::uint8_t, const char*>, 154> opcode_list
{{
{ 0x17, "SET_NEW_LOCAL_VARIABLE_FIELD_CACHED0" },

View File

@ -11,6 +11,9 @@ namespace xsk::gsc::s1
class resolver
{
public:
static void init(read_cb_type callback);
static void cleanup();
static auto opcode_id(const std::string& name) -> std::uint8_t;
static auto opcode_name(std::uint8_t id) -> std::string;
@ -31,7 +34,6 @@ public:
static auto make_token(std::string_view str) -> std::string;
static auto file_data(const std::string& name) -> std::tuple<const std::string*, char*, size_t>;
static void set_reader(std::function<std::vector<std::uint8_t>(const std::string&)> callback);
};
} // namespace xsk::gsc::s1

View File

@ -12,6 +12,7 @@
#include "compiler.hpp"
#include "decompiler.hpp"
#include "resolver.hpp"
#include "context.hpp"
namespace xsk::gsc::s1
{