Add multiline program support

Signed-off-by: Avinal Kumar <avinal.xlvii@gmail.com>
Co-authored-by: Sarita Singh <saritasingh.0425@gmail.com>
This commit is contained in:
2021-11-02 21:25:23 +05:30
parent 01a5a46191
commit ba9e34d5cb
3 changed files with 52 additions and 17 deletions

View File

@@ -1,11 +1,12 @@
#ifndef XBASIC_INTERPRETER_HPP_
#define XBASIC_INTERPRETER_HPP_
#include <iostream>
#include "basic/basic.hpp"
#include "nlohmann/json.hpp"
#include "xeus/xinterpreter.hpp"
using xeus::xinterpreter;
namespace nl = nlohmann;
@@ -13,7 +14,8 @@ namespace xbasic {
class xbasic_interpreter : public xinterpreter {
private:
basic code_runner;
basic* code_runner = nullptr;
public:
xbasic_interpreter() = default;

View File

@@ -3,17 +3,21 @@
#include "xbasic/xbasic_interpreter.hpp"
#include "xeus/xkernel.hpp"
#include "xeus/xkernel_configuration.hpp"
#include "xeus/xserver_zmq.hpp"
int main(int argc, char const *argv[]) {
// Load configuration file
std::string file_name = (argc == 1) ? "connection.json" : argv[2];
xeus::xconfiguration config = xeus::load_configuration(file_name);
auto context = xeus::make_context<zmq::context_t>();
// Create interpreter instance
using interpreter_ptr = std::unique_ptr<xbasic::xbasic_interpreter>;
interpreter_ptr interpreter = std::make_unique<xbasic::xbasic_interpreter>();
// Create kernel instance and start it
xeus::xkernel kernel(config, xeus::get_user_name(), std::move(interpreter));
xeus::xkernel kernel(config, xeus::get_user_name(), std::move(context), std::move(interpreter),
xeus::make_xserver_zmq);
kernel.start();
return 0;
}

View File

@@ -1,3 +1,4 @@
#include "xbasic/xbasic_interpreter.hpp"
std::string basic::output = "";
@@ -5,8 +6,9 @@ std::string basic::output = "";
namespace xbasic {
nl::json xbasic_interpreter::execute_request_impl(int execution_counter, const std::string &code,
bool silent, bool store_history,
nl::json user_expressions, bool allow_stdin) {
bool /*silent*/, bool /*store_history*/,
nl::json /*user_expressions*/,
bool /*allow_stdin*/) {
auto ok = []() {
nl::json result;
result["status"] = "ok";
@@ -25,36 +27,63 @@ namespace xbasic {
return result;
};
auto dispose = [&]() {
delete code_runner;
code_runner = nullptr;
};
auto init_basic = [&]() { code_runner = new basic(); };
if (!code_runner) {
init_basic();
}
nl::json pub_data;
bool is_complete = false;
try {
if (code_runner.evaluate_line(code)) {
pub_data["text/plain"] = code_runner.run();
code_runner.clear_output();
publish_execution_result(execution_counter, std::move(pub_data), nl::json::object());
return ok();
} else {
throw(code_runner.get_error());
std::string line;
std::stringstream to_line(code);
while (std::getline(to_line, line, '\n')) {
std::cout << line << "\n";
if (line == "CLEAR" || line == "RUN") {
is_complete = true;
std::cout << "Disposing basic...\n";
} else if (!code_runner->evaluate_line(line)) {
std::cout << "Throw error\n";
throw(code_runner->get_error());
}
}
std::string result = code_runner->run();
code_runner->clear_output();
if (is_complete) {
dispose();
result += "\n\n Clearing Scope\n";
}
pub_data["text/plain"] = result;
publish_execution_result(execution_counter, std::move(pub_data), nl::json::object());
return ok();
} catch (const std::exception &err) {
std::cout << "Check error\n";
return handle_exception(err.what());
}
return ok();
}
nl::json xbasic_interpreter::complete_request_impl(const std::string &code, int cursor_pos) {
nl::json xbasic_interpreter::complete_request_impl(const std::string & /*code*/,
int /*cursor_pos*/) {
nl::json result;
result["status"] = "ok";
return result;
}
nl::json xbasic_interpreter::inspect_request_impl(const std::string &code, int cursor_pos,
int detail_level) {
nl::json xbasic_interpreter::inspect_request_impl(const std::string & /*code*/,
int /*cursor_pos*/, int /*detail_level*/) {
nl::json result;
result["status"] = "ok";
return result;
}
nl::json xbasic_interpreter::is_complete_request_impl(const std::string &code) {
nl::json xbasic_interpreter::is_complete_request_impl(const std::string & /*code*/) {
nl::json result;
result["status"] = "complete";
return result;
@@ -73,7 +102,7 @@ namespace xbasic {
`VA ,V' MMooon* ,M `MM `YMMNq. MM MM
XMX MM `Y. AbmmmqMA . `MM MM MM.
,V' VA. MM ,9 A' VML Mb dM MM `Mb. .'
.AM. .MA..JMMonal.AMA. .AMMA.`*Ybmmd* .JMML. `*bMond^
.AM. .MA..JMMonald'AMA. .AMMA.`*Ybmmd* .JMML. `*bMond^
xeus-basic: a Jupyter Kernel for BASIC Language
xBASIC Version: 0.1.0")V0G0N";