Added "NEW" command to demo BASIC interpreter. Still not very useful, but we

now have some of the basic framework in place to implement a minimalistic
interpreter.


git-svn-id: https://shellinabox.googlecode.com/svn/trunk@102 0da03de8-d603-11dd-86c2-0f8696b7b6f9
This commit is contained in:
zodiac 2009-03-30 16:55:00 +00:00
parent 81845fd5f7
commit 312b2853f5
5 changed files with 183 additions and 97 deletions

View file

@ -95,7 +95,7 @@
#define STDC_HEADERS 1 #define STDC_HEADERS 1
/* Most recent revision number in the version control system */ /* Most recent revision number in the version control system */
#define VCS_REVISION "101" #define VCS_REVISION "102"
/* Version number of package */ /* Version number of package */
#define VERSION "2.5" #define VERSION "2.5"

2
configure vendored
View file

@ -2056,7 +2056,7 @@ ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $
ac_compiler_gnu=$ac_cv_c_compiler_gnu ac_compiler_gnu=$ac_cv_c_compiler_gnu
VCS_REVISION=101 VCS_REVISION=102
cat >>confdefs.h <<_ACEOF cat >>confdefs.h <<_ACEOF

View file

@ -2,7 +2,7 @@ AC_PREREQ(2.57)
dnl This is the one location where the authoritative version number is stored dnl This is the one location where the authoritative version number is stored
AC_INIT(shellinabox, 2.5, markus@shellinabox.com) AC_INIT(shellinabox, 2.5, markus@shellinabox.com)
VCS_REVISION=101 VCS_REVISION=102
AC_SUBST(VCS_REVISION) AC_SUBST(VCS_REVISION)
AC_DEFINE_UNQUOTED(VCS_REVISION, "${VCS_REVISION}", AC_DEFINE_UNQUOTED(VCS_REVISION, "${VCS_REVISION}",
[Most recent revision number in the version control system]) [Most recent revision number in the version control system])

View file

@ -69,8 +69,8 @@
// #define STATE_READLINE 3 // #define STATE_READLINE 3
// #define STATE_COMMAND 4 // #define STATE_COMMAND 4
// #define STATE_EVAL 5 // #define STATE_EVAL 5
// #define STATE_RUN 6 // #define STATE_EXEC 6
// #define STATE_EXEC 7 // #define STATE_NEW_Y_N 7
function extend(subClass, baseClass) { function extend(subClass, baseClass) {
function inheritance() { } function inheritance() { }
@ -87,6 +87,16 @@ function Demo(container) {
extend(Demo, VT100); extend(Demo, VT100);
Demo.prototype.keysPressed = function(ch) { Demo.prototype.keysPressed = function(ch) {
if (this.state == 6 /* STATE_EXEC */) {
for (var i = 0; i < ch.length; i++) {
var c = ch.charAt(i);
if (c == '\u0003') {
this.keys = '';
this.error('Interrupted');
return;
}
}
}
this.keys += ch; this.keys += ch;
this.gotoState(this.state); this.gotoState(this.state);
}; };
@ -97,7 +107,7 @@ Demo.prototype.gotoState = function(state, tmo) {
if (!tmo) { if (!tmo) {
tmo = 1; tmo = 1;
} }
this.timer = setTimeout(function(demo) { this.nextTimer = setTimeout(function(demo) {
return function() { return function() {
demo.demo(); demo.demo();
}; };
@ -107,35 +117,31 @@ Demo.prototype.gotoState = function(state, tmo) {
Demo.prototype.demo = function() { Demo.prototype.demo = function() {
var done = false; var done = false;
this.nextTimer = undefined;
while (!done) { while (!done) {
var state = this.state; var state = this.state;
this.state = 0 /* STATE_IDLE */; this.state = 0 /* STATE_IDLE */;
switch (state) { switch (state) {
case 1 /* STATE_INIT */: case 1 /* STATE_INIT */:
this.doInit(); done = this.doInit();
break; break;
case 2 /* STATE_PROMPT */: case 2 /* STATE_PROMPT */:
this.doPrompt(); done = this.doPrompt();
break; break;
case 3 /* STATE_READLINE */: case 3 /* STATE_READLINE */:
this.doReadLine(); done = this.doReadLine();
if (this.state == 3 /* STATE_READLINE */) {
done = true;
}
break; break;
case 4 /* STATE_COMMAND */: case 4 /* STATE_COMMAND */:
this.doCommand(); done = this.doCommand();
break; break;
case 5 /* STATE_EVAL */: case 5 /* STATE_EVAL */:
this.doEval(); done = this.doEval();
break; break;
case 6 /* STATE_RUN */: case 6 /* STATE_EXEC */:
this.doRun(); done = this.doExec();
break; break;
case 7 /* STATE_EXEC */: case 7 /* STATE_NEW_Y_N */:
if (this.doExec()) { done = this.doNewYN();
return;
}
break; break;
case 0 /* STATE_IDLE */: case 0 /* STATE_IDLE */:
default: default:
@ -143,15 +149,22 @@ Demo.prototype.demo = function() {
break; break;
} }
} }
this.timer = undefined; this.timer = this.nextTimer;
this.nextTimer = undefined;
}; };
Demo.prototype.ok = function() {
this.vt100('OK\r\n');
this.gotoState(2 /* STATE_PROMPT */);
}
Demo.prototype.error = function(msg) { Demo.prototype.error = function(msg) {
if (msg == undefined) { if (msg == undefined) {
msg = 'Syntax Error'; msg = 'Syntax Error';
} }
this.vt100('\u0007? ' + msg + '\r\n'); this.vt100('\u0007? ' + msg + '\r\n');
this.gotoState(2 /* STATE_PROMPT */); this.gotoState(2 /* STATE_PROMPT */);
this.currentLineIndex = -1;
}; };
Demo.prototype.doInit = function() { Demo.prototype.doInit = function() {
@ -170,6 +183,7 @@ Demo.prototype.doInit = function() {
'Type HELP for a list of commands.\r\n' + 'Type HELP for a list of commands.\r\n' +
'\r\n'); '\r\n');
this.gotoState(2 /* STATE_PROMPT */); this.gotoState(2 /* STATE_PROMPT */);
return false;
}; };
Demo.prototype.doPrompt = function() { Demo.prototype.doPrompt = function() {
@ -178,6 +192,7 @@ Demo.prototype.doPrompt = function() {
this.currentLineIndex = -1; this.currentLineIndex = -1;
this.vt100('> '); this.vt100('> ');
this.gotoState(3 /* STATE_READLINE */); this.gotoState(3 /* STATE_READLINE */);
return false;
}; };
Demo.prototype.doReadLine = function() { Demo.prototype.doReadLine = function() {
@ -192,6 +207,7 @@ Demo.prototype.doReadLine = function() {
} else if (ch == '\r' || ch == '\n') { } else if (ch == '\r' || ch == '\n') {
this.vt100('\r\n'); this.vt100('\r\n');
this.gotoState(4 /* STATE_COMMAND */); this.gotoState(4 /* STATE_COMMAND */);
return false;
} else if (ch == '\u0008' || ch == '\u007F') { } else if (ch == '\u0008' || ch == '\u007F') {
if (this.line.length > 0) { if (this.line.length > 0) {
this.line = this.line.substr(0, this.line.length - 1); this.line = this.line.substr(0, this.line.length - 1);
@ -212,6 +228,7 @@ Demo.prototype.doReadLine = function() {
break; break;
} }
} }
return true;
}; };
Demo.prototype.doCommand = function() { Demo.prototype.doCommand = function() {
@ -244,8 +261,9 @@ Demo.prototype.doCommand = function() {
this.tokens = tokens; this.tokens = tokens;
this.gotoState(5 /* STATE_EVAL */); this.gotoState(5 /* STATE_EVAL */);
} }
tokens.reset();
} }
tokens.reset();
return false;
}; };
Demo.prototype.doEval = function() { Demo.prototype.doEval = function() {
@ -253,11 +271,10 @@ Demo.prototype.doEval = function() {
var cmd = this.tokens.nextToken().toUpperCase(); var cmd = this.tokens.nextToken().toUpperCase();
if (cmd == "HELP") { if (cmd == "HELP") {
this.vt100('Supported commands:\r\n' + this.vt100('Supported commands:\r\n' +
' HELP LIST RUN\r\n'); ' HELP LIST NEW RUN\r\n');
} else if (cmd == "LIST") { } else if (cmd == "LIST") {
if (this.tokens.nextToken() != undefined) { if (this.tokens.nextToken() != undefined) {
this.error(); this.error();
return false;
} else { } else {
for (var i = 0; i < this.program.length; i++) { for (var i = 0; i < this.program.length; i++) {
var line = this.program[i]; var line = this.program[i];
@ -270,39 +287,65 @@ Demo.prototype.doEval = function() {
this.vt100('\r\n'); this.vt100('\r\n');
} }
} }
} else if (cmd == "NEW") {
if (this.currentLineIndex >= 0) {
this.error('Cannot call NEW from a program');
} else if (this.program.length == 0) {
this.ok();
} else {
this.vt100('Do you really want to delete the program (y/N) ');
this.gotoState(7 /* STATE_NEW_Y_N */);
}
} else if (cmd == "RUN") { } else if (cmd == "RUN") {
if (this.tokens.nextToken() != undefined) { if (this.tokens.nextToken() != undefined) {
this.error(); this.error();
} else if (this.program.length > 0) {
this.currentLineIndex = 0;
this.gotoState(6 /* STATE_EXEC */);
} else { } else {
this.gotoState(6 /* STATE_RUN */); this.ok();
} }
return false;
} else { } else {
this.error(); this.error();
}
return false; return false;
}
return true;
};
Demo.prototype.doRun = function() {
if (this.program.length > 0) {
this.currentLineIndex = 0;
this.gotoState(7 /* STATE_EXEC */);
}
}; };
Demo.prototype.doExec = function() { Demo.prototype.doExec = function() {
this.tokens = this.program[this.currentLineIndex++].tokens(); this.tokens = this.program[this.currentLineIndex++].tokens();
this.tokens.reset(); this.tokens.reset();
if (this.doEval()) { this.doEval();
if (this.currentLineIndex >= this.program.length) { if (this.currentLineIndex < 0) {
this.gotoState(2 /* STATE_PROMPT */); return false;
} else if (this.currentLineIndex >= this.program.length) {
this.ok();
return false; return false;
} else { } else {
this.gotoState(7 /* STATE_EXEC */, 20); this.gotoState(6 /* STATE_EXEC */, 20);
return true; return true;
} }
};
Demo.prototype.doNewYN = function() {
for (var i = 0; i < this.keys.length; ) {
var ch = this.keys.charAt(i++);
if (ch == 'n' || ch == 'N' || ch == '\r' || ch == '\n') {
this.vt100('N\r\n');
this.keys = this.keys.substr(i);
this.error('Aborted');
return false;
} else if (ch == 'y' || ch == 'Y') {
this.vt100('Y\r\n');
this.program.splice(0, this.program.length);
this.keys = this.keys.substr(i);
this.ok();
return false;
} else {
this.vt100('\u0007');
} }
}
this.gotoState(7 /* STATE_NEW_Y_N */);
return true;
}; };
Demo.prototype.findLine = function(lineNumber) { Demo.prototype.findLine = function(lineNumber) {

View file

@ -69,8 +69,8 @@
#define STATE_READLINE 3 #define STATE_READLINE 3
#define STATE_COMMAND 4 #define STATE_COMMAND 4
#define STATE_EVAL 5 #define STATE_EVAL 5
#define STATE_RUN 6 #define STATE_EXEC 6
#define STATE_EXEC 7 #define STATE_NEW_Y_N 7
function extend(subClass, baseClass) { function extend(subClass, baseClass) {
function inheritance() { } function inheritance() { }
@ -87,6 +87,16 @@ function Demo(container) {
extend(Demo, VT100); extend(Demo, VT100);
Demo.prototype.keysPressed = function(ch) { Demo.prototype.keysPressed = function(ch) {
if (this.state == STATE_EXEC) {
for (var i = 0; i < ch.length; i++) {
var c = ch.charAt(i);
if (c == '\u0003') {
this.keys = '';
this.error('Interrupted');
return;
}
}
}
this.keys += ch; this.keys += ch;
this.gotoState(this.state); this.gotoState(this.state);
}; };
@ -97,7 +107,7 @@ Demo.prototype.gotoState = function(state, tmo) {
if (!tmo) { if (!tmo) {
tmo = 1; tmo = 1;
} }
this.timer = setTimeout(function(demo) { this.nextTimer = setTimeout(function(demo) {
return function() { return function() {
demo.demo(); demo.demo();
}; };
@ -107,35 +117,31 @@ Demo.prototype.gotoState = function(state, tmo) {
Demo.prototype.demo = function() { Demo.prototype.demo = function() {
var done = false; var done = false;
this.nextTimer = undefined;
while (!done) { while (!done) {
var state = this.state; var state = this.state;
this.state = STATE_IDLE; this.state = STATE_IDLE;
switch (state) { switch (state) {
case STATE_INIT: case STATE_INIT:
this.doInit(); done = this.doInit();
break; break;
case STATE_PROMPT: case STATE_PROMPT:
this.doPrompt(); done = this.doPrompt();
break; break;
case STATE_READLINE: case STATE_READLINE:
this.doReadLine(); done = this.doReadLine();
if (this.state == STATE_READLINE) {
done = true;
}
break; break;
case STATE_COMMAND: case STATE_COMMAND:
this.doCommand(); done = this.doCommand();
break; break;
case STATE_EVAL: case STATE_EVAL:
this.doEval(); done = this.doEval();
break;
case STATE_RUN:
this.doRun();
break; break;
case STATE_EXEC: case STATE_EXEC:
if (this.doExec()) { done = this.doExec();
return; break;
} case STATE_NEW_Y_N:
done = this.doNewYN();
break; break;
case STATE_IDLE: case STATE_IDLE:
default: default:
@ -143,15 +149,22 @@ Demo.prototype.demo = function() {
break; break;
} }
} }
this.timer = undefined; this.timer = this.nextTimer;
this.nextTimer = undefined;
}; };
Demo.prototype.ok = function() {
this.vt100('OK\r\n');
this.gotoState(STATE_PROMPT);
}
Demo.prototype.error = function(msg) { Demo.prototype.error = function(msg) {
if (msg == undefined) { if (msg == undefined) {
msg = 'Syntax Error'; msg = 'Syntax Error';
} }
this.vt100('\u0007? ' + msg + '\r\n'); this.vt100('\u0007? ' + msg + '\r\n');
this.gotoState(STATE_PROMPT); this.gotoState(STATE_PROMPT);
this.currentLineIndex = -1;
}; };
Demo.prototype.doInit = function() { Demo.prototype.doInit = function() {
@ -170,6 +183,7 @@ Demo.prototype.doInit = function() {
'Type HELP for a list of commands.\r\n' + 'Type HELP for a list of commands.\r\n' +
'\r\n'); '\r\n');
this.gotoState(STATE_PROMPT); this.gotoState(STATE_PROMPT);
return false;
}; };
Demo.prototype.doPrompt = function() { Demo.prototype.doPrompt = function() {
@ -178,6 +192,7 @@ Demo.prototype.doPrompt = function() {
this.currentLineIndex = -1; this.currentLineIndex = -1;
this.vt100('> '); this.vt100('> ');
this.gotoState(STATE_READLINE); this.gotoState(STATE_READLINE);
return false;
}; };
Demo.prototype.doReadLine = function() { Demo.prototype.doReadLine = function() {
@ -192,6 +207,7 @@ Demo.prototype.doReadLine = function() {
} else if (ch == '\r' || ch == '\n') { } else if (ch == '\r' || ch == '\n') {
this.vt100('\r\n'); this.vt100('\r\n');
this.gotoState(STATE_COMMAND); this.gotoState(STATE_COMMAND);
return false;
} else if (ch == '\u0008' || ch == '\u007F') { } else if (ch == '\u0008' || ch == '\u007F') {
if (this.line.length > 0) { if (this.line.length > 0) {
this.line = this.line.substr(0, this.line.length - 1); this.line = this.line.substr(0, this.line.length - 1);
@ -212,6 +228,7 @@ Demo.prototype.doReadLine = function() {
break; break;
} }
} }
return true;
}; };
Demo.prototype.doCommand = function() { Demo.prototype.doCommand = function() {
@ -244,8 +261,9 @@ Demo.prototype.doCommand = function() {
this.tokens = tokens; this.tokens = tokens;
this.gotoState(STATE_EVAL); this.gotoState(STATE_EVAL);
} }
tokens.reset();
} }
tokens.reset();
return false;
}; };
Demo.prototype.doEval = function() { Demo.prototype.doEval = function() {
@ -253,11 +271,10 @@ Demo.prototype.doEval = function() {
var cmd = this.tokens.nextToken().toUpperCase(); var cmd = this.tokens.nextToken().toUpperCase();
if (cmd == "HELP") { if (cmd == "HELP") {
this.vt100('Supported commands:\r\n' + this.vt100('Supported commands:\r\n' +
' HELP LIST RUN\r\n'); ' HELP LIST NEW RUN\r\n');
} else if (cmd == "LIST") { } else if (cmd == "LIST") {
if (this.tokens.nextToken() != undefined) { if (this.tokens.nextToken() != undefined) {
this.error(); this.error();
return false;
} else { } else {
for (var i = 0; i < this.program.length; i++) { for (var i = 0; i < this.program.length; i++) {
var line = this.program[i]; var line = this.program[i];
@ -270,39 +287,65 @@ Demo.prototype.doEval = function() {
this.vt100('\r\n'); this.vt100('\r\n');
} }
} }
} else if (cmd == "NEW") {
if (this.currentLineIndex >= 0) {
this.error('Cannot call NEW from a program');
} else if (this.program.length == 0) {
this.ok();
} else {
this.vt100('Do you really want to delete the program (y/N) ');
this.gotoState(STATE_NEW_Y_N);
}
} else if (cmd == "RUN") { } else if (cmd == "RUN") {
if (this.tokens.nextToken() != undefined) { if (this.tokens.nextToken() != undefined) {
this.error(); this.error();
} else { } else if (this.program.length > 0) {
this.gotoState(STATE_RUN);
}
return false;
} else {
this.error();
return false;
}
return true;
};
Demo.prototype.doRun = function() {
if (this.program.length > 0) {
this.currentLineIndex = 0; this.currentLineIndex = 0;
this.gotoState(STATE_EXEC); this.gotoState(STATE_EXEC);
} else {
this.ok();
} }
} else {
this.error();
}
return false;
}; };
Demo.prototype.doExec = function() { Demo.prototype.doExec = function() {
this.tokens = this.program[this.currentLineIndex++].tokens(); this.tokens = this.program[this.currentLineIndex++].tokens();
this.tokens.reset(); this.tokens.reset();
if (this.doEval()) { this.doEval();
if (this.currentLineIndex >= this.program.length) { if (this.currentLineIndex < 0) {
this.gotoState(STATE_PROMPT); return false;
} else if (this.currentLineIndex >= this.program.length) {
this.ok();
return false; return false;
} else { } else {
this.gotoState(STATE_EXEC, 20); this.gotoState(STATE_EXEC, 20);
return true; return true;
} }
};
Demo.prototype.doNewYN = function() {
for (var i = 0; i < this.keys.length; ) {
var ch = this.keys.charAt(i++);
if (ch == 'n' || ch == 'N' || ch == '\r' || ch == '\n') {
this.vt100('N\r\n');
this.keys = this.keys.substr(i);
this.error('Aborted');
return false;
} else if (ch == 'y' || ch == 'Y') {
this.vt100('Y\r\n');
this.program.splice(0, this.program.length);
this.keys = this.keys.substr(i);
this.ok();
return false;
} else {
this.vt100('\u0007');
} }
}
this.gotoState(STATE_NEW_Y_N);
return true;
}; };
Demo.prototype.findLine = function(lineNumber) { Demo.prototype.findLine = function(lineNumber) {