c-repl/test/variables.cpp
2023-12-15 10:38:36 +01:00

59 lines
1.4 KiB
C++

#include "include/test.h"
#include "../src/include/memory.h"
#include "../src/include/tokenize.h"
#include "../src/include/parser.h"
#include "../src/include/analysis.h"
#include "../src/include/interpreter.h"
EvalResult execute(string s) {
Memory memory;;
vector<Token> tokens = tokenize({ s });
Node ast = parse(tokens);
analyze(ast, memory);
EvalResult res = eval(ast, memory);
return res;
}
int main() {
_TEST_PRESENTATION("Variables");
_TEST_ASSERT(
_TEST_NO_EXCEPTION(get<int>(execute("int uneVariableFarfelue__ = 12;")) == 12),
"Déclaration avec assignement",
true
);
_TEST_ASSERT(
_TEST_NO_EXCEPTION(get<int>(execute("int x = 5; x = x + 3;")) == 8),
"Déclaration puis assignement",
true
);
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute("1 + x;"), RuntimeError),
"Identifieur indéfini",
true
);
_TEST_ASSERT(
_TEST_IS_EXCEPTION(execute("int x; int x;"), RuntimeError),
"Identifieur déjà défini",
true
);
_TEST_ASSERT(
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("int x; { int x; }"))),
"Identifieur déjà défini dans une autre scope",
true
);
_TEST_ASSERT(
_TEST_NO_EXCEPTION(get<int>(execute("int x = 1; int y; { int x = 2; y = x + 2; }; y;")) == 4),
"Shadowing",
true
);
return 0;
}