summaryrefslogtreecommitdiff
path: root/js/prolog.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/prolog.js')
-rw-r--r--js/prolog.js225
1 files changed, 104 insertions, 121 deletions
diff --git a/js/prolog.js b/js/prolog.js
index 7ea8ce3..2868830 100644
--- a/js/prolog.js
+++ b/js/prolog.js
@@ -8,80 +8,82 @@
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;
- terminal.resume();
if (data.code === 0) {
t = data.terminal;
- lines = t.messages;
- for (i = 0; i < lines.length; i++) {
- terminal.echo(lines[i]);
- }
+ terminal.append(t.messages.join('\n'), 'output');
promptMode = !t.have_more;
}
else {
- terminal.error(data.message);
+ terminal.append(data.message, 'error');
promptMode = true;
}
+ if (promptMode) {
+ terminal.setLineBuffered();
+ terminal.append('.\n?- ', 'output');
+ }
},
tcf = function terminalCommandFailed (error) {
- terminal.resume();
- terminal.exception(error);
promptMode = true;
- },
- terminal = jqConsole.terminal(function (command, terminal) {
- if (promptMode) {
- terminal.pause();
- codeq.comms.sendQuery({
+ 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 {
+ terminal.append('\n', 'input');
+ if (command == ';') {
+ // show next answer
+ 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).done();
+ 'step': 'next',
+ 'trace': activityHandler.addAndPurge({'typ': 'nxt'})
+ }, problem_id).then(tcs, tcf);
}
else {
- // not in prompt mode -- we should never land here, but handle it anyway
- codeq.comms.sendQuery({
+ // stop searching for answers
+ return codeq.comms.sendQuery({
'problem_id': problem_id,
'step': 'end',
'trace': activityHandler.addAndPurge({'typ': 'stp'})
- }, problem_id).then(tcs, tcf).done();
- }
- }, {
- 'history': true,
- 'prompt': '?- ',
- 'greetings': 'CodeQ prolog terminal proxy',
- 'exit': false,
- 'clear': false,
- 'keypress': function (event, terminal) {
- if (promptMode) return true;
- setTimeout(function () {
- terminal.echo(''); // send newline after semicolon or full-stop
- terminal.pause();
- }, 0);
- if ((event.which == 32) || (event.which == 59) || (event.which == 110)) {
- // space or semicolon or n -> show next answer
- event.which = 59; // semicolon
- codeq.comms.sendQuery({
- 'problem_id': problem_id,
- 'step': 'next',
- 'trace': activityHandler.addAndPurge({'typ': 'nxt'})
- }, problem_id).then(tcs, tcf).done();
- }
- else {
- // everything else: stop searching for answers
- event.which = 46; // full stop
- codeq.comms.sendQuery({
- 'problem_id': problem_id,
- 'step': 'end',
- 'trace': activityHandler.addAndPurge({'typ': 'stp'})
- }, problem_id).then(tcs, tcf).done();
- }
+ }, problem_id).then(tcs, tcf);
}
- });
- return {};
+ }
+ };
+
+ terminal.leftmostCol = 3;
+ terminal.append('?- ', 'output');
+
+ return terminal;
};
var makeActivityHandler = function (editor, problem_id) {
@@ -108,16 +110,11 @@
};
return {
- "trace": function (trace) {
+ 'queueTrace': function (trace) {
trace['dt'] = deltaActivityMillis();
- return trace;
- },
- 'queue': function (trace) {
queue.push(trace);
- if (ts === null) setTimeout(timer, 10000); // flush every 10 seconds
- },
- 'queueTrace': function (trace) {
- this.queue(this.trace(trace));
+ if (ts === null) ts = setTimeout(timer, 10000); // flush every 10 seconds
+ return this;
},
'flush': flush,
'addAndPurge': function (trace) {
@@ -148,21 +145,7 @@
jqHints = $('#info'),
editor = CodeMirror(jqEditor[0], { cursorHeight: 0.85, lineNumbers: true, matchBrackets: true }),
activityHandler = makeActivityHandler(editor, problem.id),
-/* controller = jqConsole.console({
- promptLabel: '?- ',
- commandValidate: function (line) {
- return !!line;
- },
- commandHandle: function (line) {
- return [{msg:'Not implemented.', className:'console-response'}];
- },
- autofocus: false,
- animateScroll: false,
- promptHistory: false,
- welcomeMessage: 'Prolog REPL.'
- }),*/
terminal = makePrologTerminalHandler(jqConsole, editor, problem.id, activityHandler),
- /** Object.<string, HintDefinition> */
hintDefs = problem.hint,
hintCounter = 0, // for generating unique class-names
hintCleaners = [],
@@ -296,6 +279,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
@@ -360,58 +344,57 @@
// $(jqButtons.get(1)).on('click', function () { handler.processServerHints([{id:'popup_unknown', start: 20, end: 26}]); });
// $(jqButtons.get(2)).on('click', function () { handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]); });
- $('#btn_code_run').on('click', function () {
- var doc = editor.getDoc();
-// codeq.comms.sendActivity({'typ': 'slv', 'dt': dt, 'qry': });
-// handler.processServerHints([{id:'list_empty'}]);
- });
- $('#btn_code_break').on('click', function () {
-// handler.processServerHints([{id:'popup_unknown', start: 20, end: 26}]);
- });
$('#btn_code_hint').on('click', function () {
-// handler.processServerHints([{id:'drop_down', start: 20, end: 26, choices:['ena', 'dva', 'tri']}]);
- jqConsole.echo('?- hint.');
- jqConsole.pause();
+ 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) {
- jqConsole.resume();
- if (data.code === 0)
- handler.processServerHints(data.hints);
- else
- jqConsole.error(data.message);
- },
- function hintFailed (error) {
- jqConsole.resume();
- jqConsole.exception(error);
- }
- ).done();
+ '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.pause();
+ 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) {
- jqConsole.resume();
- if (data.code === 0)
- handler.processServerHints(data.hints);
- else
- jqConsole.error(data.message);
- },
- function testFailed (error) {
- jqConsole.resume();
- jqConsole.exception(error);
- }
- ).done();
+ '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;