From 6e2c30d863cd10337daeb5d22bb2f51cb8c9c56f Mon Sep 17 00:00:00 2001 From: Martin Dobrev Date: Mon, 26 Oct 2020 21:51:03 +0200 Subject: Now JSImpl act like command line tool. --- JSImpl/solution/JSImpl.sln | 5 ++ JSImpl/src/main.cpp | 125 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 126 insertions(+), 4 deletions(-) (limited to 'JSImpl') diff --git a/JSImpl/solution/JSImpl.sln b/JSImpl/solution/JSImpl.sln index bb2638e..ce219bf 100644 --- a/JSImpl/solution/JSImpl.sln +++ b/JSImpl/solution/JSImpl.sln @@ -1,6 +1,8 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 +VisualStudioVersion = 16.0.29613.14 +MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Spasm", "Spasm", "{9892E17D-8434-0C54-6DEF-1FA8593093A4}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "gtest", "gtest", "{1BAF0A7D-0751-3553-F00B-49A7DC4CBCA3}" @@ -147,4 +149,7 @@ Global {3F16CDE1-AB80-8158-F4BE-32FE60685FAD} = {9892E17D-8434-0C54-6DEF-1FA8593093A4} {69185F10-D52C-87C1-9EAE-2A210A8283F2} = {9892E17D-8434-0C54-6DEF-1FA8593093A4} EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B9E3E8B3-5BA4-4521-B598-F1EF432D2126} + EndGlobalSection EndGlobal 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 #include - +#include #include +#include #include #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 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 -- cgit v1.3.1-23-gd82d