#include #include #include "DragonLispDriver.h" namespace DragonLisp { DLDriver::~DLDriver() { delete (this->scanner); this->scanner = nullptr; delete (this->parser); this->parser = nullptr; } int DLDriver::parse(const std::string& f) { std::ifstream in(f); if (!in.good()) { std::printf("Could not open file %s\n", f.c_str()); return 1; } return this->parse(in, f); } int DLDriver::parse(std::istream& in, const std::string& s) { // Scanner delete this->scanner; this->scanner = new DLScanner(&in); // Parser delete this->parser; this->parser = new DLParser(*this->scanner, *this); // Execution Context delete this->context; this->context = new Context(nullptr); this->parser->set_debug_level(1); return this->parser->parse(); } std::shared_ptr DLDriver::constructLValueAST(std::string name) { return std::make_shared(std::move(name)); } std::shared_ptr DLDriver::constructLValueAST(std::string name, std::shared_ptr index) { return std::make_shared(std::move(name), std::move(index)); } std::shared_ptr DLDriver::constructFuncDefAST(std::string name, std::vector args, std::vector> body) { return std::make_shared(std::move(name), std::move(args), std::move(body)); } std::shared_ptr DLDriver::constructLiteralAST(bool value) { return std::make_shared(value); } std::shared_ptr DLDriver::constructLiteralAST(std::int64_t value) { return std::make_shared(value); } std::shared_ptr DLDriver::constructLiteralAST(double value) { return std::make_shared(value); } std::shared_ptr DLDriver::constructLiteralAST(std::string value) { return std::make_shared(std::move(value)); } std::shared_ptr DLDriver::constructBinaryExprAST(std::shared_ptr lhs, std::shared_ptr rhs, Token op) { return std::make_shared(std::move(lhs), std::move(rhs), op); } std::shared_ptr DLDriver::constructUnaryExprAST(std::shared_ptr expr, Token op) { return std::make_shared(std::move(expr), op); } std::shared_ptr DLDriver::constructListExprAST(std::vector> exprs, Token op) { return std::make_shared(std::move(exprs), op); } std::shared_ptr DLDriver::constructIfAST(std::shared_ptr cond, std::shared_ptr then, std::shared_ptr els) { return std::make_shared(std::move(cond), std::move(then), std::move(els)); } std::shared_ptr DLDriver::constructFuncCallAST(std::string name, std::vector> args) { return std::make_shared(std::move(name), std::move(args)); } std::shared_ptr DLDriver::constructVarOpAST(std::string name, std::shared_ptr value, Token op) { return std::make_shared(std::move(name), std::move(value), op); } std::shared_ptr DLDriver::constructLValOpAST(std::shared_ptr lval, std::shared_ptr value, Token op) { return std::make_shared(std::move(lval), std::move(value), op); } std::shared_ptr DLDriver::constructReturnAST(std::shared_ptr value) { return std::make_shared(std::move(value)); } std::shared_ptr DLDriver::constructReturnAST(std::shared_ptr value, std::string name) { return std::make_shared(std::move(value), std::move(name)); } std::shared_ptr DLDriver::constructLoopAST(std::vector> body) { return std::make_shared( std::move(body) ); } std::shared_ptr DLDriver::constructLoopAST(std::string id, std::shared_ptr to, std::vector> body) { return std::make_shared( std::move(id), std::move(to), std::move(body) ); } std::shared_ptr DLDriver::constructLoopAST(std::string id, std::shared_ptr from, std::shared_ptr to, std::vector> body) { return std::make_shared( std::move(id), std::move(from), std::move(to), std::move(body) ); } void DLDriver::execute(std::variant , std::shared_ptr> ast) { if (ast.index() == 0) { // ExprAST auto expr = std::get<0>(ast); expr->eval(this->context); } else { // ast.index() == 1, FuncDefAST auto func = std::get<1>(ast); this->context->setFunc(func->getName(), func); } } } // end namespace DragonLisp