2023-11-10 22:24:14 +01:00
|
|
|
#include "include/test.h"
|
|
|
|
|
2023-11-22 13:52:16 +01:00
|
|
|
#include "../src/include/memory.h"
|
2023-11-10 22:24:14 +01:00
|
|
|
#include "../src/include/tokenize.h"
|
|
|
|
#include "../src/include/parser.h"
|
|
|
|
#include "../src/include/interpreter.h"
|
|
|
|
|
2023-11-22 15:31:30 +01:00
|
|
|
EvalResult execute(string s) {
|
2023-11-22 14:32:22 +01:00
|
|
|
Memory memory = Memory();
|
2023-11-15 14:31:11 +01:00
|
|
|
vector<Token> tokens = tokenize({ s });
|
2023-11-10 22:24:14 +01:00
|
|
|
Node ast = parse(tokens);
|
2023-11-22 13:52:16 +01:00
|
|
|
EvalResult res = eval(ast, memory);
|
2023-11-15 16:10:36 +01:00
|
|
|
|
2023-11-22 15:31:30 +01:00
|
|
|
return res;
|
2023-11-10 22:24:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
_TEST_PRESENTATION("Variables");
|
|
|
|
|
2023-11-14 17:03:42 +01:00
|
|
|
_TEST_ASSERT(
|
2023-11-22 15:31:30 +01:00
|
|
|
_TEST_NO_EXCEPTION(get<int>(execute("int uneVariableFarfelue__ = 12;")) == 12),
|
2023-11-15 11:59:38 +01:00
|
|
|
"Déclaration avec assignement",
|
|
|
|
true
|
2023-11-14 17:03:42 +01:00
|
|
|
);
|
2023-11-10 22:24:14 +01:00
|
|
|
|
2023-11-14 17:03:42 +01:00
|
|
|
_TEST_ASSERT(
|
2023-11-22 15:31:30 +01:00
|
|
|
_TEST_NO_EXCEPTION(get<int>(execute("int x = 5; x = x + 3;")) == 8),
|
2023-11-15 11:59:38 +01:00
|
|
|
"Déclaration puis assignement",
|
|
|
|
true
|
2023-11-14 17:03:42 +01:00
|
|
|
);
|
2023-11-10 22:24:14 +01:00
|
|
|
|
2023-11-14 20:00:14 +01:00
|
|
|
_TEST_ASSERT(
|
2023-11-22 15:31:30 +01:00
|
|
|
_TEST_IS_EXCEPTION(execute("1 + x;"), RuntimeError),
|
|
|
|
"Identifieur indéfini",
|
2023-11-15 11:59:38 +01:00
|
|
|
true
|
2023-11-14 20:00:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
2023-11-22 15:31:30 +01:00
|
|
|
_TEST_IS_EXCEPTION(execute("int x; int x;"), RuntimeError),
|
|
|
|
"Identifieur déjà défini",
|
2023-11-15 11:59:38 +01:00
|
|
|
true
|
2023-11-14 20:00:14 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
_TEST_ASSERT(
|
2023-11-22 15:31:30 +01:00
|
|
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("int x; { int x; }"))),
|
|
|
|
"Identifieur déjà défini dans une autre scope",
|
2023-11-15 11:59:38 +01:00
|
|
|
true
|
2023-11-14 20:00:14 +01:00
|
|
|
);
|
|
|
|
|
2023-11-15 16:10:36 +01:00
|
|
|
_TEST_ASSERT(
|
2023-11-22 15:31:30 +01:00
|
|
|
_TEST_NO_EXCEPTION(get<int>(execute("int x = 1; int y; { int x = 2; y = x + 2; }; y;")) == 4),
|
|
|
|
"Shadowing",
|
2023-11-15 16:10:36 +01:00
|
|
|
true
|
|
|
|
);
|
|
|
|
|
2023-11-10 22:24:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|