36#include <gtest/gtest.h>
49 "import \"math.aw\";\n"
50 "let answer = add(40, 2);\n"});
59TEST(CompilerDriver, ParsesAndMergesOrderedMultiFileModules)
63 Compiler_Driver_Action::Parse_Only));
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);
74 const auto * import_order = driver.
find_artifact(
"imports.order");
75 ASSERT_NE(import_order,
nullptr);
76 EXPECT_EQ(import_order->text,
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);
90 EXPECT_EQ(parsed, Compiler_Driver_Action::Emit_Bytecode);
94TEST(CompilerDriver, RunsMergedHIRModuleAndPublishesArtifacts)
98 Compiler_Driver_Action::Run));
105 " add = Function(add)\n"
106 " answer = Int(42)\n");
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"),
115 ASSERT_NE(globals,
nullptr);
119TEST(CompilerDriver, EmitsBytecodeAndPortableCArtifacts)
123 Compiler_Driver_Action::Emit_Bytecode));
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);
133 options.c_backend_options.emit_main =
true;
137 Compiler_Driver_Action::Emit_C));
141 ASSERT_NE(c_artifact,
nullptr);
142 EXPECT_NE(c_artifact->text.find(
"main(void)"), std::string::npos);
145 EXPECT_NE(c_artifact->text.find(
"driver_demo"), std::string::npos);
148TEST(CompilerDriver, StopsBeforeLoweringWhenDiagnosticsExist)
151 inputs.
append({
"main.aw",
"let answer = missing(1);\n"});
154 EXPECT_FALSE(driver.
execute_sources(inputs, Compiler_Driver_Action::Run));
160 const auto * diagnostics = driver.
find_artifact(
"diagnostics");
161 ASSERT_NE(diagnostics,
nullptr);
162 EXPECT_NE(diagnostics->text.find(
"undeclared identifier"), std::string::npos);
165TEST(CompilerDriver, ReportsMissingImportDependencies)
169 "import \"missing.aw\";\n"
170 "let answer = 1;\n"});
173 EXPECT_FALSE(driver.
execute_sources(inputs, Compiler_Driver_Action::Parse_Only));
176 EXPECT_NE(driver.
diagnostics_text().find(
"cannot resolve import 'missing.aw'"),
180TEST(CompilerDriver, ReportsCyclicImports)
183 inputs.
append({
"a.aw",
"import \"b.aw\";\nlet a = 1;\n"});
184 inputs.
append({
"b.aw",
"import \"a.aw\";\nlet b = 2;\n"});
187 EXPECT_FALSE(driver.
execute_sources(inputs, Compiler_Driver_Action::Parse_Only));
190 EXPECT_NE(driver.
diagnostics_text().find(
"cyclic import"), std::string::npos);
193TEST(CompilerDriver, ClearsValueTextWhenRuntimeEvaluationFails)
197 "let crash = 1 / 0;\n"});
200 EXPECT_FALSE(driver.
execute_sources(inputs, Compiler_Driver_Action::Run));
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);
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"),
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.
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[]
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.