hy/eg/tryhy/js/repl.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2013-10-31 22:30:00 +01:00
$(document).ready(function(){
2013-11-02 00:21:54 +01:00
var backlog = [];
2013-11-02 07:34:27 +01:00
var console = $('#hy-console').console({
promptLabel: '=> ',
2013-10-31 22:30:00 +01:00
commandValidate:function(line){
2013-11-02 00:21:54 +01:00
if (line == '') return false;
2013-10-31 22:30:00 +01:00
else return true;
},
commandHandle:function(line, report){
2013-11-02 00:21:54 +01:00
$.ajax({
type: 'POST',
url: '/eval',
data: JSON.stringify({code: line, env: backlog}),
contentType: 'application/json',
dataType: 'json',
success: function(data) {
report([{msg : data.stdout, className:'jquery-console-message-value'},
{msg : data.stderr, className:'jquery-console-message-error'}]);
}
2013-10-31 22:30:00 +01:00
});
2013-11-02 00:21:54 +01:00
backlog.push(line);
2013-10-31 22:30:00 +01:00
},
animateScroll:true,
promptHistory:true,
autofocus:true,
2013-11-02 07:34:27 +01:00
welcomeMessage: 'hy ({hy_version})'.supplant({hy_version: hy_version})
});
console.promptText('(+ 41 1)');
2013-10-31 22:30:00 +01:00
});
2013-11-02 07:34:27 +01:00
if (!String.prototype.supplant) {
String.prototype.supplant = function (o) {
return this.replace(
/\{([^{}]*)\}/g,
function (a, b) {
var r = o[b];
return typeof r === 'string' || typeof r === 'number' ? r : a;
}
);
};
}