mirror of
https://github.com/avinal/xeus-basic.git
synced 2026-01-10 23:28:36 +05:30
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:
@@ -1,11 +1,12 @@
|
|||||||
#ifndef XBASIC_INTERPRETER_HPP_
|
#ifndef XBASIC_INTERPRETER_HPP_
|
||||||
#define XBASIC_INTERPRETER_HPP_
|
#define XBASIC_INTERPRETER_HPP_
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#include "basic/basic.hpp"
|
#include "basic/basic.hpp"
|
||||||
#include "nlohmann/json.hpp"
|
#include "nlohmann/json.hpp"
|
||||||
#include "xeus/xinterpreter.hpp"
|
#include "xeus/xinterpreter.hpp"
|
||||||
|
|
||||||
|
|
||||||
using xeus::xinterpreter;
|
using xeus::xinterpreter;
|
||||||
namespace nl = nlohmann;
|
namespace nl = nlohmann;
|
||||||
|
|
||||||
@@ -13,7 +14,8 @@ namespace xbasic {
|
|||||||
|
|
||||||
class xbasic_interpreter : public xinterpreter {
|
class xbasic_interpreter : public xinterpreter {
|
||||||
private:
|
private:
|
||||||
basic code_runner;
|
basic* code_runner = nullptr;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
xbasic_interpreter() = default;
|
xbasic_interpreter() = default;
|
||||||
|
|
||||||
|
|||||||
@@ -3,17 +3,21 @@
|
|||||||
#include "xbasic/xbasic_interpreter.hpp"
|
#include "xbasic/xbasic_interpreter.hpp"
|
||||||
#include "xeus/xkernel.hpp"
|
#include "xeus/xkernel.hpp"
|
||||||
#include "xeus/xkernel_configuration.hpp"
|
#include "xeus/xkernel_configuration.hpp"
|
||||||
|
#include "xeus/xserver_zmq.hpp"
|
||||||
|
|
||||||
int main(int argc, char const *argv[]) {
|
int main(int argc, char const *argv[]) {
|
||||||
// Load configuration file
|
// Load configuration file
|
||||||
std::string file_name = (argc == 1) ? "connection.json" : argv[2];
|
std::string file_name = (argc == 1) ? "connection.json" : argv[2];
|
||||||
xeus::xconfiguration config = xeus::load_configuration(file_name);
|
xeus::xconfiguration config = xeus::load_configuration(file_name);
|
||||||
|
|
||||||
|
auto context = xeus::make_context<zmq::context_t>();
|
||||||
// Create interpreter instance
|
// Create interpreter instance
|
||||||
using interpreter_ptr = std::unique_ptr<xbasic::xbasic_interpreter>;
|
using interpreter_ptr = std::unique_ptr<xbasic::xbasic_interpreter>;
|
||||||
interpreter_ptr interpreter = std::make_unique<xbasic::xbasic_interpreter>();
|
interpreter_ptr interpreter = std::make_unique<xbasic::xbasic_interpreter>();
|
||||||
|
|
||||||
// Create kernel instance and start it
|
// 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();
|
kernel.start();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
#include "xbasic/xbasic_interpreter.hpp"
|
#include "xbasic/xbasic_interpreter.hpp"
|
||||||
|
|
||||||
std::string basic::output = "";
|
std::string basic::output = "";
|
||||||
@@ -5,8 +6,9 @@ std::string basic::output = "";
|
|||||||
namespace xbasic {
|
namespace xbasic {
|
||||||
|
|
||||||
nl::json xbasic_interpreter::execute_request_impl(int execution_counter, const std::string &code,
|
nl::json xbasic_interpreter::execute_request_impl(int execution_counter, const std::string &code,
|
||||||
bool silent, bool store_history,
|
bool /*silent*/, bool /*store_history*/,
|
||||||
nl::json user_expressions, bool allow_stdin) {
|
nl::json /*user_expressions*/,
|
||||||
|
bool /*allow_stdin*/) {
|
||||||
auto ok = []() {
|
auto ok = []() {
|
||||||
nl::json result;
|
nl::json result;
|
||||||
result["status"] = "ok";
|
result["status"] = "ok";
|
||||||
@@ -25,36 +27,63 @@ namespace xbasic {
|
|||||||
return result;
|
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;
|
nl::json pub_data;
|
||||||
|
bool is_complete = false;
|
||||||
try {
|
try {
|
||||||
if (code_runner.evaluate_line(code)) {
|
std::string line;
|
||||||
pub_data["text/plain"] = code_runner.run();
|
std::stringstream to_line(code);
|
||||||
code_runner.clear_output();
|
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());
|
publish_execution_result(execution_counter, std::move(pub_data), nl::json::object());
|
||||||
return ok();
|
return ok();
|
||||||
} else {
|
|
||||||
throw(code_runner.get_error());
|
|
||||||
}
|
|
||||||
} catch (const std::exception &err) {
|
} catch (const std::exception &err) {
|
||||||
|
std::cout << "Check error\n";
|
||||||
return handle_exception(err.what());
|
return handle_exception(err.what());
|
||||||
}
|
}
|
||||||
return ok();
|
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;
|
nl::json result;
|
||||||
result["status"] = "ok";
|
result["status"] = "ok";
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
nl::json xbasic_interpreter::inspect_request_impl(const std::string &code, int cursor_pos,
|
nl::json xbasic_interpreter::inspect_request_impl(const std::string & /*code*/,
|
||||||
int detail_level) {
|
int /*cursor_pos*/, int /*detail_level*/) {
|
||||||
nl::json result;
|
nl::json result;
|
||||||
result["status"] = "ok";
|
result["status"] = "ok";
|
||||||
return result;
|
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;
|
nl::json result;
|
||||||
result["status"] = "complete";
|
result["status"] = "complete";
|
||||||
return result;
|
return result;
|
||||||
@@ -73,7 +102,7 @@ namespace xbasic {
|
|||||||
`VA ,V' MMooon* ,M `MM `YMMNq. MM MM
|
`VA ,V' MMooon* ,M `MM `YMMNq. MM MM
|
||||||
XMX MM `Y. AbmmmqMA . `MM MM MM.
|
XMX MM `Y. AbmmmqMA . `MM MM MM.
|
||||||
,V' VA. MM ,9 A' VML Mb dM MM `Mb. .'
|
,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
|
xeus-basic: a Jupyter Kernel for BASIC Language
|
||||||
xBASIC Version: 0.1.0")V0G0N";
|
xBASIC Version: 0.1.0")V0G0N";
|
||||||
|
|||||||
Reference in New Issue
Block a user