Skip to content
Snippets Groups Projects
Commit e9a7287f authored by deuce's avatar deuce
Browse files

Start of the JavaScript BAJA parser/interpreter.

Be afraid.
parent 7477cafd
Branches
Tags
No related merge requests found
/* bajavascript.c */
/* Synchronet command shell/module compiler */
/* $Id$ */
load("bajalib.js");
function BAJAVA_cmd(cmd_name)
{
if(baja[cmd_name.toUpperCase()]==undefined)
throw("Undefined BAJA command "+cmd_name.toUpperCase());
this.cmd=baja[cmd_name.toUpperCase()];
this.args=[];
}
function BAJAVA_program()
{
this.cmds=[];
this.defines={};
this.globals={};
this.vars={};
this.labels={};
this.stack=[];
}
function BAJAVA_compile(path, program)
{
var cmds=[];
var infile;
var str;
var defines={};
var m;
var tmp;
var lnum=0;
if(path.search(/^(?:[A-Z]:)?[\/\\]/)!=0)
path=system.exec_dir+path;
infile=new File(path);
if(program==undefined)
program=new BAJAVA_program();
function expdefs(str) {
var ret='';
var i;
var quoting=false;
var dname;
for(i=0; i<str.length; i++) {
if(quoting) {
ret += str.charAt(i);
if(str.charAt(i) == '"')
quoting=false;
continue;
}
if(str.charAt(i)=='"') {
ret += '"';
continue;
}
if(str.charAt(i)==' ') {
ret += ' ';
continue;
}
dname='';
for(;i<str.length;i++) {
if(str.charAt(i).search(/[A-Za-z0-9_]/)==0) {
dname += str.charAt(i);
}
else {
break;
}
}
if(dname.length > 0) {
if(program.defines[dname] != undefined) {
ret += program.defines[dname];
}
else {
ret += dname;
}
}
ret += str.charAt(i);
}
return ret;
}
if(!infile.open("rb")) {
print(format("error %d opening %s for read\n",infile.error,path));
exit(1);
}
while(!(infile.eof || infile.error)) {
str=infile.readln(1000);
lnum++;
if(str==undefined)
break;
str=truncsp(str);
str=str.replace(/\t/g,' ');
str=str.replace(/^[\x00- ]*/g,'');
if(str.length==0)
continue;
if(str.charAt(0)=='#')
continue;
str=expdefs(str);
// TODO: This may be easier to use a standard parser...
m=str.match(/([^ ]+)((?: [\x00- ]*([^ ]+)(?: [\x00- ]*([^ ]+)(?: [\x00- ]*([^ ]+)(?: [\x00- ]*([^ ]+))?)?)?)?.*?)$/);
if(m==null)
throw("Unhandled command "+str);
m[1]=m[1].toUpperCase();
switch(m[1].charAt(0)) {
case '!':
switch(m[1].substr(1)) {
case 'INCLUDE':
BAJAVA_compile(m[3], program);
break;
case 'DEFINE':
break;
case 'GLOBAL':
break;
}
break;
case ':':
tmp=m[1].substr(1);
if(program.labels[tmp]!=undefined)
throw("Duplicate label "+tmp+" defined at "+path+":"+lnum);
program.labels[tmp]=program.cmds.length;
break;
default:
program.cmds.push(new BAJAVA_cmd(m[1], m.slice(3)));
}
}
return program;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment