51#ifndef COMPILER_SEMA_H
52#define COMPILER_SEMA_H
89 const std::string &
code,
90 const std::string ¬e =
"",
91 const std::string &help =
"")
const
105 const std::string &name,
109 if (result.duplicate_local())
111 emit_error(span,
"duplicate declaration of '" + name +
"'",
"SEM001");
112 return result.symbol_id;
116 emit_error(span,
"shadowing declaration of '" + name +
"' is not allowed",
"SEM007");
118 return result.symbol_id;
127 if (entry !=
nullptr)
129 resolutions.access(entry->second).symbol_id = symbol_id;
141 if (result.duplicate_local())
143 emit_error(span,
"duplicate type declaration of '" + name +
"'",
"SEM008");
150 if (module ==
nullptr)
154 for (
size_t i = 0; i <
module->imports.size(); ++i)
156 const auto *
decl =
module->imports.access(i);
162 "module '" + module->
source_name +
"' cannot import itself",
165 bool duplicate =
false;
175 "duplicate import of '" +
decl->module_name +
"'",
200 if (symbol_id ==
nullptr)
202 emit_error(node->span,
"use of undeclared identifier '" + node->name +
"'",
"SEM002");
230 for (
size_t i = 0; i < node->arguments.size(); ++i)
239 if (block ==
nullptr)
243 for (
size_t i = 0; i < block->statements.size(); ++i)
276 emit_error(node->keyword_span,
"'return' is only valid inside a function",
"SEM006");
308 emit_error(node->keyword_span,
"'break' is only valid inside a loop",
"SEM003");
316 emit_error(node->keyword_span,
"'continue' is only valid inside a loop",
"SEM004");
329 for (
size_t i = 0; i <
fn->parameters.size(); ++i)
331 const auto &
param =
fn->parameters.access(i);
370 if (module ==
nullptr)
378 for (
size_t i = 0; i <
module->type_declarations.size(); ++i)
380 const auto *
decl =
module->type_declarations.access(i);
385 for (
size_t i = 0; i <
module->functions.size(); ++i)
387 const auto *
fn =
module->functions.access(i);
392 for (
size_t i = 0; i <
module->statements.size(); ++i)
395 for (
size_t i = 0; i <
module->functions.size(); ++i)
434 <<
"Compiler_Semantic_Analyzer::resolution(): invalid index " << i;
449 if (entry ==
nullptr)
452 const auto id =
resolutions.access(entry->second).symbol_id;
Arena-backed AST node model for the compiler-support MVP.
Reusable lexical bindings and symbol records for frontends.
Plain-text diagnostic engine for compiler-style tooling.
#define ah_out_of_range_error_unless(C)
Throws std::out_of_range if condition does NOT hold.
Name-resolution and basic semantic checker for the MVP AST.
const Compiler_Symbol * resolved_symbol(const Compiler_Expr *expr) const noexcept
Returns the resolved symbol for an identifier expression.
Compiler_Semantic_Options options
Compiler_Symbol_Bindings value_bindings
void analyze_imports(const Compiler_Module *module) const
Compiler_Symbol_Bindings type_bindings
DynArray< Compiler_Name_Resolution > resolutions
std::string dump_symbols() const
Dumps the current symbol table in a deterministic text format.
void declare_type_name(const std::string &name, const Source_Span &span)
const Compiler_Name_Resolution & resolution(const size_t i) const
Returns resolution record i.
size_t resolution_count() const noexcept
Returns the number of identifier resolutions.
void analyze_function(const Compiler_Function_Decl *fn)
Compiler_Symbol_Id declare_symbol(const Compiler_Symbol_Kind kind, const std::string &name, const Source_Span &span)
Diagnostic_Engine * diagnostics
const Compiler_Symbol & symbol(const Compiler_Symbol_Id id) const
Returns symbol id.
void analyze_expr(const Compiler_Expr *expr)
void record_resolution(const Compiler_Expr *expr, const Compiler_Symbol_Id symbol_id)
size_t symbol_count() const noexcept
Returns the number of collected symbols.
void clear() noexcept
Clears previously collected semantic state.
Compiler_Semantic_Analyzer(Diagnostic_Engine *dx=nullptr, const Compiler_Semantic_Options &opts={})
Constructs a semantic analyzer.
void emit_error(const Source_Span &span, const std::string &message, const std::string &code, const std::string ¬e="", const std::string &help="") const
void analyze_block(const Compiler_Block_Stmt *block)
DynMapTree< const Compiler_Expr *, size_t > resolution_index
void analyze_module(const Compiler_Module *module)
Analyzes a parsed module.
void analyze_stmt(const Compiler_Stmt *stmt)
Reusable lexical symbol table with stable ids and scope tracking.
Compiler_Symbol_Declare_Result declare(const Compiler_Symbol_Kind kind, const std::string &name, const Source_Span &span)
Declares one symbol in the current scope.
void clear() noexcept
Clears all scopes and previously recorded symbols.
const Compiler_Symbol & symbol(const Compiler_Symbol_Id id) const
Returns one symbol by its stable 1-based identifier.
std::string dump_symbols() const
Dumps the current symbol table in deterministic text form.
const Compiler_Symbol_Id * lookup(const std::string &name) const noexcept
Looks up one visible symbol id by name.
size_t symbol_count() const noexcept
Returns the number of recorded symbols.
void leave_scope()
Leaves the current lexical scope.
size_t enter_scope()
Enters one nested lexical scope.
Diagnostic_Builder & code(const std::string &value)
Sets the stable diagnostic code.
size_t emit() const noexcept
Finalizes the builder and returns the diagnostic index.
Diagnostic_Builder & help(const std::string &msg)
Appends a help line.
Diagnostic_Builder & note(const std::string &msg)
Appends a note line.
Diagnostic accumulator and renderer.
Diagnostic_Builder error(const Source_Span &span, const std::string &msg)
Starts an error diagnostic.
Generic key-value map implemented on top of a binary search tree.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
@ Grouping
Parenthesized expression.
@ Unary
Prefix unary operation (e.g., -x, !p).
@ Identifier
Variable or function name reference.
@ Integer_Literal
Numeric integer constant.
@ Invalid
Placeholder for malformed expressions.
@ Char_Literal
Character constant.
@ Binary
Infix binary operation (e.g., x + y).
@ Call
Function or method call.
@ String_Literal
String constant.
@ Bool_Literal
Boolean constant (true/false).
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
@ Import
One concrete import "name"; declaration.
Compiler_Symbol_Kind
Generic symbol kinds reusable across multiple frontends.
@ While
Loop while a condition is true.
@ Invalid
Placeholder for malformed statements.
@ Expr
Expression evaluated for side effects (e.g., assignment, call).
@ If
Conditional execution.
@ Return
Exit current function with an optional value.
@ Continue
Skip to the next iteration of the innermost loop.
@ Let
Variable declaration and optional initialization.
@ Block
Scoped sequence of statements.
@ Break
Immediate exit from the innermost loop.
and
Check uniqueness with explicit hash + equality functors.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
size_t Compiler_Symbol_Id
Node representing an infix binary operation.
Node representing a braced sequence of statements.
Node representing a loop break.
Node representing a function or method call.
Node representing a loop continuation.
Node representing an expression evaluated as a statement.
Abstract base class for expression nodes.
Compiler_Expr_Kind kind
Specific expression type.
Node representing a top-level function declaration.
Node representing an expression explicitly wrapped in parentheses.
Node representing a named identifier reference.
Node representing a conditional branch.
Node representing a local variable binding.
Node representing a complete translation unit or module.
std::string source_name
User-facing source name that produced the module.
DynArray< Compiler_Stmt * > statements
Optional top-level code.
DynArray< Compiler_Function_Decl * > functions
Top-level function definitions.
Mapping from an identifier expression to its resolved symbol.
const Compiler_Expr * expr
Identifier expression node.
Compiler_Symbol_Id symbol_id
Resolved symbol id.
Node representing a return from the current function.
Options controlling semantic analyzer behavior.
bool allow_shadowing
If false, nested redefinitions report SEM007.
Abstract base class for statement nodes.
Compiler_Stmt_Kind kind
Specific statement type.
One reusable symbol record tracked by lexical bindings.
Node representing a prefix unary operation.
Node representing a while-loop.
Half-open byte range inside a source file.
Dynamic key-value map based on balanced binary search trees.