mirror of
https://github.com/avinal/xeus-basic.git
synced 2026-01-10 23:28:36 +05:30
refactor BASIC interpreter code
Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>
This commit is contained in:
@@ -4,15 +4,15 @@ SPDX-FileCopyrightText: 2021 Avinal Kumar <avinal.xlvii@gmail.com>
|
|||||||
#]=======================================================================]
|
#]=======================================================================]
|
||||||
|
|
||||||
cmake_minimum_required(VERSION 3.16)
|
cmake_minimum_required(VERSION 3.16)
|
||||||
project(basic LANGUAGES C CXX)
|
project(basic LANGUAGES C)
|
||||||
|
|
||||||
file(GLOB BASIC_SRC src/*.c)
|
file(GLOB BASIC_SRC src/*.c)
|
||||||
add_library(basic "")
|
add_library(basic "")
|
||||||
target_sources(basic PRIVATE ${BASIC_SRC})
|
target_sources(basic PRIVATE ${BASIC_SRC})
|
||||||
target_include_directories(basic PRIVATE include/basic)
|
target_include_directories(basic PRIVATE include/basic)
|
||||||
|
|
||||||
add_executable(basic_exec main.cc)
|
#add_executable(basic_exec main.cc)
|
||||||
target_include_directories(basic_exec PRIVATE include/basic include)
|
#target_include_directories(basic_exec PRIVATE include/basic)
|
||||||
target_link_libraries(basic_exec PRIVATE basic)
|
#target_link_libraries(basic_exec PRIVATE basic)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,60 +0,0 @@
|
|||||||
/**
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
* SPDX-FileCopyrightText: 2021 Avinal Kumar <avinal.xlvii@gmail.com>
|
|
||||||
*
|
|
||||||
* @file basic.hpp
|
|
||||||
* @brief Wrapper around basic
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef BASIC_BASIC_HPP_
|
|
||||||
#define BASIC_BASIC_HPP_
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
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<char*>(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_
|
|
||||||
57
libs/basic/include/basic/basic.hpp
Normal file
57
libs/basic/include/basic/basic.hpp
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
/**
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
* SPDX-FileCopyrightText: 2021 Avinal Kumar <avinal.xlvii@gmail.com>
|
||||||
|
*
|
||||||
|
* @file basic.hpp
|
||||||
|
* @brief Wrapper around basic
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BASIC_BASIC_HPP_
|
||||||
|
#define BASIC_BASIC_HPP_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
extern "C" {
|
||||||
|
#include "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_init(1024 * 8, 2048);
|
||||||
|
basic_register_io(out, in);
|
||||||
|
}
|
||||||
|
|
||||||
|
~basic() { basic_destroy(); }
|
||||||
|
|
||||||
|
bool evaluate_line(const std::string& line) {
|
||||||
|
char* input = const_cast<char*>(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 get_error() { return last_error; }
|
||||||
|
|
||||||
|
std::string run() {
|
||||||
|
evaluate_line("RUN");
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_output() { output = ""; }
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // BASIC_BASIC_HPP_
|
||||||
@@ -8,9 +8,9 @@
|
|||||||
#ifndef BASIC_PARSER_H_
|
#ifndef BASIC_PARSER_H_
|
||||||
#define BASIC_PARSER_H_
|
#define BASIC_PARSER_H_
|
||||||
|
|
||||||
#include <io.h>
|
#include "io.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <tokenizer.h>
|
#include "tokenizer.h"
|
||||||
|
|
||||||
float evaluate(char* expression_string);
|
float evaluate(char* expression_string);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user