37# include <gtest/gtest.h>
50 std::vector<std::string>
53 std::vector<std::string> codes;
54 for (
size_t i = 0; i < dx.
size(); ++i)
55 codes.push_back(dx.
get(i).
code);
60 contains_code(
const std::vector<std::string> & codes,
61 const std::string &
code)
63 for (
const auto & item : codes)
71TEST(CompilerSema, ResolvesFunctionsParametersLocalsAndGlobals)
80 "let value = add(1, 2);\n");
85 const auto *
module = parser.parse_module();
95 const auto * fn =
module->functions.access(0);
96 const auto * let_stmt =
104 const auto * ret_stmt =
108 const auto * top_let =
112 const auto * add_ref =
133TEST(CompilerSema, ReportsDuplicateDeclarationsAndShadowing)
138 "fn dup(x, x) { let y = 1; let y = 2; }\n"
140 "fn shadow(a) { { let a = 1; } }\n");
145 const auto *
module = parser.parse_module();
154 const auto codes = collect_codes(dx);
155 EXPECT_TRUE(contains_code(codes,
"SEM001"));
156 EXPECT_TRUE(contains_code(codes,
"SEM007"));
160TEST(CompilerSema, ReportsDuplicateTypeDeclarations)
165 "struct Point { x: Int; }\n"
166 "enum Point { Origin }\n");
171 const auto *
module = parser.parse_module();
178 const auto codes = collect_codes(dx);
179 EXPECT_TRUE(contains_code(codes,
"SEM008"));
183TEST(CompilerSema, ReportsDuplicateAndSelfImports)
188 "import \"dep.aw\";\n"
189 "import \"dep.aw\";\n"
190 "import \"main.aw\";\n");
195 const auto *
module = parser.parse_module();
202 const auto codes = collect_codes(dx);
203 EXPECT_TRUE(contains_code(codes,
"SEM009"));
204 EXPECT_TRUE(contains_code(codes,
"SEM010"));
208TEST(CompilerSema, ReportsUnresolvedNamesAndControlFlowMisuse)
222 const auto *
module = parser.parse_module();
229 const auto codes = collect_codes(dx);
230 EXPECT_TRUE(contains_code(codes,
"SEM002"));
231 EXPECT_TRUE(contains_code(codes,
"SEM003"));
232 EXPECT_TRUE(contains_code(codes,
"SEM004"));
233 EXPECT_TRUE(contains_code(codes,
"SEM006"));
Recursive-descent parser for the compiler-support MVP grammar.
Name-resolution and basic semantic checks for the compiler-support MVP.
Arena-backed ownership context for AST nodes.
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
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.
size_t resolution_count() const noexcept
Returns the number of identifier resolutions.
size_t symbol_count() const noexcept
Returns the number of collected symbols.
void analyze_module(const Compiler_Module *module)
Analyzes a parsed module.
Diagnostic accumulator and renderer.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
const Diagnostic & get(const size_t i) const
Returns diagnostic i.
size_t size() const noexcept
Returns the number of stored diagnostics.
Stores source files and resolves offsets into human-readable data.
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
Main namespace for Aleph-w library functions.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Node representing an infix binary operation.
Node representing a function or method call.
Node representing a named identifier reference.
Node representing a local variable binding.
Node representing a return from the current function.
Options controlling semantic analyzer behavior.
bool allow_shadowing
If false, nested redefinitions report SEM007.
std::string name
Declared name.
Compiler_Symbol_Kind kind
Symbol category.
std::string code
Optional stable code such as E001.