Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_driver_example.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 <iostream>
37#include <sstream>
38#include <string>
39
40#include <Compiler_Driver.H>
41
42using namespace Aleph;
43
44namespace
45{
47 build_inputs()
48 {
50 inputs.append({"main.aw",
51 "import \"math.aw\";\n"
52 "let answer = add(40, 2);\n"});
53 inputs.append({"math.aw",
54 "fn add(x, y) {\n"
55 " return x + y;\n"
56 "}\n"});
57 return inputs;
58 }
59
60 bool
61 report_failure(const Compiler_Driver & driver,
62 const char * label)
63 {
64 std::cerr << label << " failed\n";
65 std::cerr << driver.diagnostics_text();
66 if (const auto * messages = driver.find_artifact("driver.messages");
67 messages != nullptr)
68 std::cerr << messages->text;
69 return false;
70 }
71
72 void
73 print_first_lines(const std::string & text,
74 const size_t max_lines)
75 {
76 std::istringstream input(text);
77 std::string line;
78 size_t printed = 0;
79 while (printed < max_lines and std::getline(input, line))
80 {
81 std::cout << line << '\n';
82 ++printed;
83 }
84 }
85}
86
87int main()
88{
89 const auto inputs = build_inputs();
90
91 Compiler_Driver run_driver;
92 if (not run_driver.execute_sources(inputs, Compiler_Driver_Action::Run))
93 return report_failure(run_driver, "run") ? 0 : 1;
94
95 std::cout << "=== imports.order ===\n";
96 std::cout << run_driver.find_artifact("imports.order")->text;
97 std::cout << "=== run.result ===\n";
98 std::cout << run_driver.find_artifact("run.result")->text;
99 std::cout << "=== run.globals ===\n";
100 std::cout << run_driver.run_output().globals_text;
101
102 Compiler_Driver bytecode_driver;
103 if (not bytecode_driver.execute_sources(inputs,
104 Compiler_Driver_Action::Emit_Bytecode))
105 return report_failure(bytecode_driver, "emit-bytecode") ? 0 : 1;
106
107 std::cout << "\n=== bytecode ===\n";
108 std::cout << bytecode_driver.find_artifact("bytecode")->text;
109
110 Compiler_Driver_Options c_options;
111 c_options.c_backend_options.module_name = "driver_demo";
112 c_options.c_backend_options.emit_main = true;
113
114 Compiler_Driver c_driver(c_options);
115 if (not c_driver.execute_sources(inputs, Compiler_Driver_Action::Emit_C))
116 return report_failure(c_driver, "emit-c") ? 0 : 1;
117
118 std::cout << "\n=== emit-c (first 24 lines) ===\n";
119 print_first_lines(c_driver.c_emission().source, 24);
120
121 return 0;
122}
End-to-end multi-file driver for the compiler-support pipeline.
Reusable orchestration object for compiler pipeline experiments.
std::string diagnostics_text() const
Renders diagnostics from the last run as plain text.
const Compiler_Driver_Run_Output & run_output() const noexcept
Returns the run summary from the last run action.
const Compiler_C_Backend_Emission & c_emission() const noexcept
Returns the C emission payload from the last emit-c action.
const Compiler_Driver_Artifact * find_artifact(std::string_view label) const noexcept
Finds one artifact by label.
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.
T & append()
Allocate a new entry to the end of array.
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
std::string source
Final generated C translation unit.
bool emit_main
Whether to emit a standalone main() when the IR has a top-level body.
std::string module_name
Prefix used to derive stable C identifiers.
std::string text
Deterministic plain-text artifact payload.
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 globals_text
Stable dump of the final globals environment.