Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Driver_Contracts.H
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
42#ifndef COMPILER_DRIVER_CONTRACTS_H
43#define COMPILER_DRIVER_CONTRACTS_H
44
45#include <sstream>
46#include <string>
47#include <string_view>
48#include <utility>
49
50#include <Interpreter_Runtime.H>
51#include <tpl_dynArray.H>
52
53namespace Aleph {
56{
58 Sema_Only,
59 HIR,
60 IR,
61 Run,
62 Emit_C,
64};
65
68{
69 switch (action)
70 {
72 return "parse-only";
74 return "sema-only";
76 return "hir";
78 return "ir";
80 return "run";
82 return "emit-c";
84 return "emit-bytecode";
85 }
86
87 return "unknown";
88}
89
100inline bool compiler_parse_driver_action(std::string_view text, Compiler_Driver_Action &action) noexcept
101{
102 if (text == "parse-only" or text == "parse")
103 {
105 return true;
106 }
107 if (text == "sema-only" or text == "sema")
108 {
110 return true;
111 }
112 if (text == "hir")
113 {
115 return true;
116 }
117 if (text == "ir")
118 {
120 return true;
121 }
122 if (text == "run")
123 {
125 return true;
126 }
127 if (text == "emit-c" or text == "c")
128 {
130 return true;
131 }
132 if (text == "emit-bytecode" or text == "bytecode")
133 {
135 return true;
136 }
137
138 return false;
139}
140
143{
144 std::string name;
145 std::string text;
146};
147
150{
151 std::string label;
152 std::string text;
153};
154
157{
158 bool executed = false;
160 std::string value_text;
161 std::string globals_text;
162
165 {
166 return not executed or result.ok();
167 }
168};
169
170namespace Compiler_Driver_Detail {
171inline std::string artifact_label(const std::string &prefix, const size_t index, const std::string &name)
172{
173 std::ostringstream out;
174 out << prefix << '[' << index << "]:" << name;
175 return out.str();
176}
177
179 const std::string &label,
180 std::string text)
181{
182 artifacts.append({label, std::move(text)});
183}
184
185template <typename Report>
186inline bool merge_report(const std::string &stage,
187 const Report &report,
188 DynArray<std::string> &errors,
189 DynArray<std::string> &warnings)
190{
191 for (size_t i = 0; i < report.errors.size(); ++i)
192 errors.append(stage + ": " + report.errors.access(i));
193 for (size_t i = 0; i < report.warnings.size(); ++i)
194 warnings.append(stage + ": " + report.warnings.access(i));
195 return report.valid;
196}
197
198inline std::string render_messages(const DynArray<std::string> &errors,
199 const DynArray<std::string> &warnings)
200{
201 std::ostringstream out;
202 out << "DriverMessages\n";
203
204 if (errors.is_empty() and warnings.is_empty())
205 {
206 out << " <none>\n";
207 return out.str();
208 }
209
210 for (size_t i = 0; i < errors.size(); ++i)
211 out << " error: " << errors.access(i) << '\n';
212 for (size_t i = 0; i < warnings.size(); ++i)
213 out << " warning: " << warnings.access(i) << '\n';
214 return out.str();
215}
216
218{
219 std::ostringstream out;
220 out << "HIRRun\n";
221 out << " Executed: " << (output.executed ? "true" : "false") << '\n';
222 if (not output.executed)
223 return out.str();
224
225 out << " Flow: " << interpreter_control_flow_name(output.result.flow) << '\n';
226 if (output.result.ok())
227 out << " Value: " << output.value_text << '\n';
228 else
229 {
230 out << " Error: " << output.result.error.code << " " << output.result.error.message << '\n';
231 if (output.result.error.span.is_valid())
232 out << " Span: " << output.result.error.span.to_string() << '\n';
233 }
234 return out.str();
235}
236} // namespace Compiler_Driver_Detail
237} // namespace Aleph
238
239#endif
Reusable runtime and structured evaluator for HIR-based interpreters.
size_t size_t int32_t * out
Definition ca-c-api.h:120
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) const noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
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
std::string render_messages(const DynArray< std::string > &errors, const DynArray< std::string > &warnings)
std::string artifact_label(const std::string &prefix, const size_t index, const std::string &name)
bool merge_report(const std::string &stage, const Report &report, DynArray< std::string > &errors, DynArray< std::string > &warnings)
std::string render_run_result(const Compiler_Driver_Run_Output &output)
void append_artifact(DynArray< Compiler_Driver_Artifact > &artifacts, const std::string &label, std::string text)
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Compiler_Driver_Action
High-level actions supported by compiler pipeline drivers.
@ Emit_C
Lower to IR and emit portable C.
@ IR
Stop after lowering to explicit IR.
@ Parse_Only
Stop after parsing and publish AST/frontend artifacts.
@ Run
Execute the lowered HIR module with Interpreter_Runtime.
@ Emit_Bytecode
Lower to IR and emit reusable bytecode.
@ Sema_Only
Stop after semantic analysis.
@ HIR
Stop after lowering to typed HIR.
and
Check uniqueness with explicit hash + equality functors.
static void prefix(Node *root, DynList< Node * > &acc)
const char * interpreter_control_flow_name(const Interpreter_Control_Flow_Kind kind) noexcept
Stable debug name for one control-flow outcome.
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.
Stable text artifact produced during one pipeline run.
std::string text
Deterministic plain-text artifact payload.
std::string label
Logical artifact identifier such as hir or run.globals.
Structured output of a run action.
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.
bool ok() const noexcept
Returns whether execution succeeded or never ran.
One logical input source handled by a compiler frontend.
std::string text
Full source text.
std::string name
User-facing file name used in diagnostics and artifacts.
Result of evaluating one expression, statement, module, or call.
bool ok() const noexcept
Returns whether this result represents a runtime failure.
Lazy and scalable dynamic array implementation.
ofstream output
Definition writeHeap.C:215