c-repl/test/variables.cpp

59 lines
1.4 KiB
C++
Raw Normal View History

2023-11-10 22:24:14 +01:00
#include "include/test.h"
#include "../src/include/memory.h"
2023-11-10 22:24:14 +01:00
#include "../src/include/tokenize.h"
#include "../src/include/parser.h"
2023-12-15 10:38:36 +01:00
#include "../src/include/analysis.h"
2023-11-10 22:24:14 +01:00
#include "../src/include/interpreter.h"
2023-11-22 15:31:30 +01:00
EvalResult execute(string s) {
2023-12-08 15:29:30 +01:00
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-12-15 10:38:36 +01:00
analyze(ast, memory);
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-12-15 15:10:05 +01:00
_TEST_IS_EXCEPTION(execute("1 + x;"), ErrorType::UnknownIdentifier),
2023-11-22 15:31:30 +01:00
"Identifieur indéfini",
2023-11-15 11:59:38 +01:00
true
2023-11-14 20:00:14 +01:00
);
_TEST_ASSERT(
2023-12-15 15:10:05 +01:00
_TEST_IS_EXCEPTION(execute("int x; int x;"), ErrorType::AlreadyDefinedIdentifier),
2023-11-22 15:31:30 +01:00
"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;
}