Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_hir_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
40# include <Compiler_Parser.H>
41
42using namespace Aleph;
43
44
46{
48 const auto file_id = sm.add_virtual_file(
49 "main.aw",
50 "fn add(x, y) {\n"
51 " let z = x + y;\n"
52 " return z;\n"
53 "}\n"
54 "let value = add(1, 2);\n");
55
57 Compiler_Ast_Context ast_ctx(1 << 16);
58 Compiler_Parser parser(ast_ctx, sm, file_id, &dx);
59 const auto * module = parser.parse_module();
61
63 typed.analyze_module(module);
65
66 Compiler_HIR_Context hir_ctx(1 << 16);
67 Compiler_HIR_Lowering lowering(hir_ctx, typed);
68 const auto * hir = lowering.lower_module(module);
69
70 const auto dump = compiler_dump_hir_module(hir, typed.type_context());
72 dump,
73 "HIRModule\n"
74 " Function(add): fn(Int, Int) -> Int\n"
75 " Params: x: Int, y: Int\n"
76 " Body:\n"
77 " Block\n"
78 " Let(z): Int\n"
79 " Binary(Plus): Int\n"
80 " Variable(x): Int\n"
81 " Variable(y): Int\n"
82 " Return\n"
83 " Variable(z): Int\n"
84 " Let(value): Int\n"
85 " Call: Int\n"
86 " Callee:\n"
87 " Variable(add): fn(Int, Int) -> Int\n"
88 " Args:\n"
89 " Constant(Integer, 1): Int\n"
90 " Constant(Integer, 2): Int\n");
91}
92
93
95{
97 const auto file_id = sm.add_virtual_file(
98 "main.aw",
99 "fn guard(flag) {\n"
100 " if ((flag)) { return 1; }\n"
101 " return 0;\n"
102 "}\n");
103
105 Compiler_Ast_Context ast_ctx(1 << 16);
106 Compiler_Parser parser(ast_ctx, sm, file_id, &dx);
107 const auto * module = parser.parse_module();
109
111 typed.analyze_module(module);
113
114 Compiler_HIR_Context hir_ctx(1 << 16);
115 Compiler_HIR_Lowering lowering(hir_ctx, typed);
116 const auto * hir = lowering.lower_module(module);
117 const auto * function = hir->functions.access(0);
118 const auto * if_stmt =
119 static_cast<const Compiler_HIR_If_Stmt *>(function->body->statements.access(0));
120
121 ASSERT_NE(if_stmt, nullptr);
122 ASSERT_NE(if_stmt->condition, nullptr);
123 EXPECT_EQ(if_stmt->condition->kind, Compiler_HIR_Expr_Kind::Variable);
124 EXPECT_EQ(typed.type_context().to_string(if_stmt->condition->type_id), "Bool");
125}
126
127
129{
131 const auto file_id = sm.add_virtual_file(
132 "main.aw",
133 "fn loop(flag) {\n"
134 " while (flag) {\n"
135 " flag = false;\n"
136 " continue;\n"
137 " }\n"
138 "}\n");
139
141 Compiler_Ast_Context ast_ctx(1 << 16);
142 Compiler_Parser parser(ast_ctx, sm, file_id, &dx);
143 const auto * module = parser.parse_module();
145
147 typed.analyze_module(module);
149
150 Compiler_HIR_Context hir_ctx(1 << 16);
151 Compiler_HIR_Lowering lowering(hir_ctx, typed);
152 const auto * hir = lowering.lower_module(module);
153 const auto * function = hir->functions.access(0);
154 ASSERT_EQ(typed.type_context().to_string(function->type_id), "fn(Bool) -> Unit");
155
156 const auto * while_stmt =
157 static_cast<const Compiler_HIR_While_Stmt *>(function->body->statements.access(0));
158 ASSERT_NE(while_stmt, nullptr);
159 ASSERT_NE(while_stmt->body, nullptr);
160 EXPECT_EQ(while_stmt->kind, Compiler_HIR_Stmt_Kind::While);
161 EXPECT_EQ(while_stmt->condition->kind, Compiler_HIR_Expr_Kind::Variable);
162
163 const auto * body =
164 static_cast<const Compiler_HIR_Block_Stmt *>(while_stmt->body);
165 ASSERT_EQ(body->statements.size(), 2u);
166 EXPECT_EQ(body->statements.access(0)->kind, Compiler_HIR_Stmt_Kind::Eval);
167 EXPECT_EQ(body->statements.access(1)->kind, Compiler_HIR_Stmt_Kind::Continue);
168}
Lowering from the current MVP typed AST into the reusable HIR model.
Recursive-descent parser for the compiler-support MVP grammar.
Arena-backed ownership context for AST nodes.
Arena-backed ownership context for HIR nodes.
Lowers the MVP typed AST into HIR.
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
std::string to_string(const Compiler_Type_Id id) const
Renders one type to a deterministic human-readable string.
Inference-oriented semantic pass for the MVP compiler front-end.
void analyze_module(const Compiler_Module *module)
Runs the typed semantic analysis for module.
const Compiler_Type_Context & type_context() const noexcept
Returns the internal type context.
Diagnostic accumulator and renderer.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
Stores source files and resolves offsets into human-readable data.
Definition ah-source.H:184
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
Definition ah-source.H:258
#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_hir_module(const Compiler_HIR_Module *module, const Compiler_Type_Context &types)
Dumps one HIR module in a deterministic text format.
Structured block statement.
Structured conditional statement.
Structured while-loop statement.