66 lines
1.8 KiB
C++
66 lines
1.8 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("Fonctions");
|
||
|
|
||
|
_TEST_ASSERT(
|
||
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("int a();"))),
|
||
|
"Prototype de fonction sans variables",
|
||
|
true
|
||
|
);
|
||
|
|
||
|
_TEST_ASSERT(
|
||
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("void a(int b);"))),
|
||
|
"Prototype de fonction à une variable",
|
||
|
true
|
||
|
);
|
||
|
|
||
|
_TEST_ASSERT(
|
||
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("double a(int b, double c, int d);"))),
|
||
|
"Prototype de fonction à plusieurs variables",
|
||
|
true
|
||
|
);
|
||
|
|
||
|
_TEST_ASSERT(
|
||
|
_TEST_NO_EXCEPTION(holds_alternative<monostate>(execute("double a(int b, double c, int d) { return b+c+d; };"))),
|
||
|
"Déclaration de fonction",
|
||
|
true
|
||
|
);
|
||
|
|
||
|
_TEST_ASSERT(
|
||
|
_TEST_IS_EXCEPTION(execute("int a(int b) { int c(int d) {return d;} return c(b); };"), ErrorType::NestedFunction),
|
||
|
"Fonctions imbriquées",
|
||
|
true
|
||
|
);
|
||
|
|
||
|
|
||
|
_TEST_ASSERT(
|
||
|
_TEST_NO_EXCEPTION(get<int>(execute("int sum(int a, int b) {return a+b;}; sum(20, 7);")) == 27),
|
||
|
"Exécution de fonction",
|
||
|
true
|
||
|
);
|
||
|
|
||
|
_TEST_ASSERT(
|
||
|
_TEST_NO_EXCEPTION(get<int>(execute("int recurse(int a) {if (a < 27) {return recurse(a+1);} else {return a;}}; recurse(0);")) == 27),
|
||
|
"Fonction récursive",
|
||
|
true
|
||
|
);
|
||
|
|
||
|
return 0;
|
||
|
}
|