Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src / main.cpp
diff options
context:
space:
mode:
Commit12232668d931b0f3593807d2096a2c2f26844b0b (patch)
Author
 Martin Dobrev <martindobrev0@gmail.com>
2018-11-18 20:32:58 +0200
Committer
 GitHub <noreply@github.com>
2018-11-18 20:32:58 +0200
Fileseb05bfcde4de31a03ce033ed479db29c4b527bcc /JSImpl/src/main.cpp
IPL-12232668d931b0f3593807d2096a2c2f26844b0b.tar
IPL-12232668d931b0f3593807d2096a2c2f26844b0b.tar.gz
IPL-12232668d931b0f3593807d2096a2c2f26844b0b.zip
Parent5729bc0ea96fc0b944c243230910c537d692bcad (diff)
Code generation improvements (#56)
* Implement code generation for some unary expressions Implement debug code augmentation. Now part of expresions have source location. Extend Tokens with Column information. Refactor byte code generation. Add options for byte code generator. * Fix linux build * Little refactoring in byte code generation * Fix couple bugs in byte code generation and add some tests. * Fix some linux warnings.
1 files changed, 45 insertions, 3 deletions
-rw-r--r--JSImpl/src/main.cpp48
diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp
index c15458e..90f6023 100644
--- a/JSImpl/src/main.cpp
+++ b/JSImpl/src/main.cpp
@@ -6,6 +6,13 @@
#include <iostream>
#include <cstring>
+#include <sstream>
+#include <iterator>
+
+#if defined(_WIN32)
+#include <Windows.h>
+#endif
+
void RunParseCalc()
{
IPLVector<Token> tokens;
@@ -68,8 +75,43 @@ void Generate()
IPLVector<Token> tokens;
//Tokenize("var a = 0; var b = a + 123;a+b;", tokens);
/*Tokenize("var a; if(a < 4) { a = 8; if(a < 7) {var b = 4;}} else { a = 2} ", tokens);*/
- Tokenize("var i = 0; for (var j = 0; j < 10; j = j + 1) { i = i + j; }", tokens);
- std::cout << GenerateByteCode(Parse(tokens));
+ IPLString source = "var i = 0; \n"
+ "for (var j = 0; j < 10; j++)\n"
+ "{\n"
+ " i = i + j;\n"
+ "}";
+ Tokenize(source.c_str(), tokens);
+
+ auto asmb = GenerateByteCode(Parse(tokens), source,
+ ByteCodeGeneratorOptions(ByteCodeGeneratorOptions::OptimizationsType::None, true));
+
+ //sets the color to intense red on blue background
+#if defined(_WIN32)
+ HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
+ std::istringstream asmbStream(asmb);
+ while (asmbStream.good())
+ {
+ IPLString it;
+ std::getline(asmbStream, it);
+ if (it[0] == 'D')
+ {
+ SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN| BACKGROUND_BLUE | FOREGROUND_INTENSITY);
+ auto dd = it.find("@@=>");
+ std::cout << it.substr(0, dd);
+ SetConsoleTextAttribute(hStdout, FOREGROUND_RED | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
+ std::cout << it.substr(dd, 4);
+ SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
+ std::cout << it.substr(dd + 4, it.size()) << std::endl;
+ }
+ else
+ {
+ std::cout << it << std::endl;
+ }
+ SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
+ }
+#else
+ std::cout << asmb;
+#endif
}
int main()
@@ -78,7 +120,7 @@ int main()
//InteractiveInterpreter();
Generate();
#if defined(_WIN32)
- std::system("pause");
+ //std::system("pause");
#endif
return 0;
}