Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_ast_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_AST.H>
40
41using namespace Aleph;
42
43
44namespace
45{
46 struct Tracked_Object
47 {
48 static int destroyed;
49 std::string payload;
50
51 explicit Tracked_Object(std::string value = "")
52 : payload(std::move(value))
53 {}
54
56 {
57 ++destroyed;
58 }
59 };
60
61 int Tracked_Object::destroyed = 0;
62}
63
64
66{
67 Tracked_Object::destroyed = 0;
68
69 Compiler_Ast_Context ctx(4096);
70 auto * first = ctx.make<Tracked_Object>("a");
71 auto * second = ctx.make<Tracked_Object>("b");
72
73 ASSERT_NE(first, nullptr);
74 ASSERT_NE(second, nullptr);
75 EXPECT_EQ(Tracked_Object::destroyed, 0);
76
77 ctx.reset();
78
79 EXPECT_EQ(Tracked_Object::destroyed, 2);
80 EXPECT_EQ(ctx.allocated_size(), 0u);
81}
82
83
85{
86 Compiler_Ast_Context ctx(16384);
87
88 auto * lhs = ctx.make<Compiler_Identifier_Expr>("x", Source_Span{1, 0, 1});
89 auto * rhs = ctx.make<Compiler_Literal_Expr>(Compiler_Expr_Kind::Integer_Literal,
90 "1",
91 Source_Span{1, 4, 5});
92 auto * sum = ctx.make<Compiler_Binary_Expr>(lhs,
93 Compiler_Token_Kind::Plus,
94 Source_Span{1, 2, 3},
95 rhs);
96
97 auto * ret = ctx.make<Compiler_Return_Stmt>(Source_Span{1, 6, 12},
98 sum,
99 Source_Span{1, 13, 14});
100 auto * body = ctx.make<Compiler_Block_Stmt>(Source_Span{1, 15, 16},
101 Source_Span{1, 17, 18});
102 body->statements.append(ret);
103
104 auto * fn = ctx.make<Compiler_Function_Decl>(Source_Span{1, 19, 21},
105 "inc",
106 Source_Span{1, 22, 25},
107 Source_Span{},
108 nullptr,
109 body);
110 fn->parameters.append({"x", Source_Span{1, 26, 27}});
111
112 auto * module = ctx.make<Compiler_Module>();
113 module->functions.append(fn);
114
115 const auto expr_dump = compiler_dump_expr(sum);
116 const auto module_dump = compiler_dump_module(module);
117
118 EXPECT_NE(expr_dump.find("Binary(Plus)"), std::string::npos);
119 EXPECT_NE(expr_dump.find("Identifier(x)"), std::string::npos);
120 EXPECT_NE(expr_dump.find("Integer(1)"), std::string::npos);
121
122 EXPECT_NE(module_dump.find("Module"), std::string::npos);
123 EXPECT_NE(module_dump.find("Function(inc)"), std::string::npos);
124 EXPECT_NE(module_dump.find("Params: x"), std::string::npos);
125 EXPECT_NE(module_dump.find("Return"), std::string::npos);
126}
Arena-backed AST node model for the compiler-support MVP.
size_t size_t int32_t value
Definition ca-c-api.h:116
Arena-backed ownership context for AST nodes.
size_t allocated_size() const noexcept
Returns the total bytes currently used in the arena.
T * make(Args &&...args)
Allocates and constructs an AST node of type T.
void reset() noexcept
Reclaims all memory and destroys managed nodes.
#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
std::string compiler_dump_module(const Compiler_Module *module)
Dumps a parsed module in a deterministic text format.
std::string compiler_dump_expr(const Compiler_Expr *expr)
Dumps an expression subtree in a deterministic text format.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
STL namespace.
Node representing an infix binary operation.
Node representing a braced sequence of statements.
Node representing a top-level function declaration.
DynArray< Compiler_Param > parameters
Ordered list of parameters.
Node representing a named identifier reference.
Node representing a literal value.
Node representing a return from the current function.
Half-open byte range inside a source file.
Definition ah-source.H:100