summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorAleš Smodiš <aless@guru.si>2015-09-15 20:06:01 +0200
committerAleš Smodiš <aless@guru.si>2015-09-15 20:06:01 +0200
commit3c02044761366fe474c7bf8cb5ba7307960c788c (patch)
tree9a894b324bc75ab6734a264961b2b16f9effd272 /js
parent6bb35042ef6fb19d9af5ee874de9da2816cbc5d7 (diff)
Prolog handler now uses CodeQ terminal.
Diffstat (limited to 'js')
-rw-r--r--js/prolog.js140
-rw-r--r--js/python.js9
2 files changed, 140 insertions, 9 deletions
diff --git a/js/prolog.js b/js/prolog.js
index 7ea8ce3..cc46736 100644
--- a/js/prolog.js
+++ b/js/prolog.js
@@ -6,7 +6,7 @@
// a constant
var firstCharacterPos = {'line': 0, 'ch': 0};
- var makePrologTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) {
+/* var makePrologTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) {
var promptMode = true, // default: query composition; alternative: query result browsing
tcs = function terminalCommandSuccess (data) {
var t, lines, i;
@@ -82,6 +82,85 @@
});
return {};
+ };*/
+
+ var makePrologTerminalHandler = function (jqConsole, editor, problem_id, activityHandler) {
+ var promptMode = true, // default: query composition; alternative: query result browsing
+ terminal = codeq.makeConsole(jqConsole, {
+ 'greeting': 'CodeQ Prolog terminal proxy'
+ }),
+ tcs = function terminalCommandSuccess (data) {
+ var t, lines, i;
+ if (data.code === 0) {
+ t = data.terminal;
+ terminal.append(t.messages.join('\n') + '\n', 'output');
+ promptMode = !t.have_more;
+ }
+ else {
+ terminal.error(data.message + '\n', 'error');
+ promptMode = true;
+ }
+ if (promptMode) {
+ terminal.setLineBuffered();
+ terminal.append('?- ', 'output');
+ }
+ },
+ tcf = function terminalCommandFailed (error) {
+ promptMode = true;
+ terminal.setLineBuffered();
+ terminal.append(error + '\n', 'error');
+ terminal.append('?- ', 'output');
+ };
+
+ terminal.onKeypress = function (c) {
+ if (promptMode) return c; // query composition: return the character unchanged
+ switch (c) {
+ case ' ':
+ case ';':
+ case '\n':
+ return ';'; // show next answer on space, semicolon or enter
+ default:
+ return '.'; // everything else: stop searching for answers
+ }
+ };
+
+ terminal.onInput = function (command) {
+ if (promptMode) {
+ promptMode = false;
+ terminal.setNotBuffered();
+ return codeq.comms.sendQuery({
+ 'problem_id': problem_id,
+ 'step': 'run',
+ 'program': editor.getDoc().getValue(),
+ 'query': command,
+ 'trace': activityHandler.addAndPurge({'typ': 'slv', 'qry': command})
+ }, problem_id).then(tcs, tcf);
+ }
+ else {
+ if (command == ';') {
+ // show next answer
+ return codeq.comms.sendQuery({
+ 'problem_id': problem_id,
+ 'step': 'next',
+ 'trace': activityHandler.addAndPurge({'typ': 'nxt'})
+ }, problem_id).then(tcs, tcf);
+ }
+ else {
+ // stop searching for answers
+ return codeq.comms.sendQuery({
+ 'problem_id': problem_id,
+ 'step': 'end',
+ 'trace': activityHandler.addAndPurge({'typ': 'stp'})
+ }, problem_id).then(tcs, tcf);
+ }
+
+ }
+ };
+
+ terminal.leftmostCol = 3;
+ terminal.append('?- ', 'output');
+
+ return terminal;
};
var makeActivityHandler = function (editor, problem_id) {
@@ -296,6 +375,7 @@
var handler = {
destroy: function () {
+ terminal.destroy();
jqDescription.empty();
jqEditor.empty(); // TODO: perhaps you do not want to "free" the editor, just empty it
jqConsole.empty(); // TODO: the same with the console
@@ -370,7 +450,7 @@
});
$('#btn_code_hint').on('click', function () {
// handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]);
- jqConsole.echo('?- hint.');
+/* jqConsole.echo('?- hint.');
jqConsole.pause();
var doc = editor.getDoc();
codeq.comms.sendHint({
@@ -389,10 +469,35 @@
jqConsole.resume();
jqConsole.exception(error);
}
- ).done();
+ ).done();*/
+
+ terminal.append('hint.\n', 'input');
+ terminal.inputDisable();
+ var doc = editor.getDoc();
+ codeq.comms.sendHint({
+ 'language': 'prolog',
+ 'program': editor.getDoc().getValue(),
+ 'problem_id': problem.id
+ })
+ .then(
+ function hintSuccess(data) {
+ if (data.code === 0)
+ handler.processServerHints(data.hints);
+ else
+ terminal.append(data.message + '\n', 'error');
+ },
+ function hintFailed (error) {
+ terminal.append(error + '\n', 'error');
+ }
+ )
+ .fin(function () {
+ terminal.inputEnable();
+ terminal.append('?- ', 'output');
+ })
+ .done();
});
$('#btn_code_test').on('click', function () {
- jqConsole.echo('?- test.');
+/* jqConsole.echo('?- test.');
jqConsole.pause();
var doc = editor.getDoc();
codeq.comms.sendTest({
@@ -411,7 +516,32 @@
jqConsole.resume();
jqConsole.exception(error);
}
- ).done();
+ ).done();*/
+
+ terminal.append('test.\n', 'input');
+ terminal.inputDisable();
+ var doc = editor.getDoc();
+ codeq.comms.sendTest({
+ 'language': 'prolog',
+ 'program': editor.getDoc().getValue(),
+ 'problem_id': problem.id
+ })
+ .then(
+ function testSuccess(data) {
+ if (data.code === 0)
+ handler.processServerHints(data.hints);
+ else
+ terminal.append(data.message + '\n', 'error');
+ },
+ function testFailed (error) {
+ terminal.append(error + '\n', 'error');
+ }
+ )
+ .fin(function () {
+ terminal.inputEnable();
+ terminal.append('?- ', 'output');
+ })
+ .done();
});
return handler;
diff --git a/js/python.js b/js/python.js
index d16cc72..82423c6 100644
--- a/js/python.js
+++ b/js/python.js
@@ -48,11 +48,11 @@
return terminal;
};
- var makeActivityHandler = function (editor) {
+ var makeActivityHandler = function (editor, problem_id) {
var lastActivityMillis = Date.now(),
deltaActivityMillis = function deltaActivityMillisFunc () {
var now = Date.now(),
- dt = Math.max(0, Math.min(30000, now - lastActivityMillis)); // 0 sec <= dt <= 30 sec
+ dt = now - lastActivityMillis;
lastActivityMillis = now;
return dt;
},
@@ -62,7 +62,7 @@
var promise;
ts = null;
if (queue.length === 0) return Q(true);
- promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue());
+ promise = codeq.comms.sendActivity(queue, editor.getDoc().getValue(), problem_id);
queue.length = 0;
return promise;
},
@@ -111,7 +111,7 @@
jqConsole = $('#console'),
jqHints = $('#info'),
editor = CodeMirror(jqEditor[0], { cursorHeight: 0.85, lineNumbers: true, matchBrackets: true, mode: 'python' }),
- activityHandler = makeActivityHandler(editor),
+ activityHandler = makeActivityHandler(editor, problem.id),
terminal = makePythonTerminalHandler(jqConsole, editor, problem.id, activityHandler),
/** Object.<string, HintDefinition> */
@@ -248,6 +248,7 @@
var handler = {
destroy: function () {
+ terminal.destroy();
jqDescription.empty();
jqEditor.empty(); // TODO: perhaps you do not want to "free" the editor, just empty it
jqConsole.empty(); // TODO: the same with the console