36#include <gtest/gtest.h>
47 lower_to_hir(
const std::string & source,
56 const auto *
module = parser.parse_module();
63 return lowering.lower_module(module);
72 if (arguments.
size() != 2)
74 error.code =
"HOST001";
75 error.message =
"pair expects exactly 2 arguments";
87TEST(InterpreterRuntime, EvaluatesTopLevelBindingsAndCallsFunctions)
95 const auto * hir = lower_to_hir(
99 "let value = add(20, 22);\n",
100 sm, dx, ast_ctx, hir_ctx, typed);
104 ASSERT_TRUE(evaluated.ok());
111 " add = Function(add)\n"
112 " value = Int(42)\n");
117 const auto called = runtime.
call(
"add", arguments);
118 ASSERT_TRUE(called.ok());
119 EXPECT_EQ(called.value.kind, Interpreter_Value_Kind::Integer);
120 EXPECT_EQ(called.value.integer_value, 12);
123TEST(InterpreterRuntime, SupportsLoopsAssignmentsAndLocalMutation)
131 const auto * hir = lower_to_hir(
132 "fn countdown(n) {\n"
138 "let result = countdown(3);\n",
139 sm, dx, ast_ctx, hir_ctx, typed);
143 ASSERT_TRUE(evaluated.ok());
147 ASSERT_NE(result,
nullptr);
148 EXPECT_EQ(result->
kind, Interpreter_Value_Kind::Integer);
152TEST(InterpreterRuntime, BindsHostFunctionsInPrelude)
161 const auto called = runtime.
call(
"pair", arguments);
162 ASSERT_TRUE(called.ok());
163 EXPECT_EQ(called.value.kind, Interpreter_Value_Kind::Tuple);
164 ASSERT_EQ(called.value.tuple_elements().size(), 2u);
165 EXPECT_EQ(called.value.tuple_elements().access(0).integer_value, 1);
166 EXPECT_EQ(called.value.tuple_elements().access(1).integer_value, 2);
170TEST(InterpreterRuntime, TupleValuesKeepDeepCopySemantics)
177 auto copied = original;
180 EXPECT_EQ(original.tuple_elements().access(0).integer_value, 1);
181 EXPECT_EQ(copied.tuple_elements().access(0).integer_value, 99);
183 auto moved = std::move(copied);
184 EXPECT_EQ(moved.tuple_elements().access(0).integer_value, 99);
185 EXPECT_EQ(copied.kind, Interpreter_Value_Kind::Invalid);
188TEST(InterpreterRuntime, ReportsStructuredRuntimeErrors)
196 const auto * hir = lower_to_hir(
200 "let doomed = explode(5);\n",
201 sm, dx, ast_ctx, hir_ctx, typed);
205 ASSERT_FALSE(evaluated.ok());
206 EXPECT_EQ(evaluated.flow, Interpreter_Control_Flow_Kind::Runtime_Error);
207 EXPECT_EQ(evaluated.error.code,
"RUN007");
208 EXPECT_EQ(evaluated.error.message,
"division by zero");
215TEST(InterpreterRuntime, CompoundAssignmentThatOverflowsIsARuntimeError)
219 const char * statement;
224 const Case cases[] = {
225 {
"x += 1;",
true,
nullptr},
226 {
"x -= -1;",
true,
nullptr},
227 {
"x *= 2;",
true,
nullptr},
228 {
"x += 0;",
false,
"Int(9223372036854775807)"},
229 {
"x = -x - 1; x -= 1;",
true,
nullptr},
230 {
"x = -x - 1; x /= -1;",
true,
nullptr},
231 {
"x = -x - 1; x %= -1;",
false,
"Int(0)"},
232 {
"x = -x - 1; x *= 1;",
false,
"Int(-9223372036854775808)"},
235 for (
const auto & c : cases)
243 const auto * hir = lower_to_hir(std::string(
"fn f() {\n"
244 " let x = 9223372036854775807;\n ")
248 "let value = f();\n",
249 sm, dx, ast_ctx, hir_ctx, typed);
255 ASSERT_FALSE(evaluated.ok()) << c.statement;
256 EXPECT_EQ(evaluated.error.code,
"RUN013") << c.statement;
257 EXPECT_EQ(evaluated.error.message,
"integer overflow") << c.statement;
261 ASSERT_TRUE(evaluated.ok()) << c.statement;
263 std::string::npos) << c.statement;
Lowering from the current MVP typed AST into the reusable HIR model.
Recursive-descent parser for the compiler-support MVP grammar.
Reusable runtime and structured evaluator for HIR-based interpreters.
size_t size_t int32_t value
Arena-backed ownership context for AST nodes.
Arena-backed ownership context for HIR nodes.
Lowers the MVP typed AST into HIR.
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
Inference-oriented semantic pass for the MVP compiler front-end.
void analyze_module(const Compiler_Module *module)
Runs the typed semantic analysis for module.
Diagnostic accumulator and renderer.
bool has_errors() const noexcept
Returns whether any error or fatal diagnostic was emitted.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
Interpreter_Value * lookup(const std::string &name) noexcept
Finds a visible binding recursively.
Structured runtime for executing typed HIR.
const Interpreter_Environment & global_environment() const noexcept
Returns the globals environment for the last evaluated module.
bool bind_host_function(const std::string &name, const Interpreter_Host_Function callback, void *user_data=nullptr)
Defines one host function in the persistent prelude.
Interpreter_Execution_Result call(const std::string &name, const DynArray< Interpreter_Value > &arguments)
Calls one visible function by name.
Interpreter_Execution_Result evaluate_module(const Compiler_HIR_Module *module)
Evaluates one HIR module and leaves its globals installed.
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.
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
void error(const char *file, int line, const char *format,...)
Print an error message with file and line info.
std::string interpreter_dump_globals(const Interpreter_Runtime &runtime)
Dumps the globals currently installed in one runtime.
std::string interpreter_value_to_string(const Interpreter_Value &value)
Formats one runtime value deterministically for dumps and tests.
Structured runtime error.
static Interpreter_Value make_tuple(const DynArray< Interpreter_Value > &elements)
Builds a tuple runtime value by copying elements.
long long integer_value
Payload for Int values.
Interpreter_Value_Kind kind
Active value kind.
static Interpreter_Value make_integer(const long long i)
Builds an integer runtime value.