Add translation and cxxopts

This commit is contained in:
2020-10-01 22:03:09 +05:30
parent b893608a57
commit 036fefcd1d
8 changed files with 86 additions and 25 deletions

View File

@@ -1,39 +1,70 @@
#include <iostream>
#include <vector>
#include <string>
#include <antlr4-common.h>
#include <cxxopts.hpp>
#include "antlr4-common.h"
#include "cxxopts.hpp"
#include "bfeLexer.h"
#include "bfeParser.h"
#include "toBFListener.hpp"
#include "executeBFE.hpp"
using namespace antlr4;
int main(int argc, const char *argv[])
int main(int argc, char **argv)
{
std::ifstream stream;
bool outFile = false;
stream.open(argv[1]);
if (stream.fail())
cxxopts::Options options(argv[0], "An interpreter/translator for bfe");
options.add_options()
("t,translate", "Translate to bf", cxxopts::value<bool>()->default_value("false"))
("i,input", "Input(First argument)", cxxopts::value<std::string>())
("h,help", "Print usage");
options.parse_positional({"input"});
auto results = options.parse(argc, argv);
if (results.count("help"))
{
std::cout << "Could not open" << std::endl;
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);
tree::ParseTree *tree = parser.program();
if(results["translate"].as<bool>()){
toBFListener listener;
tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
}else{
executeBGE *visitor = new executeBGE();
try
{
visitor->visit(tree);
std::cout << std::endl;
}
catch (std::string e)
{
std::cout << "\nIllegal:" << e<<std::endl;
}
}
}else{
std::cout<<"?:Expected file"<<std::endl;
return 1;
}
ANTLRInputStream input(stream);
bfeLexer lexer(&input);
CommonTokenStream tokens(&lexer);
bfeParser parser(&tokens);
tree::ParseTree *tree = parser.program();
// toBFListener listener;
// tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);
executeBGE* visitor = new executeBGE();
try{
visitor->visit(tree);
std::cout<<std::endl;
}catch(std::string e){
std::cout<<"Illegal:"<<e<<std::endl;
}
// if(argc>2 && )
return 0;
}