Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_generic_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
40
41using namespace Aleph;
42
43namespace
44{
46 make_program_inputs()
47 {
49 inputs.append({"main.aw",
50 "import \"math.aw\";\n"
51 "let answer = add(40, 2);\n"});
52 inputs.append({"math.aw",
53 "fn add(x, y) {\n"
54 " return x + y;\n"
55 "}\n"});
56 return inputs;
57 }
58}
59
60TEST(CompilerGenericDriver, RunsProgramThroughReusableFrontendContract)
61{
62 Compiler_MVP_Frontend frontend;
63 Compiler_Generic_Driver driver(frontend);
64
65 ASSERT_TRUE(driver.execute_sources(make_program_inputs(),
66 Compiler_Driver_Action::Run));
67 ASSERT_TRUE(driver.run_output().executed);
68 ASSERT_TRUE(driver.run_output().result.ok());
69 EXPECT_EQ(driver.run_output().globals_text,
70 "Globals\n"
71 " add = Function(add)\n"
72 " answer = Int(42)\n");
73
74 const auto * hir = driver.find_artifact("hir");
75 ASSERT_NE(hir, nullptr);
76 EXPECT_NE(hir->text.find("HIRModule"), std::string::npos);
77 const auto * order = driver.find_artifact("imports.order");
78 ASSERT_NE(order, nullptr);
79 EXPECT_EQ(order->text,
80 "ImportOrder\n"
81 " [0] math.aw\n"
82 " [1] main.aw\n");
83 const auto * surface = driver.find_artifact("modules.surface");
84 ASSERT_NE(surface, nullptr);
85 EXPECT_NE(surface->text.find("Module(main.aw)"), std::string::npos);
86 EXPECT_NE(surface->text.find("Module(math.aw)"), std::string::npos);
87 EXPECT_NE(surface->text.find("Function(add)"), std::string::npos);
88 EXPECT_NE(surface->text.find("Global(answer)"), std::string::npos);
89 const auto * linkage = driver.find_artifact("modules.linkage");
90 ASSERT_NE(linkage, nullptr);
91 EXPECT_NE(linkage->text.find("ModuleLinkage"), std::string::npos);
92 EXPECT_NE(linkage->text.find("Module(main.aw) order=1"), std::string::npos);
93 EXPECT_NE(linkage->text.find("Visible(Function(add)) from math.aw imported"),
94 std::string::npos);
95 EXPECT_NE(linkage->text.find("Visible(Global(answer)) from main.aw local"),
96 std::string::npos);
97 const auto * binding = driver.find_artifact("modules.binding");
98 ASSERT_NE(binding, nullptr);
99 EXPECT_NE(binding->text.find("ModuleBindings"), std::string::npos);
100 EXPECT_NE(binding->text.find("Bind(Function(add)) -> math.aw imported"),
101 std::string::npos);
102 EXPECT_NE(binding->text.find("Bind(Global(answer)) -> main.aw local"),
103 std::string::npos);
104 const auto * names = driver.find_artifact("modules.names");
105 ASSERT_NE(names, nullptr);
106 EXPECT_NE(names->text.find("ModuleNameTable"), std::string::npos);
107 EXPECT_NE(names->text.find("Name(Function(add)) -> math.aw imported"),
108 std::string::npos);
109 const auto * semantic = driver.find_artifact("modules.semantic");
110 ASSERT_NE(semantic, nullptr);
111 EXPECT_NE(semantic->text.find("ModuleSemanticEnvironment"), std::string::npos);
112 EXPECT_NE(semantic->text.find("Value(Function(add)) -> math.aw imported"),
113 std::string::npos);
114 ASSERT_EQ(frontend.module_descriptors().size(), 2u);
115 ASSERT_EQ(frontend.module_merge_order().size(), 2u);
116 ASSERT_EQ(frontend.module_metadata().size(), 2u);
117 EXPECT_NE(frontend.find_artifact("ast"), nullptr);
118 EXPECT_NE(frontend.find_artifact("typed-sema"), nullptr);
119}
120
121TEST(CompilerGenericDriver, EmitsBytecodeAndPortableCThroughReusableFrontendContract)
122{
123 Compiler_MVP_Frontend bytecode_frontend;
124 Compiler_Generic_Driver bytecode_driver(bytecode_frontend);
125
126 ASSERT_TRUE(bytecode_driver.execute_sources(make_program_inputs(),
127 Compiler_Driver_Action::Emit_Bytecode));
128 ASSERT_NE(bytecode_driver.bytecode_module(), nullptr);
129
130 const auto * bytecode = bytecode_driver.find_artifact("bytecode");
131 ASSERT_NE(bytecode, nullptr);
132 EXPECT_NE(bytecode->text.find("BytecodeModule"), std::string::npos);
133
134 Compiler_MVP_Frontend c_frontend;
136 options.c_backend_options.module_name = "generic_driver_demo";
137 options.c_backend_options.emit_main = true;
138
139 Compiler_Generic_Driver c_driver(c_frontend, options);
140 ASSERT_TRUE(c_driver.execute_sources(make_program_inputs(),
141 Compiler_Driver_Action::Emit_C));
142 ASSERT_TRUE(c_driver.c_emission().valid);
143
144 const auto * c_artifact = c_driver.find_artifact("emit-c");
145 ASSERT_NE(c_artifact, nullptr);
146 EXPECT_NE(c_artifact->text.find("main(void)"), std::string::npos);
147 EXPECT_NE(c_artifact->text.find("generic_driver_demo"), std::string::npos);
148}
149
150TEST(CompilerGenericDriver, ReportsRuntimeFailuresThroughDriverMessages)
151{
153 inputs.append({"main.aw",
154 "let crash = 1 / 0;\n"});
155
156 Compiler_MVP_Frontend frontend;
157 Compiler_Generic_Driver driver(frontend);
158
159 EXPECT_FALSE(driver.execute_sources(inputs, Compiler_Driver_Action::Run));
160 ASSERT_TRUE(driver.run_output().executed);
161 EXPECT_FALSE(driver.run_output().result.ok());
162 EXPECT_TRUE(driver.run_output().value_text.empty());
163 EXPECT_EQ(driver.run_output().result.error.code, "RUN007");
164
165 const auto * messages = driver.find_artifact("driver.messages");
166 ASSERT_NE(messages, nullptr);
167 EXPECT_NE(messages->text.find("run: HIR runtime execution failed: RUN007 division by zero"),
168 std::string::npos);
169}
Reusable pipeline driver that operates on any Compiler_Frontend.
Reference frontend adapter that exposes the existing MVP language through Compiler_Frontend.
Reusable orchestration object built on top of Compiler_Frontend.
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_Driver_Artifact * find_artifact(std::string_view label) const noexcept
Finds one artifact by exact label.
const Compiler_Bytecode_Module * bytecode_module() const noexcept
Returns the lowered bytecode module from the last run, when available.
const Compiler_C_Backend_Emission & c_emission() const noexcept
Returns the C emission payload from the last emit-c action.
const Compiler_Driver_Run_Output & run_output() const noexcept
Returns the run summary from the last run action.
Compiler_Frontend implementation backed by the current MVP language stack.
const DynArray< Compiler_Module_Descriptor > & module_descriptors() const noexcept override
Returns parsed module descriptors for reusable import orchestration.
const Compiler_Driver_Artifact * find_artifact(std::string_view label) const noexcept override
Finds one frontend artifact by exact label.
const DynArray< size_t > & module_merge_order() const noexcept override
Returns the dependency-first merge order from the last parse.
const DynArray< Compiler_Module_Metadata > & module_metadata() const noexcept override
Returns reusable metadata for the top-level surface of each module.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
#define TEST(name)
Main namespace for Aleph-w library functions.
static struct argp_option options[]
Definition ntreepic.C:1890
bool valid
Whether emission completed successfully.
std::string module_name
Prefix used to derive stable C identifiers.
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.
Configuration shared by all runs of Compiler_Generic_Driver.
Compiler_C_Backend_Options c_backend_options
Backend-C emission options used by emit-c.
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.