blob: e2fc448e2781c1fd4182d78bae84f415ab2652a1 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include "Lexer.h"
#include "Parser.h"
#include <iostream>
#define LOG(msg) std::cout << msg << std::endl
#define EXECUTE_TEST(test) std::cout << #test << " "; test();
// Lexer Tests
void TestLess()
{
auto tokens = Tokenize("<");
if (tokens.size() == 2 && tokens[0].Type == TokenType::Less)
{
LOG("PASS");
}
else
{
LOG("FAIL");
}
}
void TestNumber()
{
auto tokens = Tokenize("213434.24");
if (tokens.size() == 2 && tokens[0].Type == TokenType::Number && tokens[0].Number == 213434.24)
{
LOG("PASS");
}
else
{
LOG("FAIL");
}
}
void TestString()
{
auto tokens = Tokenize("\"alabala\"");
if (tokens.size() == 2 && tokens[0].Type == TokenType::String && tokens[0].Lexeme == "\"alabala\"")
{
LOG("PASS");
}
else
{
LOG("FAIL");
}
}
void TestKeyWord()
{
auto tokens = Tokenize("for");
if (tokens.size() == 2 && tokens[0].Type == TokenType::For)
{
LOG("PASS");
}
else
{
LOG("FAIL");
}
}
void TestVariableDeclaration()
{
auto tokens = Tokenize("var pesho = 10");
if (tokens.size() == 5 && tokens[0].Type == TokenType::Var
&& tokens[1].Type == TokenType::Identifier
&& tokens[2].Type == TokenType::Equal
&& tokens[3].Type == TokenType::Number
&& tokens[4].Type == TokenType::Eof)
{
LOG("PASS");
}
else
{
LOG("FAIL");
}
}
// Parser Tests
void TestParseUnaryExpr()
{
// TODO make actual test :D
auto expr = Parse(Tokenize("function pesho() { var a = 0; return a; }"));
expr->Print(std::cout);
}
int main()
{
EXECUTE_TEST(TestLess);
EXECUTE_TEST(TestNumber);
EXECUTE_TEST(TestString);
EXECUTE_TEST(TestKeyWord);
EXECUTE_TEST(TestVariableDeclaration);
EXECUTE_TEST(TestParseUnaryExpr);
std::system("pause");
return 0;
}
|