Website Summary Files Commits Branches @commit @diff @stats
path: root / JSImpl / src / main.cpp
diff options
context:
space:
mode:
Commit6e2c30d863cd10337daeb5d22bb2f51cb8c9c56f (patch)
Author
 Martin Dobrev <martindobrev0@gmail.com>
2020-10-26 21:51:03 +0200
Committer
 Martin Dobrev <martindobrev0@gmail.com>
2020-10-26 21:51:03 +0200
Filesae877349f5baee2c6ae93da5ca2d8a9cdee7f43f /JSImpl/src/main.cpp
IPL-6e2c30d863cd10337daeb5d22bb2f51cb8c9c56f.tar
IPL-6e2c30d863cd10337daeb5d22bb2f51cb8c9c56f.tar.gz
IPL-6e2c30d863cd10337daeb5d22bb2f51cb8c9c56f.zip
Parent8452476baf9eba44319c7c00530efc0ce89ebcf0 (diff)
Now JSImpl act like command line tool.
1 files changed, 121 insertions, 4 deletions
-rw-r--r--JSImpl/src/main.cpp125
diff --git a/JSImpl/src/main.cpp b/JSImpl/src/main.cpp
index 3a6b84e..c1cfe64 100644
--- a/JSImpl/src/main.cpp
+++ b/JSImpl/src/main.cpp
@@ -5,8 +5,9 @@
#include "ByteCodeGenerator.h"
#include <iostream>
#include <cstring>
-
+#include <unordered_map>
#include <sstream>
+#include <fstream>
#include <iterator>
#if defined(_WIN32)
@@ -110,10 +111,126 @@ void Generate()
#endif
}
-int main()
+
+class CammandLineApp
+{
+public:
+ CammandLineApp()
+ : m_PrintAst(false)
+ , m_PrintTokens(false)
+ {
+ m_CommandMapping["--output"] = CommandType::Output;
+ m_CommandMapping["--input"] = CommandType::Input;
+ m_CommandMapping["--ast"] = CommandType::PrintAST;
+ m_CommandMapping["--tokens"] = CommandType::PrintTokens;
+ m_CommandMapping["--help"] = CommandType::Help;
+ }
+
+ enum class CommandType
+ {
+ Output,
+ Input,
+ PrintAST,
+ PrintTokens,
+ Help
+ };
+
+ void PrintHelp()
+ {
+ std::cout << "Supports following arguments:" << std::endl
+ << "--output - output file path" << std::endl
+ << "--input - input file path" << std::endl
+ << "--ast - will write ast of the input in the output" << std::endl
+ << "--tokens - will tokenize input file and will write the result as json in the output" << std::endl
+ << "--help - this message" << std::endl;
+ }
+
+ void ParseArguments(int argcount, char* arguments[])
+ {
+ for (int i = 1; i < argcount; )
+ {
+ auto it = m_CommandMapping.find(arguments[i]);
+ if (it == m_CommandMapping.end())
+ {
+ PrintHelp();
+ }
+ auto command = it->second;
+ ++i;
+ switch (command)
+ {
+ case CommandType::Output:
+ assert(i < argcount);
+ m_Output = arguments[i];
+ ++i;
+ break;
+ case CommandType::Input:
+ assert(i < argcount);
+ m_Input = arguments[i];
+ ++i;
+ break;
+ case CommandType::PrintAST:
+ m_PrintAst = true;
+ break;
+ case CommandType::PrintTokens:
+ m_PrintTokens = true;
+ break;
+ case CommandType::Help:
+ default:
+ PrintHelp();
+ return;
+ }
+ }
+ }
+
+ void ExecuteCommands()
+ {
+ std::ifstream input(m_Input.c_str());
+ input.seekg(0, std::ios::end);
+ std::string code;
+ int size = (int)input.tellg();
+ code.resize(size);
+ input.seekg(0, std::ios::beg);
+ input.read(&code[0], code.size());
+ input.close();
+
+ auto tokenizationResult = Tokenize(code.c_str());
+ if (!tokenizationResult.IsSuccessful)
+ {
+ return;
+ }
+ auto programAst = Parse(tokenizationResult.tokens);
+ if (m_PrintAst)
+ {
+ std::ofstream output(m_Output, std::ofstream::trunc);
+ PrintAST(programAst, output);
+ output.close();
+ }
+
+ if (m_PrintTokens)
+ {
+
+ }
+ }
+
+ void RunAsCommandLine(int argcount, char* arguments[])
+ {
+ ParseArguments(argcount, arguments);
+ ExecuteCommands();
+ }
+private:
+ std::string m_Output;
+ std::string m_Input;
+ bool m_PrintAst;
+ bool m_PrintTokens;
+ std::unordered_map<std::string, CommandType> m_CommandMapping;
+};
+
+int main(int argc, char* argv[])
{
- auto r = Tokenize("a#4", {true ,true});
- RunParseCalc();
+ CammandLineApp cmd;
+ cmd.RunAsCommandLine(argc, argv);
+ //auto r = Tokenize("a#4", {true ,true});
+ //RunParseCalc();
#if defined(_WIN32)
//std::system("pause");
#endif