From e9a7287fc23b9665ac79443b4cfb1720dfb56b85 Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Tue, 4 Feb 2014 08:27:17 +0000
Subject: [PATCH] Start of the JavaScript BAJA parser/interpreter. Be afraid.

---
 exec/bajavascript.js | 132 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
 create mode 100644 exec/bajavascript.js

diff --git a/exec/bajavascript.js b/exec/bajavascript.js
new file mode 100644
index 0000000000..0c0e4134cc
--- /dev/null
+++ b/exec/bajavascript.js
@@ -0,0 +1,132 @@
+/* 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;
+}
-- 
GitLab