From 38496209c014861209e060c20ca958167a065344 Mon Sep 17 00:00:00 2001 From: lachrymaL Date: Fri, 7 Apr 2023 16:04:30 -0400 Subject: [PATCH] thoughts about compiler --- ADVect/ADVect.cpp | 4 ++-- NVL/CMakeLists.txt | 2 +- NVL/Common.h | 3 +++ NVL/Compiler.cpp | 28 ++++++++++++++++++++++++++++ NVL/Compiler.h | 1 + NVL/Environment.cpp | 28 ++++++++++++++-------------- NVL/Environment.h | 12 ++++++------ NVL/Parser.cpp | 34 +++++++++++++++++----------------- NVL/Parser.h | 13 ++++++------- 9 files changed, 78 insertions(+), 47 deletions(-) create mode 100644 NVL/Compiler.cpp create mode 100644 NVL/Compiler.h diff --git a/ADVect/ADVect.cpp b/ADVect/ADVect.cpp index d104f4c..281438e 100644 --- a/ADVect/ADVect.cpp +++ b/ADVect/ADVect.cpp @@ -249,8 +249,8 @@ namespace ADVect { } int main(int argc, char* argv[]) { -// std::filesystem::current_path("E:\\Archive\\Projects\\NouVeL\\ADVect\\runtime"); - std::filesystem::current_path("/Users/lachrymal/Projects/NouVeL/ADVect/runtime/"); + std::filesystem::current_path("E:\\Archive\\Projects\\NouVeL\\ADVect\\runtime"); + //std::filesystem::current_path("/Users/lachrymal/Projects/NouVeL/ADVect/runtime/"); NVL::Environment::ENVIRONMENT.enter({ { diff --git a/NVL/CMakeLists.txt b/NVL/CMakeLists.txt index f4d118a..798c729 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") +add_library (NVL STATIC "Parser.cpp" "Environment.cpp" "Environment.h" "Common.h" "Compiler.cpp" "Compiler.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/Common.h b/NVL/Common.h index 47e1869..603028d 100644 --- a/NVL/Common.h +++ b/NVL/Common.h @@ -15,6 +15,7 @@ typedef int64_t i64; typedef uint64_t u64; #include +#include #include #include @@ -24,6 +25,8 @@ namespace NVL { using Number = f32; using Char = char16_t; using String = std::u16string; + template + using Vector = std::vector; static std::wstring_convert, Char> str_converter; diff --git a/NVL/Compiler.cpp b/NVL/Compiler.cpp new file mode 100644 index 0000000..757a0a1 --- /dev/null +++ b/NVL/Compiler.cpp @@ -0,0 +1,28 @@ +#include "Compiler.h" +#include "Parser.h" +#include "Environment.h" + +namespace NVL::Compiler { + struct _DirectedGraphNode { + Vector<_DirectedGraphNode*> reached_by; + + Vector>> sequence; + }; + void Compile(Vector& scenes, i32 entry_scene_index) { + _DirectedGraphNode n{}; + Vector procs_used{}; + + 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 Serialize(_DirectedGraphNode root) { + + } +} diff --git a/NVL/Compiler.h b/NVL/Compiler.h new file mode 100644 index 0000000..bba4cd6 --- /dev/null +++ b/NVL/Compiler.h @@ -0,0 +1 @@ +#include "Common.h" diff --git a/NVL/Environment.cpp b/NVL/Environment.cpp index d6cf4b1..bfe1c59 100644 --- a/NVL/Environment.cpp +++ b/NVL/Environment.cpp @@ -12,8 +12,8 @@ namespace NVL::Environment { } Variable::Variable(const Number& v) : type(Type::Number), value(v), length(1) {} Variable::Variable(const String& v) : type(Type::String), value(v), length(1) {} - Variable::Variable(const std::vector& v) : type(Type::Array), value(v), length(v.size()) {} - Variable::Variable(const std::function < Variable(std::vector)>& v, u32 l) : type(Type::Procedure), value(v), length(l) {} + 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) {} bool Variable::operator==(const Variable& other) const { if (other.type != type) @@ -62,20 +62,20 @@ namespace NVL::Environment { return Variable(std::get(obj.value)); case Parse::Type::Array: { - std::vector v{}; - for (const auto& x : std::get>(obj.value)) + Vector v{}; + for (const auto& x : std::get>(obj.value)) v.push_back(Eval(x)); return Variable(v); } case Parse::Type::Subexpression: { - return Apply(std::get>(obj.value)); + return Apply(std::get>(obj.value)); } } } Variable Apply(const Parse::Command& c) { - std::vector args{}; + Vector args{}; Variable f = Eval(c[0]); u32 i = 1; @@ -83,7 +83,7 @@ namespace NVL::Environment { args.push_back(Eval(c[i++])); } if (f.length != -1 && f.length != i - 1) throw std::runtime_error("Function arity mismatch"); - return std::get)>>(f.value)(args); + return std::get)>>(f.value)(args); } void EvalScene(const Parse::Scene& s) { @@ -92,23 +92,23 @@ namespace NVL::Environment { } // Check MatchMarkup for more information - MarkupString UnpackMarkupVariable(const std::vector& v) { + MarkupString UnpackMarkupVariable(const Vector& v) { MarkupString ms{}; for (const Variable& segment : v) { - const auto& pair = std::get>(segment.value); + const auto& pair = std::get>(segment.value); const String s = std::get(pair[0].value); - std::vector>> efs; - for (const Variable& combination : std::get>(pair[1].value)) { + Vector>> efs; + for (const Variable& combination : std::get>(pair[1].value)) { if (combination.type == Type::String) { const String ef = std::get(combination.value); efs.push_back({ ef, {} }); } else if (combination.type == Type::Array) { - const std::vector& ef_t = std::get>(combination.value); + const Vector& ef_t = std::get>(combination.value); const String& ef = std::get(ef_t[0].value); - const std::vector& args = std::get>(ef_t[1].value); - std::vector ss{}; + const Vector& args = std::get>(ef_t[1].value); + Vector ss{}; for (const auto& s : args) { ss.push_back(std::get(s.value)); } efs.push_back({ ef, ss }); } diff --git a/NVL/Environment.h b/NVL/Environment.h index ee8598d..d249308 100644 --- a/NVL/Environment.h +++ b/NVL/Environment.h @@ -10,7 +10,7 @@ namespace NVL::Environment { struct Variable { Type type; - std::variant, std::function)>> value; + std::variant, std::function)>> value; // Most things will have length 1 (including string); this is mostly for function arity and array length u32 length; bool operator==(const Variable& other) const; @@ -18,8 +18,8 @@ namespace NVL::Environment { Variable(Type t); // Reserved for Nil Variable(const Number& v); Variable(const String& v); - Variable(const std::vector& v); - Variable(const std::function)>& v, u32 l); + Variable(const Vector& v); + Variable(const std::function)>& v, u32 l); }; class Environment { @@ -41,15 +41,15 @@ namespace NVL::Environment { struct MarkupSegment { String str; - std::vector>> efs; + Vector>> efs; }; struct MarkupString { private: mutable i32 mem_length = -1; public: - std::vector segments{}; + Vector segments{}; size_t length() const; MarkupString substr(size_t idx) const; }; - MarkupString UnpackMarkupVariable(const std::vector& v); + MarkupString UnpackMarkupVariable(const Vector& v); } diff --git a/NVL/Parser.cpp b/NVL/Parser.cpp index bcdeecd..1302758 100644 --- a/NVL/Parser.cpp +++ b/NVL/Parser.cpp @@ -125,8 +125,8 @@ namespace { return to_NVL_string(buffer.str()); } - std::vector split_string_by_lines(const String& str) { - std::vector lines; + Vector split_string_by_lines(const String& str) { + Vector lines; size_t pos = 0; size_t prev = 0; while ((pos = str.find(NEWLINE, prev)) != String::npos) { @@ -230,7 +230,7 @@ namespace { Parse::Object ParseArray(const String& f, size_t& pos, u32 layer) { SkipComments(f, pos); - std::vector array{}; + Vector array{}; array.push_back(ParseExpression(f, pos)); while (PeekToken(f, pos)[0] != ARRAY_CLOSE) { @@ -246,7 +246,7 @@ namespace { String ParseString(const String& f, size_t& pos) { SkipComments(f, pos); - std::vector discards{}; + Vector discards{}; auto start = ++pos; // skip opening quote do { if (f[pos] == QUOTE) { @@ -352,7 +352,7 @@ namespace { bool has_markup = false; // Match tags - std::vector segments; + Vector segments; String::const_iterator tags_start(s.cbegin()); while (srell::regex_search(tags_start, s.cend(), tags_match, typer)) { has_markup = true; @@ -361,13 +361,13 @@ namespace { if (!before.empty()) segments.push_back({ Parse::Type::Array, { { Parse::Type::String, before }, - { Parse::Type::Array, std::vector{} } + { Parse::Type::Array, Vector{} } }}); String inner = tags_match[2].str(); // markupped // Match markup options - std::vector effects{}; + Vector effects{}; String::const_iterator effects_start(tags_match[1].first); while (srell::regex_search(effects_start, tags_match[1].second, effects_match, effect)) { if (effects_match[3].matched) { // no params @@ -376,19 +376,19 @@ namespace { else { // no params // Comma split - std::vector args; + Vector args; String::const_iterator params_start(effects_match[2].first); while (srell::regex_search(params_start, effects_match[2].second, params_match, param)) { size_t temp = 0; args.push_back(ParseExpression(params_match[1].str() + SEPARATOR.accept[0], temp)); // PeekToken will freak out if I don't do this params_start = params_match.suffix().first; } - effects.push_back({ Parse::Type::Array, std::vector{ { Parse::Type::String, effects_match[1].str() }, { Parse::Type::Array, args } } }); + effects.push_back({ Parse::Type::Array, Vector{ { Parse::Type::String, effects_match[1].str() }, { Parse::Type::Array, args } } }); } effects_start = effects_match.suffix().first; } tags_start = tags_match.suffix().first; - segments.push_back({ Parse::Type::Array, std::vector{ { Parse::Type::String, inner }, { Parse::Type::Array, effects } } }); + segments.push_back({ Parse::Type::Array, Vector{ { Parse::Type::String, inner }, { Parse::Type::Array, effects } } }); } if (has_markup) { @@ -396,15 +396,15 @@ namespace { if (!end.empty()) segments.push_back({ Parse::Type::Array, { { Parse::Type::String, end }, - { Parse::Type::Array, std::vector{} } + { Parse::Type::Array, Vector{} } }}); return { Parse::Type::Array, segments }; } else { - return { Parse::Type::Array, std::vector{ + return { Parse::Type::Array, Vector{ { Parse::Type::Array, { - { Parse::Type::String, s} , { Parse::Type::Array, std::vector{} } + { Parse::Type::String, s} , { Parse::Type::Array, Vector{} } } } } }; } @@ -489,7 +489,7 @@ namespace NVL::Parse { if (t != Type::String && t != Type::Symbol) throw std::runtime_error("Bad type when constructing object!"); } - Object::Object(Type t, const std::vector& v) : type(t), value(v) { + Object::Object(Type t, const Vector& v) : type(t), value(v) { if (t != Type::Array && t != Type::Subexpression) throw std::runtime_error("Bad type when constructing object!"); } @@ -500,14 +500,14 @@ namespace NVL::Parse { if (t != Type::String && t != Type::Symbol) throw std::runtime_error("Bad type when constructing object!"); } - Object::Object(Type t, std::vector&& v) : type(t), value(std::move(v)) { + Object::Object(Type t, Vector&& v) : type(t), value(std::move(v)) { if (t != Type::Array && t != Type::Subexpression) throw std::runtime_error("Bad type when constructing object!"); } - std::vector ParseFile(const std::string& path) { + Vector ParseFile(const std::string& path) { String f = read_file_to_string(path); - std::vector list {}; // Vector of scenes which each contain a vector of Parses + Vector list {}; // Vector of scenes which each contain a vector of Parses for (size_t i = 0; i < f.length(); i++) { list.push_back(ParseScene(f, i)); } diff --git a/NVL/Parser.h b/NVL/Parser.h index 5246b7e..6a538b4 100644 --- a/NVL/Parser.h +++ b/NVL/Parser.h @@ -1,5 +1,4 @@ #pragma once -#include #include #include #include @@ -13,22 +12,22 @@ namespace NVL::Parse { Object() = delete; Object(const Number& v); Object(Type t, const String& v); - Object(Type t, const std::vector& v); + Object(Type t, const Vector& v); Object(Number&& v); Object(Type t, String&& v); - Object(Type t, std::vector&& v); + Object(Type t, Vector&& v); Type type; std::variant< Number, String, - std::vector + Vector > value; }; - using Command = std::vector; + using Command = Vector; class Scene { - std::vector commands{}; + Vector commands{}; public: String name; Scene(const String& name) : name(name) {} @@ -43,5 +42,5 @@ namespace NVL::Parse { } }; - std::vector ParseFile(const std::string& path); + Vector ParseFile(const std::string& path); }