Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src / main.cpp
diff options
context:
space:
mode:
1 files changed, 94 insertions, 0 deletions
-rw-r--r--JSImpl/src/main.cpp94
diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp
new file mode 100644
index 0000000..e2fc448
--- /dev/null
+++ b/JSImpl/src/main.cpp
@@ -0,0 +1,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;
+} \ No newline at end of file