Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_types_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
37# include <gtest/gtest.h>
38
39# include <Compiler_Types.H>
40
41using namespace Aleph;
42
43
45{
47
48 EXPECT_EQ(ctx.builtin_count(), 6u);
49 EXPECT_EQ(ctx.to_string(ctx.invalid_type()), "Invalid");
50 EXPECT_EQ(ctx.to_string(ctx.unit_type()), "Unit");
51 EXPECT_EQ(ctx.to_string(ctx.bool_type()), "Bool");
52 EXPECT_EQ(ctx.to_string(ctx.integer_type()), "Int");
53 EXPECT_EQ(ctx.to_string(ctx.string_type()), "String");
54 EXPECT_EQ(ctx.to_string(ctx.character_type()), "Char");
55}
56
57
59{
61
62 const auto t0 = ctx.make_type_variable();
63 const auto elem = ctx.make_type_variable("Elem");
64 const auto tuple = ctx.make_tuple_type({ctx.integer_type(), ctx.bool_type(), elem});
65 const auto fn = ctx.make_function_type({tuple, t0}, ctx.character_type());
66
68 EXPECT_EQ(ctx.to_string(t0), "T0");
69 EXPECT_EQ(ctx.to_string(elem), "Elem");
70 EXPECT_EQ(ctx.to_string(tuple), "(Int, Bool, Elem)");
71 EXPECT_EQ(ctx.to_string(fn), "fn((Int, Bool, Elem), T0) -> Char");
72}
73
74
76{
78 const auto before = ctx.make_type_variable();
79 ASSERT_EQ(ctx.to_string(before), "T0");
80
81 ctx.clear();
82
83 EXPECT_EQ(ctx.builtin_count(), 6u);
84 EXPECT_EQ(ctx.size(), 6u);
85 const auto after = ctx.make_type_variable();
86 EXPECT_EQ(ctx.to_string(after), "T0");
87}
88
89
91{
93
94 const auto point = ctx.make_struct_type("Point");
97 field_names.append("y");
99 field_types.append(ctx.integer_type());
100 field_types.append(ctx.integer_type());
101 ctx.set_struct_fields(point, field_names, field_types);
102
103 const auto color = ctx.make_enum_type("Color");
104 DynArray<std::string> variants;
105 variants.append("Red");
106 variants.append("Green");
107 variants.append("Blue");
108 ctx.set_enum_variants(color, variants);
109
110 EXPECT_TRUE(ctx.is_struct(point));
112 EXPECT_EQ(ctx.to_string(point), "Point");
113 EXPECT_EQ(ctx.to_string(color), "Color");
114 EXPECT_EQ(ctx.type(point).member_names.size(), 2u);
115 EXPECT_EQ(ctx.type(point).member_names.access(0), "x");
116 EXPECT_EQ(ctx.type(point).components.access(1), ctx.integer_type());
118 EXPECT_EQ(ctx.type(color).member_names.access(2), "Blue");
119}
Stable type graph for the compiler-support MVP.
Context owning all compiler type nodes.
void set_struct_fields(const Compiler_Type_Id id, const DynArray< std::string > &field_names, const DynArray< Compiler_Type_Id > &field_types) const
Assigns field metadata to a previously created struct type.
bool is_type_variable(const Compiler_Type_Id id) const
Returns whether id names a type variable.
Compiler_Type_Id string_type() const noexcept
Returns the preloaded String type id.
size_t builtin_count() const noexcept
Returns the number of eager built-in types.
size_t size() const noexcept
Returns the total number of stored types.
std::string to_string(const Compiler_Type_Id id) const
Renders one type to a deterministic human-readable string.
bool is_struct(const Compiler_Type_Id id) const
Returns whether id names a nominal struct type.
Compiler_Type_Id make_enum_type(std::string name)
Creates a nominal enum type placeholder.
const Compiler_Type & type(const Compiler_Type_Id id) const
Returns type id.
Compiler_Type_Id invalid_type() const noexcept
Returns the preloaded Invalid type id.
void set_enum_variants(const Compiler_Type_Id id, const DynArray< std::string > &variant_names) const
Assigns variant metadata to a previously created enum type.
Compiler_Type_Id make_type_variable(std::string label="", const bool rigid=false)
Creates a fresh type variable.
Compiler_Type_Id make_tuple_type(const DynArray< Compiler_Type_Id > &members)
Creates a tuple type.
Compiler_Type_Id make_function_type(const DynArray< Compiler_Type_Id > &parameters, const Compiler_Type_Id result)
Creates a function type.
Compiler_Type_Id character_type() const noexcept
Returns the preloaded Char type id.
void clear() noexcept
Drops user-created types and keeps the built-ins.
Compiler_Type_Id unit_type() const noexcept
Returns the preloaded Unit type id.
bool is_enum(const Compiler_Type_Id id) const
Returns whether id names a nominal enum type.
Compiler_Type_Id bool_type() const noexcept
Returns the preloaded Bool type id.
Compiler_Type_Id integer_type() const noexcept
Returns the preloaded Int type id.
Compiler_Type_Id make_struct_type(std::string name)
Creates a nominal struct type placeholder.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
#define TEST(name)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
static long & color(typename GT::Node *p)
DynArray< Compiler_Type_Id > components
Tuple members, function parameters, or struct field types.
DynArray< std::string > member_names
Struct field names or enum variant names.