Aleph-w 3.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{
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
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
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
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
112 c_options.c_backend_options.emit_main = true;
113
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.
const Compiler_Driver_Artifact * find_artifact(std::string_view label) const noexcept
Finds one artifact by label.
T & append()
Allocate a new entry to the end of array.
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
and
Check uniqueness with explicit hash + equality functors.
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.