fixed destroyprogram bug AND did serialization partially
This commit is contained in:
parent
38496209c0
commit
3a40e5697d
9 changed files with 255 additions and 24 deletions
|
@ -2,6 +2,7 @@
|
|||
#include "Environment.h"
|
||||
#include "Graphics.h"
|
||||
#include "Track.h"
|
||||
#include "Compiler.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <SDL_main.h>
|
||||
|
@ -14,6 +15,8 @@
|
|||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -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<ImageTexture> 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);
|
||||
|
|
|
@ -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.
|
||||
|
|
186
NVL/Compiler.cpp
186
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;
|
||||
|
||||
Vector<std::pair<i32, Vector<Environment::Variable>>> sequence;
|
||||
};
|
||||
void Compile(Vector<Parse::Scene>& scenes, i32 entry_scene_index) {
|
||||
_DirectedGraphNode n{};
|
||||
Vector<Environment::Variable> procs_used{};
|
||||
|
||||
for (auto& c : scenes[entry_scene_index].get()) {
|
||||
Environment::Variable f = Environment::Eval(c[0]);
|
||||
Vector<Environment::Variable> s{};
|
||||
u32 i = 1;
|
||||
while (i < c.size()) {
|
||||
s.push_back(Environment::Eval(c[i++]));
|
||||
namespace {
|
||||
template <typename T>
|
||||
void SerializeDirect(std::ostringstream& s, const T& v) {
|
||||
s.write(reinterpret_cast<const char*>(&v), sizeof v);
|
||||
}
|
||||
|
||||
void SerializeString(std::ostringstream& s, const NVL::String& st) {
|
||||
u16 v = st.size() * sizeof NVL::Char;
|
||||
s.write(reinterpret_cast<const char*>(&v), sizeof v);
|
||||
s.write(reinterpret_cast<const char*>(st.c_str()), v);
|
||||
}
|
||||
}
|
||||
void Serialize(_DirectedGraphNode root) {
|
||||
|
||||
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<NVL::Vector<NVL::Environment::Variable>>(v.value);
|
||||
SerializeDirect(s, static_cast<u16>(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<NVL::Number>(v.value));
|
||||
return;
|
||||
case Type::String:
|
||||
SerializeString(s, std::get<NVL::String>(v.value));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void DeserializeDirect(std::istringstream& s, T& v) {
|
||||
s.read(reinterpret_cast<char*>(&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<char*>(str), size);
|
||||
reinterpret_cast<NVL::Char*>(str)[size / sizeof NVL::Char] = u'\0';
|
||||
NVL::String st = NVL::String(reinterpret_cast<NVL::Char*>(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<NVL::Environment::Variable> 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<String>& 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<String>(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<Parse::Scene>& scenes, i32 entry_scene_index) {
|
||||
std::vector<_DirectedGraphNode> nodes{};
|
||||
Vector<String> procedure_names;
|
||||
|
||||
_DirectedGraphNode n{};
|
||||
for (auto& c : scenes[entry_scene_index].get()) {
|
||||
Vector<Environment::Variable> 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<u16>(g.static_refs.size()));
|
||||
for (const String& r : g.static_refs) {
|
||||
SerializeString(s, r);
|
||||
}
|
||||
SerializeDirect(s, static_cast<u16>(g.nodes.size()));
|
||||
for (const _DirectedGraphNode& n : g.nodes) {
|
||||
SerializeDirect(s, static_cast<u16>(n.sequence.size()));
|
||||
for (const Vector<Environment::Variable>& c : n.sequence) {
|
||||
SerializeDirect(s, static_cast<u16>(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<Environment::Variable> 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1 +1,25 @@
|
|||
#pragma once
|
||||
#include "Common.h"
|
||||
|
||||
#include "Parser.h"
|
||||
#include "Environment.h"
|
||||
|
||||
#include <sstream>
|
||||
|
||||
namespace NVL::Compiler {
|
||||
struct _DirectedGraphNode {
|
||||
Vector<Vector<Environment::Variable>> sequence;
|
||||
};
|
||||
|
||||
struct NVLGraph {
|
||||
Vector<_DirectedGraphNode> nodes;
|
||||
Vector<String> static_refs;
|
||||
};
|
||||
|
||||
Environment::Variable StaticEval(Vector<String>& refs, const Parse::Object& obj);
|
||||
|
||||
NVLGraph Compile(const Vector<Parse::Scene>& scenes, i32 entry_scene_index);
|
||||
|
||||
std::ostringstream Serialize(const NVLGraph& g);
|
||||
NVLGraph Deserialize(std::istringstream& buf);
|
||||
}
|
||||
|
|
26
NVL/Director.cpp
Normal file
26
NVL/Director.cpp
Normal file
|
@ -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() {
|
||||
|
||||
}
|
||||
}
|
24
NVL/Director.h
Normal file
24
NVL/Director.h
Normal file
|
@ -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<Environment::Variable> 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();
|
||||
};
|
||||
}
|
|
@ -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<Variable>& v) : type(Type::Array), value(v), length(v.size()) {}
|
||||
Variable::Variable(const std::function < Variable(Vector<Variable>)>& 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<Number>(other.value) == std::get<Number>(value);
|
||||
case Type::String:
|
||||
return std::get<String>(other.value) == std::get<String>(value);
|
||||
|
|
|
@ -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<Variable>& v);
|
||||
Variable(const std::function<Variable(Vector<Variable>)>& v, u32 l);
|
||||
|
|
Loading…
Add table
Reference in a new issue