66 lines
1.5 KiB
C++
66 lines
1.5 KiB
C++
#include <pslang/parser/parser.hpp>
|
|
#include <pslang/parser/error.hpp>
|
|
#include <pslang/interpreter/interpreter.hpp>
|
|
#include <pslang/ast/statement.hpp>
|
|
#include <pslang/ast/print.hpp>
|
|
|
|
#include <iostream>
|
|
#include <cstring>
|
|
|
|
int main(int argc, char ** argv) try
|
|
{
|
|
if (argc == 1)
|
|
{
|
|
std::cout << "Usage: psli [ options ] <file1> [ <file2> ... ]\n";
|
|
std::cout << "Available options:\n";
|
|
std::cout << " -t, --trace Trace each line of execution\n";
|
|
std::cout << " -d, --dump Dump all variables after processing each file\n";
|
|
std::cout << " -p, --print Print the AST after parsing each file\n";
|
|
return 0;
|
|
}
|
|
|
|
using namespace pslang;
|
|
|
|
auto context = interpreter::empty_context();
|
|
|
|
bool dump = false;
|
|
bool dump_ast = false;
|
|
|
|
for (int arg = 1; arg < argc; ++arg)
|
|
{
|
|
if (std::strcmp(argv[arg], "-d") == 0 || std::strcmp(argv[arg], "--dump") == 0)
|
|
{
|
|
dump = true;
|
|
continue;
|
|
}
|
|
|
|
if (std::strcmp(argv[arg], "-t") == 0 || std::strcmp(argv[arg], "--trace") == 0)
|
|
{
|
|
context.trace = true;
|
|
continue;
|
|
}
|
|
|
|
if (std::strcmp(argv[arg], "-p") == 0 || std::strcmp(argv[arg], "--print") == 0)
|
|
{
|
|
dump_ast = true;
|
|
continue;
|
|
}
|
|
|
|
auto ast = parser::parse(argv[arg]);
|
|
|
|
if (dump_ast)
|
|
{
|
|
ast::print(std::cout, ast);
|
|
std::cout << std::flush;
|
|
}
|
|
|
|
interpreter::execute(context, ast);
|
|
|
|
if (dump)
|
|
interpreter::dump(std::cout, context);
|
|
}
|
|
}
|
|
catch (pslang::parser::parse_error const & error)
|
|
{
|
|
std::cerr << "Parse error at " << error.location() << ":\n " << error.what() << std::endl;
|
|
}
|