--- /dev/null
+#include "Guile.hxx"
+
+namespace {
+ SCM handler (void*, SCM tag, SCM throw_args) {
+ scm_handle_by_message_noexit ((void*)"foo", tag, throw_args);
+ return SCM_BOOL_F;
+ }
+}
+
+GuileInterpreter::GuileInterpreter() {
+ scm_init_guile();
+
+ scm_internal_catch(SCM_BOOL_T,
+ (SCM (*)(void *))scm_c_eval_string,
+ (void*)"(use-modules (system repl server))",
+ handler, NULL);
+ scm_internal_catch(SCM_BOOL_T,
+ (SCM (*)(void *))scm_c_eval_string,
+ (void*)"(spawn-server)",
+ handler, NULL);
+
+ guile_output_port = scm_open_output_string();
+ guile_error_port = scm_open_output_string();
+ scm_set_current_output_port(guile_output_port);
+ scm_set_current_error_port(guile_error_port);
+}
+
+int GuileInterpreter::evaluate(const std::string& command,
+ std::ostream& stdout,
+ std::ostream& stderr,
+ std::string& result) {
+
+ SCM result_obj = scm_internal_catch(SCM_BOOL_T,
+ (SCM (*)(void *))scm_c_eval_string,
+ (void*)command.c_str(),
+ handler, NULL);
+
+ SCM result_str = scm_object_to_string(result_obj, SCM_UNDEFINED);
+
+ SCM output = scm_get_output_string(guile_output_port);
+ stdout << scm_to_locale_string(output);
+
+ output = scm_get_output_string(guile_error_port);
+ stderr << scm_to_locale_string(output);
+
+ result = scm_to_locale_string(result_str);
+
+ scm_truncate_file(guile_output_port, scm_from_uint16(0));
+ scm_truncate_file(guile_error_port, scm_from_uint16(0));
+
+ return 0;
+}
#include "ScriptingDock.hxx"
+#include "bindings/Interpreter.hxx"
-namespace {
- SCM handler (void*, SCM tag, SCM throw_args) {
- scm_handle_by_message_noexit ((void*)"foo", tag, throw_args);
- return SCM_BOOL_F;
- }
-}
+#include <sstream>
-ScriptingDock::ScriptingDock(const QString& title, QWidget * parent)
+ScriptingDock::ScriptingDock(Interpreter* interpreter, const QString& title, QWidget * parent)
: QDockWidget(title, parent)
- , logger(log4cxx::Logger::getLogger("ScriptingDock")) {
+ , logger(log4cxx::Logger::getLogger("ScriptingDock"))
+ , interpreter(interpreter) {
QTabWidget * tab = new QTabWidget;
QWidget * widget = new QWidget;
widget->setLayout(layout = new QGridLayout);
((QDockWidget*)this)->setWidget(tab);
connect(button, SIGNAL(released()), this, SLOT(doEvaluate()));
connect(line, SIGNAL(returnPressed()), this, SLOT(doEvaluate()));
- scm_init_guile();
- scm_internal_catch(SCM_BOOL_T,
- (SCM (*)(void *))scm_c_eval_string,
- (void*)"(use-modules (system repl server))",
- handler, NULL);
- scm_internal_catch(SCM_BOOL_T,
- (SCM (*)(void *))scm_c_eval_string,
- (void*)"(spawn-server)",
- handler, NULL);
-
- guile_output_port = scm_open_output_string();
- guile_error_port = scm_open_output_string();
- scm_set_current_output_port(guile_output_port);
- scm_set_current_error_port(guile_error_port);
}
void ScriptingDock::doEvaluate() {
+ std::stringstream stdout, stderr;
+ std::string result;
+ QString output;
QString text = line->text();
+
line->clear();
LOG4CXX_INFO(logger, "Evaluating String \"" << text.toStdString() << "\"");
browser->append(QString("> ") + text);
- SCM result_obj = scm_internal_catch(SCM_BOOL_T,
- (SCM (*)(void *))scm_c_eval_string,
- (void*)text.toStdString().c_str(),
- handler, NULL);
- SCM result_str = scm_object_to_string(result_obj, SCM_UNDEFINED);
-
- SCM output = scm_get_output_string(guile_output_port);
- QString output_q = scm_to_locale_string(output);
- if (output_q.endsWith("\n")) output_q.chop(1);
- if (output_q != "") browser->append(output_q);
+ interpreter->evaluate(text.toStdString(), stdout, stderr, result);
- output = scm_get_output_string(guile_error_port);
- output_q = scm_to_locale_string(output);
- if (output_q.endsWith("\n")) output_q.chop(1);
- if (output_q != "") browser->append(output_q);
+ output = stdout.str().c_str();
+ if (output.endsWith("\n")) output.chop(1);
+ if (output != "") browser->append(output);
- scm_truncate_file(guile_output_port, scm_from_uint16(0));
- scm_truncate_file(guile_error_port, scm_from_uint16(0));
+ output = stderr.str().c_str();
+ if (output.endsWith("\n")) output.chop(1);
+ if (output != "") browser->append(output);
- browser->append(scm_to_locale_string(result_str));
+ browser->append(result.c_str());
}