diff --git a/ADVect/ADVect.cpp b/ADVect/ADVect.cpp index 281438e..9bd3ce1 100644 --- a/ADVect/ADVect.cpp +++ b/ADVect/ADVect.cpp @@ -2,6 +2,7 @@ #include "Environment.h" #include "Graphics.h" #include "Track.h" +#include "Compiler.h" #include #include @@ -14,6 +15,8 @@ #include #include +#include + #include namespace { @@ -305,9 +308,16 @@ int main(int argc, char* argv[]) { return EXIT_FAILURE; } + //auto a = NVL::Compiler::Compile(SCENES, 0); + //auto b = NVL::Compiler::Serialize(a); + + //std::istringstream c{b.str()}; + //auto d = NVL::Compiler::Deserialize(c); + if (!ADVect::Init("ADV Test", SCENES)) return EXIT_FAILURE; ADVect::Run(); ADVect::Shutdown(); + return EXIT_SUCCESS; } diff --git a/ADVect/Graphics.cpp b/ADVect/Graphics.cpp index 3fb0e39..d0c2304 100644 --- a/ADVect/Graphics.cpp +++ b/ADVect/Graphics.cpp @@ -149,7 +149,7 @@ namespace { } inline bgfx::ProgramHandle load_shader_program(const std::string& shader) { - return bgfx::createProgram(loadShader(shader + ".vert.bin"), loadShader(shader + ".frag.bin"), true); + return bgfx::createProgram(loadShader(shader + ".vert.bin"), loadShader(shader + ".frag.bin"), false); } std::optional get_image_texture(const std::string& file) { @@ -253,7 +253,7 @@ namespace ADVect::Graphics { } bgfx::destroy(a_program); - bgfx::destroy(img_program); + //bgfx::destroy(img_program); bgfx::destroy(imga_program); bgfx::destroy(ibh); diff --git a/NVL/CMakeLists.txt b/NVL/CMakeLists.txt index 798c729..bac0b0e 100644 --- a/NVL/CMakeLists.txt +++ b/NVL/CMakeLists.txt @@ -5,7 +5,7 @@ cmake_minimum_required (VERSION 3.8) project (NVL) include_directories("include") -add_library (NVL STATIC "Parser.cpp" "Environment.cpp" "Environment.h" "Common.h" "Compiler.cpp" "Compiler.h") +add_library (NVL STATIC "Parser.cpp" "Environment.cpp" "Environment.h" "Common.h" "Compiler.cpp" "Compiler.h" "Director.h") # add_executable (NVL "NouVeL.cpp" "Parser.cpp" "Environment.cpp" "Environment.h" "Common.h" ) # TODO: Add tests and install targets if needed. diff --git a/NVL/Compiler.cpp b/NVL/Compiler.cpp index 757a0a1..64b07b8 100644 --- a/NVL/Compiler.cpp +++ b/NVL/Compiler.cpp @@ -1,28 +1,174 @@ #include "Compiler.h" -#include "Parser.h" -#include "Environment.h" -namespace NVL::Compiler { - struct _DirectedGraphNode { - Vector<_DirectedGraphNode*> reached_by; +namespace { + template + void SerializeDirect(std::ostringstream& s, const T& v) { + s.write(reinterpret_cast(&v), sizeof v); + } - Vector>> sequence; - }; - void Compile(Vector& scenes, i32 entry_scene_index) { - _DirectedGraphNode n{}; - Vector procs_used{}; + void SerializeString(std::ostringstream& s, const NVL::String& st) { + u16 v = st.size() * sizeof NVL::Char; + s.write(reinterpret_cast(&v), sizeof v); + s.write(reinterpret_cast(st.c_str()), v); + } - for (auto& c : scenes[entry_scene_index].get()) { - Environment::Variable f = Environment::Eval(c[0]); - Vector s{}; - u32 i = 1; - while (i < c.size()) { - s.push_back(Environment::Eval(c[i++])); + void SerializeVariable(std::ostringstream& s, const NVL::Environment::Variable& v) { + using NVL::Environment::Type; + SerializeDirect(s, v.type); + switch (v.type) { + case Type::Array: + { + const auto& e = std::get>(v.value); + SerializeDirect(s, static_cast(e.size())); + for (const auto& x : e) { + SerializeVariable(s, x); } - + return; + } + case Type::Nil: + return; + case Type::Number: + case Type::StaticReference: + SerializeDirect(s, std::get(v.value)); + return; + case Type::String: + SerializeString(s, std::get(v.value)); + return; } } - void Serialize(_DirectedGraphNode root) { + template + void DeserializeDirect(std::istringstream& s, T& v) { + s.read(reinterpret_cast(&v), sizeof v); + } + + NVL::String DeserializeString(std::istringstream& s) { + u16 size{}; + DeserializeDirect(s, size); + u8* str = new u8[size + sizeof NVL::Char]; + s.read(reinterpret_cast(str), size); + reinterpret_cast(str)[size / sizeof NVL::Char] = u'\0'; + NVL::String st = NVL::String(reinterpret_cast(str)); + delete[] str; + return st; + } + + NVL::Environment::Variable DeserializeVariable(std::istringstream& s) { + using NVL::Environment::Type; + NVL::Environment::Type t{}; + DeserializeDirect(s, t); + switch (t) { + case Type::Array: + { + u16 size{}; + NVL::Vector vs{}; + DeserializeDirect(s, size); + for (u16 i = 0; i < size; i++) { + vs.push_back(DeserializeVariable(s)); + } + return vs; + } + case Type::Nil: + return {}; + case Type::Number: + case Type::StaticReference: + { + NVL::Number n{}; + DeserializeDirect(s, n); + return { n, t == Type::StaticReference }; + } + case Type::String: + return DeserializeString(s); + } + } +} + +namespace NVL::Compiler { + Environment::Variable StaticEval(Vector& refs, const Parse::Object& obj) { + Environment::Variable actual = Environment::Eval(obj); + if (actual.type == Environment::Type::Procedure) { + u32 proc_index = -1; // lol + const String& proc = std::get(obj.value); + for (u32 i = 0; i < refs.size(); i++) { + if (refs[i] == proc) { + proc_index = i; + break; + } + } + if (proc_index == -1) { + proc_index = refs.size(); + refs.push_back(proc); + } + return Environment::Variable(proc_index, true); + } + return actual; + } + + NVLGraph Compile(const Vector& scenes, i32 entry_scene_index) { + std::vector<_DirectedGraphNode> nodes{}; + Vector procedure_names; + + _DirectedGraphNode n{}; + for (auto& c : scenes[entry_scene_index].get()) { + Vector s{}; + + for (auto& v : c) { + s.push_back(StaticEval(procedure_names, v)); + } + + // TODO route nodes when JUMP + + n.sequence.push_back(s); + } + nodes.push_back(n); + return NVLGraph{ nodes, procedure_names }; + } + + std::ostringstream Serialize(const NVLGraph& g) { + std::ostringstream s; + + SerializeDirect(s, static_cast(g.static_refs.size())); + for (const String& r : g.static_refs) { + SerializeString(s, r); + } + SerializeDirect(s, static_cast(g.nodes.size())); + for (const _DirectedGraphNode& n : g.nodes) { + SerializeDirect(s, static_cast(n.sequence.size())); + for (const Vector& c : n.sequence) { + SerializeDirect(s, static_cast(c.size())); + for (const Environment::Variable& v : c) { + SerializeVariable(s, v); + } + } + } + return s; + } + + NVLGraph Deserialize(std::istringstream& buf) { + NVLGraph g{}; + + u16 size{}; + DeserializeDirect(buf, size); + for (u16 i = 0; i < size; i++) { + g.static_refs.push_back(DeserializeString(buf)); + } + + DeserializeDirect(buf, size); + for (u16 i = 0; i < size; i++) { + _DirectedGraphNode n{}; + u16 size2{}; + DeserializeDirect(buf, size2); + for (u16 j = 0; j < size2; j++) { + Vector cmd{}; + u16 size3{}; + DeserializeDirect(buf, size3); + for (u16 k = 0; k < size3; k++) { + cmd.push_back(DeserializeVariable(buf)); + } + n.sequence.push_back(cmd); + } + g.nodes.push_back(n); + } + return g; } } diff --git a/NVL/Compiler.h b/NVL/Compiler.h index bba4cd6..ffba05b 100644 --- a/NVL/Compiler.h +++ b/NVL/Compiler.h @@ -1 +1,25 @@ +#pragma once #include "Common.h" + +#include "Parser.h" +#include "Environment.h" + +#include + +namespace NVL::Compiler { + struct _DirectedGraphNode { + Vector> sequence; + }; + + struct NVLGraph { + Vector<_DirectedGraphNode> nodes; + Vector static_refs; + }; + + Environment::Variable StaticEval(Vector& refs, const Parse::Object& obj); + + NVLGraph Compile(const Vector& scenes, i32 entry_scene_index); + + std::ostringstream Serialize(const NVLGraph& g); + NVLGraph Deserialize(std::istringstream& buf); +} diff --git a/NVL/Director.cpp b/NVL/Director.cpp new file mode 100644 index 0000000..c46e00a --- /dev/null +++ b/NVL/Director.cpp @@ -0,0 +1,26 @@ +#include "Director.h" + +namespace NVL::Director { + Director(const Compiler::NVLGraph& g, const Environment::Environment env) : g(g), current_position(0), environment(env) { + for (const String& s : g.static_refs) + resolve.push_back(env.get(s)); + } + String& GetSceneName() { + + } + void Director::Advance() { + + } + void Director::GetBacklog() { + + } + void Director::Save() { + + } + void Director::Load() { + + } + void Director::Jump() { + + } +} diff --git a/NVL/Director.h b/NVL/Director.h new file mode 100644 index 0000000..c9013cd --- /dev/null +++ b/NVL/Director.h @@ -0,0 +1,24 @@ +#pragma once +#include "Common.h" +#include "Compiler.h" +#include "Environment.h" + +namespace NVL::Director { + class Director { + private: + u64 current_position; + const Compiler::NVLGraph& g; + const Environment::Environment& environment; + Vector resolve; + public: + bool active; + Director() = delete; + Director(const Compiler::NVLGraph& g, const Environment::Environment env); + String& GetSceneName(); + void Advance(); + void GetBacklog(); + void Save(); + void Load(); + void Jump(); + }; +} diff --git a/NVL/Environment.cpp b/NVL/Environment.cpp index bfe1c59..e21e7f6 100644 --- a/NVL/Environment.cpp +++ b/NVL/Environment.cpp @@ -10,7 +10,7 @@ namespace NVL::Environment { if (t != Type::Nil) throw std::runtime_error("Cannot create non-nil object with type alone"); } - Variable::Variable(const Number& v) : type(Type::Number), value(v), length(1) {} + Variable::Variable(const Number& v, bool is_ref) : type(is_ref ? Type::StaticReference : Type::Number), value(v), length(1) {} Variable::Variable(const String& v) : type(Type::String), value(v), length(1) {} Variable::Variable(const Vector& v) : type(Type::Array), value(v), length(v.size()) {} Variable::Variable(const std::function < Variable(Vector)>& v, u32 l) : type(Type::Procedure), value(v), length(l) {} @@ -20,6 +20,7 @@ namespace NVL::Environment { return false; switch (type) { case Type::Number: + case Type::StaticReference: // Only used with serialization return std::get(other.value) == std::get(value); case Type::String: return std::get(other.value) == std::get(value); diff --git a/NVL/Environment.h b/NVL/Environment.h index d249308..cf6ccf0 100644 --- a/NVL/Environment.h +++ b/NVL/Environment.h @@ -6,7 +6,7 @@ #include "Common.h" namespace NVL::Environment { - enum class Type { Procedure, Number, String, Array, Nil }; + enum class Type { StaticReference, Procedure, Number, String, Array, Nil }; struct Variable { Type type; @@ -16,7 +16,7 @@ namespace NVL::Environment { bool operator==(const Variable& other) const; Variable(); // Makes Nil, length 0 Variable(Type t); // Reserved for Nil - Variable(const Number& v); + Variable(const Number& v, bool is_ref = false); Variable(const String& v); Variable(const Vector& v); Variable(const std::function)>& v, u32 l);