Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src / main.cpp
diff options
context:
space:
mode:
1 files changed, 36 insertions, 12 deletions
-rw-r--r--JSImpl/src/main.cpp48
diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp
index 586eef1..bd57ce7 100644
--- a/JSImpl/src/main.cpp
+++ b/JSImpl/src/main.cpp
@@ -4,36 +4,44 @@
#include <iostream>
#define LOG(msg) std::cout << msg << std::endl
#define EXECUTE_TEST(test) std::cout << #test << " "; test();
+
#define CHECK(cond) if (cond) {LOG("PASS");} else {LOG(#cond " FAIL");}
// Lexer Tests
void TestLess()
{
- auto tokens = Tokenize("<");
+ IPLVector<Token> tokens;
+ Tokenize("<", tokens);
+
CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::Less);
}
void TestNumber()
{
- auto tokens = Tokenize("213434.24");
+ IPLVector<Token> tokens;
+ Tokenize("213434.24", tokens);
+
CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::Number && tokens[0].Number == 213434.24);
}
void TestNumberStartWithNine()
{
- auto tokens = Tokenize("999");
+ IPLVector<Token> tokens;
+ Tokenize("999", tokens);
CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::Number && tokens[0].Number == 999);
}
void TestNumberStartWithZero()
{
- auto tokens = Tokenize("0999");
+ IPLVector<Token> tokens;
+ Tokenize("0999", tokens);
CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::Number && tokens[0].Number == 999);
}
void TestSpaceNewLineSpace()
{
- auto tokens = Tokenize(" \n var a = 4;");
+ IPLVector<Token> tokens;
+ Tokenize(" \n var a = 4; ", tokens);
CHECK(tokens.size() == 6 && tokens[0].Type == TokenType::Var &&
tokens[1].Type == TokenType::Identifier &&
tokens[2].Type == TokenType::Equal &&
@@ -43,36 +51,53 @@ void TestSpaceNewLineSpace()
void TestString()
{
- auto tokens = Tokenize("\"alabala\"");
+ IPLVector<Token> tokens;
+ Tokenize("\"alabala\"", tokens);
CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::String && tokens[0].Lexeme == "\"alabala\"");
}
void TestStringSingleQuotedStrings()
{
- auto tokens = Tokenize("'alabala'");
+ IPLVector<Token> tokens;
+ Tokenize("'alabala'", tokens);
CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::String && tokens[0].Lexeme == "'alabala'");
}
void TestKeyWord()
{
- auto tokens = Tokenize("for");
+ IPLVector<Token> tokens;
+ Tokenize("for", tokens);
CHECK(tokens.size() == 2 && tokens[0].Type == TokenType::For);
}
void TestVariableDeclaration()
{
- auto tokens = Tokenize("var pesho = 10");
+ IPLVector<Token> tokens;
+ Tokenize("var pesho = 10", tokens);
+
CHECK(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);
}
+
+void TestStringError()
+{
+ IPLVector<Token> tokens;
+ const auto& res = Tokenize("\"aaaa", tokens);
+
+ CHECK(!res.IsSuccessful && res.Error.Row == 0 && res.Error.Column == 5);
+}
+
// Parser Tests
void TestParseUnaryExpr()
{
+ IPLVector<Token> tokens;
+ Tokenize("function pesho() { var a = 0; return a; }", tokens);
+
// TODO make actual test :D
- auto expr = Parse(Tokenize("function pesho(v1) { var a = 0; return a; }"));
+ auto expr = Parse(tokens);
ASTPrinter p(std::cout);
expr->Accept(p);
}
@@ -89,10 +114,9 @@ int main()
//EXECUTE_TEST(TestKeyWord);
//EXECUTE_TEST(TestVariableDeclaration);
-
TestParseUnaryExpr();
#if defined(_WIN32)
- //std::system("pause");
+ std::system("pause");
#endif
return 0;
}