Files
bfc/src/main.cpp

93 lines
2.7 KiB
C++
Raw Normal View History

2020-09-29 23:16:42 +05:30
#include <iostream>
#include <vector>
#include <string>
2020-10-01 22:03:09 +05:30
#include <cxxopts.hpp>
#include "antlr4-common.h"
#include "cxxopts.hpp"
2020-10-01 13:14:57 +05:30
#include "bfeLexer.h"
#include "bfeParser.h"
2020-09-30 20:26:38 +05:30
#include "toBFListener.hpp"
2020-10-30 20:35:01 +05:30
#include "toCPPListener.hpp"
2020-10-01 19:33:47 +05:30
#include "executeBFE.hpp"
2020-09-30 20:26:38 +05:30
2020-09-29 23:16:42 +05:30
using namespace antlr4;
2020-10-01 22:03:09 +05:30
int main(int argc, char **argv)
2020-09-29 23:16:42 +05:30
{
2020-10-01 22:03:09 +05:30
cxxopts::Options options(argv[0], "An interpreter/translator for bfe");
options.add_options()
2020-10-30 20:35:01 +05:30
("a,action", "Action (exec,cpp,bf)", cxxopts::value<std::string>()->default_value("exec"))
("i,input", "Input (First argument)", cxxopts::value<std::string>())
("h,help", "Print usage");
2020-10-01 22:03:09 +05:30
options.parse_positional({"input"});
2020-10-30 20:35:01 +05:30
// try
{
2020-10-30 20:35:01 +05:30
auto results = options.parse(argc, argv);
std::string action = results["action"].as<std::string>();
if (action.compare("exec") && action.compare("bf") && action.compare("cpp"))
2020-10-01 22:03:09 +05:30
{
2020-10-30 20:35:01 +05:30
action = std::string( "exec");
2020-10-01 22:03:09 +05:30
}
2020-10-30 20:35:01 +05:30
if (results.count("help"))
{
std::cout << options.help() << std::endl;
return 0;
}
if (results.count("input"))
{
// std::cout << results["input"].as<std::string>()<<std::endl;
std::ifstream stream;
bool outFile = false;
stream.open(results["input"].as<std::string>());
if (stream.fail())
{
std::cout << "Could not open" << std::endl;
return 1;
}
ANTLRInputStream input(stream);
bfeLexer lexer(&input);
CommonTokenStream tokens(&lexer);
bfeParser parser(&tokens);
2020-09-29 23:16:42 +05:30
2020-10-30 20:35:01 +05:30
tree::ParseTree *tree = parser.program();
if (action.compare("bf")==0)
{
toBFListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
}
else if (action.compare( "exec")==0)
2020-10-01 22:03:09 +05:30
{
2020-10-30 20:35:01 +05:30
executeBGE *visitor = new executeBGE();
try
{
visitor->visit(tree);
std::cout << std::endl;
}
catch (std::string e)
{
std::cout << "\nIllegal:" << e << std::endl;
}
2020-10-01 22:03:09 +05:30
}
2020-10-30 20:35:01 +05:30
else
2020-10-01 22:03:09 +05:30
{
2020-10-30 20:35:01 +05:30
//else cpp
toCPPListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
2020-10-01 22:03:09 +05:30
}
}
2020-10-30 20:35:01 +05:30
else
{
std::cout << "?:Expected file" << std::endl;
return 1;
}
2020-10-01 19:42:23 +05:30
}
2020-10-30 20:35:01 +05:30
// catch (std::exception e)
// {
// std::cout << "E:" << e.what() << "\n";
// return 1;
// }
2020-10-01 22:03:09 +05:30
// if(argc>2 && )
2020-09-29 23:16:42 +05:30
return 0;
2020-09-29 20:45:00 +05:30
}