From 7f9b359c8e32bef9e76bcd3d1e76bcb82154bd06 Mon Sep 17 00:00:00 2001 From: Avinal Kumar Date: Tue, 3 Aug 2021 22:28:31 +0530 Subject: [PATCH] add wrapper around basic interpreter Signed-off-by: Avinal Kumar --- libs/basic/CMakeLists.txt | 18 ++++++ libs/basic/include/basic.hpp | 60 +++++++++++++++++++ libs/basic/main.cc | 112 +++++++++++++++++++++++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 libs/basic/CMakeLists.txt create mode 100644 libs/basic/include/basic.hpp create mode 100644 libs/basic/main.cc diff --git a/libs/basic/CMakeLists.txt b/libs/basic/CMakeLists.txt new file mode 100644 index 0000000..e6d2238 --- /dev/null +++ b/libs/basic/CMakeLists.txt @@ -0,0 +1,18 @@ +#[=======================================================================[ +SPDX-License-Identifier: Apache-2.0 +SPDX-FileCopyrightText: 2021 Avinal Kumar +#]=======================================================================] + +cmake_minimum_required(VERSION 3.16) +project(basic LANGUAGES C CXX) + +file(GLOB BASIC_SRC src/*.c) +add_library(basic "") +target_sources(basic PRIVATE ${BASIC_SRC}) +target_include_directories(basic PRIVATE include/basic) + +add_executable(basic_exec main.cc) +target_include_directories(basic_exec PRIVATE include/basic include) +target_link_libraries(basic_exec PRIVATE basic) + + diff --git a/libs/basic/include/basic.hpp b/libs/basic/include/basic.hpp new file mode 100644 index 0000000..edd21a8 --- /dev/null +++ b/libs/basic/include/basic.hpp @@ -0,0 +1,60 @@ +/** + * SPDX-License-Identifier: Apache-2.0 + * SPDX-FileCopyrightText: 2021 Avinal Kumar + * + * @file basic.hpp + * @brief Wrapper around basic + */ + +#ifndef BASIC_BASIC_HPP_ +#define BASIC_BASIC_HPP_ + +#include +extern "C" { +#include "basic/parser.h" +} + +class basic { +private: + std::string last_error = ""; + static std::string output; + static int out(int ch) { + output.push_back(ch); + return 1; + } + + static int in() { return getchar(); } + +public: + basic(); + ~basic(); + bool evaluate_line(const std::string& line); + std::string run(); + std::string get_error(); +}; + +std::string basic::output = ""; + +basic::basic() { + basic_init(1024 * 8, 2048); + basic_register_io(out, in); +} + +basic::~basic() { basic_destroy(); } + +bool basic::evaluate_line(const std::string& line) { + char* input = const_cast(line.c_str()); + basic_eval(input); + if (evaluate_last_error()) { + last_error.append(evaluate_last_error()); + clear_last_error(); + return false; + } + return true; +} + +std::string basic::get_error() { return last_error; } + +std::string basic::run() { return output; } + +#endif // BASIC_BASIC_HPP_ \ No newline at end of file diff --git a/libs/basic/main.cc b/libs/basic/main.cc new file mode 100644 index 0000000..33020c6 --- /dev/null +++ b/libs/basic/main.cc @@ -0,0 +1,112 @@ +/** + * SPDX-License-Identifier: Apache-2.0 + * SPDX-FileCopyrightText: 2021 Avinal Kumar + * + * @file main.cc + * @brief Driver Code + */ + +// #include +// #include +// #include +// #include +// #include +// #include +// #include + +#include +#include +#include + +#include "basic.hpp" + +// extern bool __RUNNING; +// extern bool __STOPPED; + +// static void sigint_handler(int signum) { +// signal(SIGINT, sigint_handler); +// if (__RUNNING) { +// __RUNNING = false; +// __STOPPED = true; +// printf("STOP\n"); +// fflush(stdout); +// } +// } + +// static char *readline_gets() { +// char *line_read = readline(""); + +// if (line_read && *line_read) { +// add_history(line_read); +// } + +// return line_read; +// } +std::string outputs; +// int out(int ch) { +// outputs += char(ch); +// return 1; +// } + +// int in(void) { return getchar(); } + +void replcc() { + std::cout << " _ _ \n" + << "| |__ __ _ ___(_) ___ \n" + << "| '_ \\ / _` / __| |/ __|\n" + << "| |_) | (_| \\__ \\ | (__ \n" + << "|_.__/ \\__,_|___/_|\\___|\n" + << "(c) 2015-2016 Johan Van den Brande\n"; + // using_history(); + std::string input; + basic check; + while (getline(std::cin, input)) { + if (!input.compare("QUIT")) { + // check.~basic(); + break; + } + if (!check.evaluate_line(input)) { + std::cerr << check.get_error() << "\n"; + } + // basic_eval((char *)input.c_str()); + if (input == "RUN") { + std::cout << check.run(); + } + // if (evaluate_last_error()) { + // std::cerr << "ERROR: " << evaluate_last_error() << "\n"; + // clear_last_error(); + // } + } + // clear_history(); +} + +void runcc(std::string filename) { + // std::fstream file(filename, std::ios::in); + + // if (!file.is_open()) { + // std::cerr << "Can't open " << filename << std::endl; + // exit(EXIT_FAILURE); + // } + // std::string line; + // while (getline(file, line)) { + // basic_eval((char *)line.c_str()); + // } + // file.close(); + // basic_run(); +} + +int main(int argc, char *argv[]) { + // signal(SIGINT, sigint_handler); + // basic_init(1024 * 8, 2048); + // basic_register_io(out, in); + + if (argc > 1) { + runcc(argv[1]); + } else { + replcc(); + } + + // basic_destroy(); + + return EXIT_SUCCESS; +}