Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_driver_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
36#include <gtest/gtest.h>
37
38#include <Compiler_Driver.H>
39
40using namespace Aleph;
41
42namespace
43{
45 make_program_inputs()
46 {
48 inputs.append({"main.aw",
49 "import \"math.aw\";\n"
50 "let answer = add(40, 2);\n"});
51 inputs.append({"math.aw",
52 "fn add(x, y) {\n"
53 " return x + y;\n"
54 "}\n"});
55 return inputs;
56 }
57}
58
59TEST(CompilerDriver, ParsesAndMergesOrderedMultiFileModules)
60{
61 Compiler_Driver driver;
62 ASSERT_TRUE(driver.execute_sources(make_program_inputs(),
63 Compiler_Driver_Action::Parse_Only));
64
65 ASSERT_EQ(driver.parsed_ast_modules().size(), 2u);
66 ASSERT_NE(driver.merged_ast_module(), nullptr);
67 ASSERT_NE(driver.find_artifact("ast[0]:main.aw"), nullptr);
68 ASSERT_NE(driver.find_artifact("ast[1]:math.aw"), nullptr);
69
70 const auto * main_ast = driver.find_artifact("ast[0]:main.aw");
71 ASSERT_NE(main_ast, nullptr);
72 EXPECT_NE(main_ast->text.find("Import(\"math.aw\")"), std::string::npos);
73
74 const auto * import_order = driver.find_artifact("imports.order");
75 ASSERT_NE(import_order, nullptr);
76 EXPECT_EQ(import_order->text,
77 "ImportOrder\n"
78 " [0] math.aw\n"
79 " [1] main.aw\n");
80
81 const auto * merged = driver.find_artifact("ast");
82 ASSERT_NE(merged, nullptr);
83 EXPECT_NE(merged->text.find("Function(add)"), std::string::npos);
84 EXPECT_NE(merged->text.find("Let(answer)"), std::string::npos);
85 EXPECT_LT(merged->text.find("Function(add)"), merged->text.find("Let(answer)"));
86 EXPECT_EQ(driver.last_action(), Compiler_Driver_Action::Parse_Only);
87
88 Compiler_Driver_Action parsed = Compiler_Driver_Action::Run;
89 ASSERT_TRUE(compiler_parse_driver_action("emit-bytecode", parsed));
90 EXPECT_EQ(parsed, Compiler_Driver_Action::Emit_Bytecode);
91 EXPECT_STREQ(compiler_driver_action_name(parsed), "emit-bytecode");
92}
93
94TEST(CompilerDriver, RunsMergedHIRModuleAndPublishesArtifacts)
95{
96 Compiler_Driver driver;
97 ASSERT_TRUE(driver.execute_sources(make_program_inputs(),
98 Compiler_Driver_Action::Run));
99
100 ASSERT_TRUE(driver.run_output().executed);
101 ASSERT_TRUE(driver.run_output().result.ok());
102 EXPECT_EQ(driver.run_output().value_text, "Unit");
103 EXPECT_EQ(driver.run_output().globals_text,
104 "Globals\n"
105 " add = Function(add)\n"
106 " answer = Int(42)\n");
107
108 const auto * hir = driver.find_artifact("hir");
109 ASSERT_NE(hir, nullptr);
110 EXPECT_NE(hir->text.find("HIRModule"), std::string::npos);
111 EXPECT_NE(hir->text.find("Function(add): fn(Int, Int) -> Int"),
112 std::string::npos);
113
114 const auto * globals = driver.find_artifact("run.globals");
115 ASSERT_NE(globals, nullptr);
116 EXPECT_EQ(globals->text, driver.run_output().globals_text);
117}
118
119TEST(CompilerDriver, EmitsBytecodeAndPortableCArtifacts)
120{
121 Compiler_Driver bytecode_driver;
122 ASSERT_TRUE(bytecode_driver.execute_sources(make_program_inputs(),
123 Compiler_Driver_Action::Emit_Bytecode));
124 ASSERT_NE(bytecode_driver.bytecode_module(), nullptr);
125
126 const auto * bytecode = bytecode_driver.find_artifact("bytecode");
127 ASSERT_NE(bytecode, nullptr);
128 EXPECT_NE(bytecode->text.find("BytecodeModule"), std::string::npos);
129 EXPECT_NE(bytecode->text.find("answer"), std::string::npos);
130
132 options.c_backend_options.module_name = "driver_demo";
133 options.c_backend_options.emit_main = true;
134
135 Compiler_Driver c_driver(options);
136 ASSERT_TRUE(c_driver.execute_sources(make_program_inputs(),
137 Compiler_Driver_Action::Emit_C));
138 ASSERT_TRUE(c_driver.c_emission().valid);
139
140 const auto * c_artifact = c_driver.find_artifact("emit-c");
141 ASSERT_NE(c_artifact, nullptr);
142 EXPECT_NE(c_artifact->text.find("main(void)"), std::string::npos);
143 EXPECT_NE(c_artifact->text.find(c_driver.c_emission().module_init_symbol),
144 std::string::npos);
145 EXPECT_NE(c_artifact->text.find("driver_demo"), std::string::npos);
146}
147
148TEST(CompilerDriver, StopsBeforeLoweringWhenDiagnosticsExist)
149{
151 inputs.append({"main.aw", "let answer = missing(1);\n"});
152
153 Compiler_Driver driver;
154 EXPECT_FALSE(driver.execute_sources(inputs, Compiler_Driver_Action::Run));
155 EXPECT_TRUE(driver.diagnostic_engine().has_errors());
156 EXPECT_NE(driver.diagnostics_text().find("SEM002"), std::string::npos);
157 EXPECT_EQ(driver.hir_module(), nullptr);
158 EXPECT_EQ(driver.find_artifact("hir"), nullptr);
159
160 const auto * diagnostics = driver.find_artifact("diagnostics");
161 ASSERT_NE(diagnostics, nullptr);
162 EXPECT_NE(diagnostics->text.find("undeclared identifier"), std::string::npos);
163}
164
165TEST(CompilerDriver, ReportsMissingImportDependencies)
166{
168 inputs.append({"main.aw",
169 "import \"missing.aw\";\n"
170 "let answer = 1;\n"});
171
172 Compiler_Driver driver;
173 EXPECT_FALSE(driver.execute_sources(inputs, Compiler_Driver_Action::Parse_Only));
174 EXPECT_TRUE(driver.diagnostic_engine().has_errors());
175 EXPECT_NE(driver.diagnostics_text().find("DRV001"), std::string::npos);
176 EXPECT_NE(driver.diagnostics_text().find("cannot resolve import 'missing.aw'"),
177 std::string::npos);
178}
179
180TEST(CompilerDriver, ReportsCyclicImports)
181{
183 inputs.append({"a.aw", "import \"b.aw\";\nlet a = 1;\n"});
184 inputs.append({"b.aw", "import \"a.aw\";\nlet b = 2;\n"});
185
186 Compiler_Driver driver;
187 EXPECT_FALSE(driver.execute_sources(inputs, Compiler_Driver_Action::Parse_Only));
188 EXPECT_TRUE(driver.diagnostic_engine().has_errors());
189 EXPECT_NE(driver.diagnostics_text().find("DRV002"), std::string::npos);
190 EXPECT_NE(driver.diagnostics_text().find("cyclic import"), std::string::npos);
191}
192
193TEST(CompilerDriver, ClearsValueTextWhenRuntimeEvaluationFails)
194{
196 inputs.append({"main.aw",
197 "let crash = 1 / 0;\n"});
198
199 Compiler_Driver driver;
200 EXPECT_FALSE(driver.execute_sources(inputs, Compiler_Driver_Action::Run));
201 ASSERT_TRUE(driver.run_output().executed);
202 EXPECT_FALSE(driver.run_output().result.ok());
203 EXPECT_TRUE(driver.run_output().value_text.empty());
204 EXPECT_EQ(driver.run_output().result.error.code, "RUN007");
205
206 const auto * run_result = driver.find_artifact("run.result");
207 ASSERT_NE(run_result, nullptr);
208 EXPECT_EQ(run_result->text.find(" Value: "), std::string::npos);
209 EXPECT_NE(run_result->text.find(" Error: RUN007 division by zero"), std::string::npos);
210
211 const auto * messages = driver.find_artifact("driver.messages");
212 ASSERT_NE(messages, nullptr);
213 EXPECT_NE(messages->text.find("run: HIR runtime execution failed: RUN007 division by zero"),
214 std::string::npos);
215}
End-to-end multi-file driver for the compiler-support pipeline.
Reusable orchestration object for compiler pipeline experiments.
const Compiler_Bytecode_Module * bytecode_module() const noexcept
Returns the lowered bytecode module from the last run, when available.
std::string diagnostics_text() const
Renders diagnostics from the last run as plain text.
const Compiler_Module * merged_ast_module() const noexcept
Returns the merged AST module from the last run.
const Compiler_Driver_Run_Output & run_output() const noexcept
Returns the run summary from the last run action.
const Diagnostic_Engine & diagnostic_engine() const noexcept
Returns the diagnostic engine populated by the last run.
const Compiler_C_Backend_Emission & c_emission() const noexcept
Returns the C emission payload from the last emit-c action.
const DynArray< Compiler_Module * > & parsed_ast_modules() const noexcept
Returns the parsed per-file AST modules from the last run.
const Compiler_Driver_Artifact * find_artifact(std::string_view label) const noexcept
Finds one artifact by label.
Compiler_Driver_Action last_action() const noexcept
Returns the last requested action.
bool execute_sources(const DynArray< Compiler_Driver_Source > &inputs, const Compiler_Driver_Action action=Compiler_Driver_Action::Run)
Runs the pipeline over in-memory sources.
const Compiler_HIR_Module * hir_module() const noexcept
Returns the lowered HIR module from the last run, when available.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
T & append()
Allocate a new entry to the end of array.
#define TEST(name)
Main namespace for Aleph-w library functions.
Compiler_Driver_Action
High-level actions supported by compiler pipeline drivers.
const char * compiler_driver_action_name(const Compiler_Driver_Action action) noexcept
Returns the canonical external spelling for one driver action.
bool compiler_parse_driver_action(std::string_view text, Compiler_Driver_Action &action) noexcept
Parses one canonical driver action spelling.
static struct argp_option options[]
Definition ntreepic.C:1890
bool valid
Whether emission completed successfully.
std::string module_init_symbol
Generated C symbol that initializes module globals.
std::string module_name
Prefix used to derive stable C identifiers.
Immutable configuration shared by all runs of one driver object.
Compiler_C_Backend_Options c_backend_options
Backend-C emission options used by emit-c.
std::string value_text
Stable rendering of the produced value.
std::string globals_text
Stable dump of the final globals environment.
Interpreter_Execution_Result result
Raw runtime result.
bool executed
Whether the current driver action actually executed code.
Interpreter_Runtime_Error error
Structured runtime failure when flow == Runtime_Error.
bool ok() const noexcept
Returns whether this result represents a runtime failure.
std::string code
Stable runtime code such as RUN004.