From a570ed4745b973649f7c90b14debcbec31f37d7b Mon Sep 17 00:00:00 2001 From: Ludovic 'Archivist' Lagouardette Date: Thu, 22 Apr 2021 15:32:35 +0200 Subject: [PATCH] Grimoire port --- include/gp/dynamic/compiler/mangle/decls.hpp | 7 +- .../compiler/mangle/mangle_function.hpp | 106 ++++++++++++------ .../dynamic/compiler/type/complex_types.hpp | 16 +-- include/gp/dynamic/compiler/type/decls.hpp | 4 +- 4 files changed, 88 insertions(+), 45 deletions(-) diff --git a/include/gp/dynamic/compiler/mangle/decls.hpp b/include/gp/dynamic/compiler/mangle/decls.hpp index 2870d80..9fae9ba 100644 --- a/include/gp/dynamic/compiler/mangle/decls.hpp +++ b/include/gp/dynamic/compiler/mangle/decls.hpp @@ -1,10 +1,11 @@ #pragma once #include "gp/containers/vector.hpp" -#include "gp/dynamic/compiler/type/type.hpp" +#include "gp/dynamic/compiler/type/decls.hpp" #include "gp/utils/allocators/allocator.hpp" using string = gp::vector; -string grMangleFunction(gp::vector, gp::allocator&); -gp::vector grUnmangleSignature(string, gp::allocator&); \ No newline at end of file +string gr_mangle_function(gp::vector, gp::allocator&); +gp::vector gr_unmangle_function(string, gp::allocator&); +gr_type gr_get_function_as_type(gr_function func, gp::allocator&); \ No newline at end of file diff --git a/include/gp/dynamic/compiler/mangle/mangle_function.hpp b/include/gp/dynamic/compiler/mangle/mangle_function.hpp index 05991eb..67c883f 100644 --- a/include/gp/dynamic/compiler/mangle/mangle_function.hpp +++ b/include/gp/dynamic/compiler/mangle/mangle_function.hpp @@ -1,72 +1,79 @@ #pragma once #include "gp/containers/vector.hpp" +#include "gp/dynamic/compiler/type/type.hpp" #include "gp/dynamic/compiler/mangle/decls.hpp" -constexpr auto append = [] (string& target, auto... var) { - constexpr auto append_str = [] (string& target, auto& var) { - if constexpr (std::is_same_v) { - for(auto it = var; *it != 0; it++) { - char c = *it; - target.push_back(c); - } - } else { - for(char c : var) { - target.push_back(c); +struct gr_function; + + + +namespace _hidden { + constexpr auto append = [] (string& target, auto... var) { + constexpr auto append_str = [] (string& target, auto& var) { + if constexpr (std::is_same_v) { + for(auto it = var; *it != 0; it++) { + char c = *it; + target.push_back(c); + } + } else { + for(char c : var) { + target.push_back(c); + } } - } - }; + }; - (append_str(target, var), ...); -}; + (append_str(target, var), ...); + }; +} -string grMangleFunction(gp::vector signature, gp::allocator& alloc) { +string gr_mangle_function(gp::vector signature, gp::allocator& alloc) { string mangledName(alloc); for(auto& type : signature) { - append(mangledName, "$"); + _hidden::append(mangledName, "$"); switch(type.base_type){ case gr_base_type::void_: - append(mangledName, "*"); + _hidden::append(mangledName, "*"); break; case gr_base_type::null_: - append(mangledName, "0"); + _hidden::append(mangledName, "0"); break; case gr_base_type::int_: - append(mangledName, "i"); + _hidden::append(mangledName, "i"); break; case gr_base_type::float_: - append(mangledName, "r"); + _hidden::append(mangledName, "r"); break; case gr_base_type::bool_: - append(mangledName, "b"); + _hidden::append(mangledName, "b"); break; case gr_base_type::string_: - append(mangledName, "s"); + _hidden::append(mangledName, "s"); break; case gr_base_type::array_: - append(mangledName, "n(", type.mangled_type, ")"); + _hidden::append(mangledName, "n(", type.mangled_type, ")"); break; case gr_base_type::class_: - append(mangledName, "p(", type.mangled_type, ")"); + _hidden::append(mangledName, "p(", type.mangled_type, ")"); break; case gr_base_type::enum_: - append(mangledName, "e(", type.mangled_type, ")"); + _hidden::append(mangledName, "e(", type.mangled_type, ")"); break; case gr_base_type::foreign: - append(mangledName, "u(", type.mangled_type, ")"); + _hidden::append(mangledName, "u(", type.mangled_type, ")"); break; case gr_base_type::function_: - append(mangledName, "f(", type.mangled_type, ")(", type.mangled_return_type, ")"); + _hidden::append(mangledName, "f(", type.mangled_type, ")(", type.mangled_return_type, ")"); break; case gr_base_type::task: - append(mangledName, "t(", type.mangled_type, ")"); + _hidden::append(mangledName, "t(", type.mangled_type, ")"); break; case gr_base_type::chan: - append(mangledName, "c(", type.mangled_type, ")"); + _hidden::append(mangledName, "c(", type.mangled_type, ")"); break; case gr_base_type::reference: - append(mangledName, "h(", type.mangled_type, ")"); + _hidden::append(mangledName, "h(", type.mangled_type, ")"); break; case gr_base_type::internalTuple: gp_config::assertion(false, "Trying to mangle a tuple. Tuples should not exist here."); @@ -75,8 +82,41 @@ string grMangleFunction(gp::vector signature, gp::allocator& alloc) { return mangledName; } -string grMangleNamedFunction(string name,gp::vector signature, gp::allocator& alloc) { +string gr_mangle_named_function(string name,gp::vector signature, gp::allocator& alloc) { string mangledName(alloc); - append(mangledName, name, grMangleFunction(signature, alloc)); + _hidden::append(mangledName, name, grMangleFunction(signature, alloc)); return mangledName; -} \ No newline at end of file +} + +gr_type gr_get_function_as_type(gr_function func, gp::allocator&);/* { + GrType type = func.isTask ? GrBaseType.task : GrBaseType.function_; + type.mangledType = grMangleNamedFunction("", func.inSignature); + type.mangledReturnType = grMangleNamedFunction("", func.outSignature); + return type; +}*/ + +string gr_unmangle_sub_function(string mangledSignature, int& i, gp::allocator& alloc) { + string subString(alloc); + int blockCount = 1; + if(i >= mangledSignature.length && mangledSignature[i] != '(') + throw new Exception("Invalid subType mangling format, missing ("); + i ++; + + for(; i < mangledSignature.length; i ++) { + switch(mangledSignature[i]) { + case '(': + blockCount ++; + break; + case ')': + blockCount --; + if(blockCount == 0) { + return subString; + } + break; + default: + break; + } + subString ~= mangledSignature[i]; + } + throw new Exception("Invalid subType mangling format, missing )"); +} diff --git a/include/gp/dynamic/compiler/type/complex_types.hpp b/include/gp/dynamic/compiler/type/complex_types.hpp index 3d18782..a86aee5 100644 --- a/include/gp/dynamic/compiler/type/complex_types.hpp +++ b/include/gp/dynamic/compiler/type/complex_types.hpp @@ -8,28 +8,28 @@ using string = gp::vector; /// Returns an array GrType of `subType` subtype. -inline gr_type grArray(gr_type subType, gp::allocator& alloc) { +inline gr_type gr_array(gr_type subType, gp::allocator& alloc) { auto list = gp::vector(alloc); list.push_back(subType); - return gr_type(gr_base_type::array_, grMangleFunction(list, alloc), alloc); + return gr_type(gr_base_type::array_, gr_mangle_function(list, alloc), alloc); } /// Returns a channel GrType of `subType` subtype. -inline gr_type grChannel(gr_type subType, gp::allocator& alloc) { +inline gr_type gr_channel(gr_type subType, gp::allocator& alloc) { auto list = gp::vector(alloc); list.push_back(subType); - return gr_type(gr_base_type::chan, grMangleFunction(list, alloc), alloc); + return gr_type(gr_base_type::chan, gr_mangle_function(list, alloc), alloc); } /// Pack multiple types as a single one. -inline gr_type grPackTuple(gp::vector types, gp::allocator& alloc) { - const string mangledName = grMangleFunction(types, alloc); +inline gr_type gr_pack_tuple(gp::vector types, gp::allocator& alloc) { + const string mangledName = gr_mangle_function(types, alloc); gr_type type(gr_base_type::internalTuple, mangledName, alloc); return type; } /// Unpack multiple types from a single one. -inline gp::vector grUnpackTuple(gr_type type, gp::allocator& alloc) { +inline gp::vector gr_unpack_tuple(gr_type type, gp::allocator& alloc) { gp_config::assertion(type.base_type != gr_base_type::internalTuple, "Cannot unpack a not tuple type."); - return grUnmangleSignature(type.mangled_type); + return gr_unmangle_signature(type.mangled_type); } \ No newline at end of file diff --git a/include/gp/dynamic/compiler/type/decls.hpp b/include/gp/dynamic/compiler/type/decls.hpp index d4175f7..fcc5bf7 100644 --- a/include/gp/dynamic/compiler/type/decls.hpp +++ b/include/gp/dynamic/compiler/type/decls.hpp @@ -1,5 +1,7 @@ #pragma once -enum class gr_base_type; +#include "gp/dynamic/compiler/type/base_type.hpp" + struct gr_type; struct gr_variable; +struct gr_function;