Scheme-like Script Interpreter

From Hack Wars Wiki

Jump to: navigation, search
Information for the Scheme-like Script Interpreter (HTTP Version) Script
Script Scheme-like Script Interpreter (HTTP Version)
API HTTP
Cost $2,335
Level 40 HTTP
CPU Usage 12
Description This script will execute a script (in a Scheme-like syntax) which is written in a file, allowing you to change your script any time without paying money to recompile. I encourage anyone with programming experience to improve upon this code. I may continue to update it periodically as well.

I have split the functions section of the code from the rest for convenience.

For the HTTP version, the Enter and Submit parts of it are mostly the same; for the Submit part, just comment out some functions and uncomment others, and change the filename being used to "submit.txt."

Output N/A


Contents

Optimization

I hope that some people may try to improve the code. One thing that may be improved is simply the amount of operations it uses. In Hack Wars, there is a limit to how many operations can occur before a script dies. Use the following code to test whether your changes to the script successfully improved the efficiency or not:

(define int i 1)
(while (= 1 1)
	(logMessage (intToStr i))
	(define int i (+ i 1))
	)

Currently, the code will loop successfully 61 times on a "subscribed" account.

In comparison, the following hackscript code will loop 8191 times before dying on a "subscribed" account.

int main(){
	int i=1;
	while(1==1) {
		logMessage(i+"");
		i++;
	}
}


The Code

Please note: This code does NOT include the eval() functions. You must copy in one of the sets of code (such as HTTP Functions) for the script interpreter to work.

string varData="//"; //This is used to store what I call 'Params' (which is anything found in parenthesis). Stored in the form "ParamName/ParamValue//"
boolean boolVars[]; //Stores boolean variable values
int intVars[]; //Stores integer variable values
 
string strNames="//"; //This actually stores all string variable data (names and values). Sorry the name is misleading.
string boolNames="//"; //Stores names of boolean variables, in the form "name/arrayNumber//"
string intNames="//"; //Stores names of integer variables.
 
//These funcData variables store functions.
string strFuncData="////";
string intFuncData="////";
string boolFuncData="////";
string voidFuncData="////";
 
int funcCalls[];
string funcC="";
int calls=0;
int pCount=0;
int varCount=0;
int strCount=0;
string numbers="0123456789";
int cStart=0;
string scriptFile="script.txt";
string dataFile="script.data";
string callData="";
 
string newLine=char(10);
string quote=char(34);
 
int main() {
	boolean useFiles=true;
 
	string script=readFile(scriptFile);
	if(useFiles==false) mainProcess(script, false);
	else {
		string pFile=readFile(dataFile);
		string backup=substr(pFile, indexOf(pFile, "!~~backupS~~!", 0)+13, indexOf(pFile, "!~~backupE~~!", 0));
 
		if(backup!=script) mainProcess(script, true);
		else { //load more stuff from file
			varData=substr(pFile, indexOf(pFile, "!~~paramS~~!", 0)+12, indexOf(pFile, "!~~paramE~~!", 0));
			callData=substr(pFile, indexOf(pFile, "!~~callsS~~!", 0)+12, indexOf(pFile, "!~~callsE~~!", 0));
			strNames=substr(pFile, indexOf(pFile, "!~~strNameS~~!", 0)+14, indexOf(pFile, "!~~strNameE~~!", 0));
			strNames+="visitorIP/"+getVIP()+"//hostIP/"+getHIP()+"//";
			//theLog("Proceeding with "+strCount(callData, "/")+" function calls.");
			evalParams(true);
		}
	}
}
void mainProcess(string script, boolean save) {
	if(calls>0) cStart=calls;
	string orScript=script;
	if(strCount(script, "(")==strCount(script, ")")) {
		if( strCount(script, quote)%2==0) { //if script has even number of quotation marks
			//script=strFind(script);
			string intScript=script;
			//Here, I will use a regex method to handle the strings. I'm not sure if this uses more or less ops than the strFind() function.
			if(strCount(script, quote)>0) {
				string yesStrings[]=split(script, "(?="+quote+")(?!"+quote+")*(?="+quote+")");
				int len=length(yesStrings);
				for(int i=1;i<len;i+=2) {
					defineStr("$STR_"+i, yesStrings[i]);
					script=replaceAll(script, yesStrings[i]+quote, "$STR_"+i);
					//intScript=replaceAll(intScript, yesStrings[i]+quote, "");
				}
				strNames=replaceAll(strNames, quote, "");
			}
 
 
			defineBool("false", false);
			defineBool("true", true);
 
			script=parameterFind(script, true, 0);
 
			if(save) {
				string backupData="!~~backupS~~!"+orScript+"!~~backupE~~!"+"!~~paramS~~!"+varData+"!~~paramE~~!"+"!~~callsS~~!"+funcC+"!~~callsE~~!"+"!~~strNameS~~!"+strNames+"!~~strNameE~~!"+"!~~strVarE~~!";
				writeFile(dataFile, backupData);
			}
			strNames+="visitorIP/"+getVIP()+"//hostIP/"+getHIP()+"//";
			evalParams(false);
		}
		else theLog("Error: Missing a quotation mark somewhere?");
	}
	else {
		theLog("Error: Uneven number of brackets.");
	}
}
void evalParams(boolean m) {
 
	if(m) {
		string callSplit[]=split(callData, "/");
		int l1=length(callSplit);
		pCount=strCount(varData, "/")+l1;
		strCount=strCount(strNames, "//");
		for(int i=0;i<l1;i++) {
			//string toEval=processParam("$PARAM_"+callSplit[i]);
			voidEval(processParam("$PARAM_"+callSplit[i]));
		}
	}
	else {
		int l=length(funcCalls);
		//theLog("Starting evaluation at "+cStart);
		for(int i=cStart;i<l;i++) {
			//string toEval=processParam("$PARAM_"+funcCalls[i]);
			//theLog("Evaluating "+i+":"+toEval);
			voidEval(processParam("$PARAM_"+funcCalls[i]));
		}
		//theLog("Ended evaluation at "+l);
	}
}
 
void theLog(string l) {
	logMessage(l);
}
void defineStr(string name, string value) {
	if(value=="") theLog("Error: "+name+" has a null value?");
	else {
		int p=indexOf(strNames, "//"+name+"/", 0);
		if(p > -1) {
			string data=substr(strNames, p+strlen(name)+3, indexOf(strNames, "//", p+1));
			strNames=replaceAll(strNames, "//"+name+"/"+value+"//", "//"+name+"/"+replaceAll(value, "//", "!~~SLASH~~!")+"//");
		}
		else {
			strNames+=name+"/"+replaceAll(value, "//", "!~~SLASH~~!")+"//";
			//theLog("Added "+name+" value to array "+n);
		}
	}
}
void defineInt(string name, int value) {
	int p=indexOf(intNames, "//"+name+"/", 0);
	if(p > -1) {
		int arrayPos=parseInt(substr(intNames, p+strlen(name)+3, indexOf(intNames, "//", p+1)));
		intVars[arrayPos]=value;
	}
	else {
		int n=length(intVars);
		intVars[n]=value;
		intNames+=name+"/"+n+"//";
	}
}
void defineBool(string name, boolean value) {
	int p=indexOf(boolNames, "//"+name+"/", 0);
	if(p > -1) {
		int arrayPos=parseInt(substr(boolNames, p+strlen(name)+3, indexOf(boolNames, "//", p+1)));
		boolVars[arrayPos]=value;
	}
	else {
		int n=length(boolVars);
		boolVars[n]=value;
		boolNames+=name+"/"+n+"//";
	}
}
string processStr(string name, boolean eval) {
	string value="";
	/*if(indexOf(numbers, substr(name, 0, 1), 0)>-1) {//it's a number) 
		theLog("Error: Input "+name+" does not match expected type (str).");
	}
	else if(name=="false" || name=="true") {
		theLog("Error: Input "+name+" does not match expected type (str).");
	}
	else {*/
		//name=replaceAll(name, " ", "");
	if(substr(name, 0, 1)=="'") value=substr(name, 1, strlen(name)); //it's a symbol
	else {
		if(indexOf(name, "$PARAM_", 0)==0) {
			value=processParam(name);
			if(eval==true) value=strEval(value);
		}
		else {
			int l=indexOf(strNames, "//"+name+"/", 0);
			int l2=indexOf(strNames, "//", l+1);
			if(l>-1 && l2>-1) {
				//theLog("Pos? "+substr(strNames, l+strlen(name)+3, l2));
				value=replaceAll(substr(strNames, l+strlen(name)+3, l2), "!~~SLASH~~!", "//");
			}
			else {
				theLog("Warning: string "+name+" is null");
				//value="NULL";
			}
		}
	}
	//theLog(name+":"+value);
	return value;
}
int processInt(string name, boolean eval) {
	int value=0;
	string c1=substr(name, 0, 1);
	if(indexOf(numbers, c1, 0)>-1 || c1=="-") {//it's a number
		value=parseInt(name);
	}
	/*else if(name=="false" || name=="true") {
		theLog("Error: Input "+name+" does not match expected type (int).");
	}*/
	else {
		//name=replaceAll(name, " ", "");
		if(indexOf(name, "$PARAM_", 0)==0) {
			if(eval==true) value=intEval(processParam(name));
		}
		else {
			int l=indexOf(intNames, "//"+name+"/", 0);
			int l2=indexOf(intNames, "//", l+1);
			if(l>-1 && l2>-1) {
				//theLog("Pos? "+substr(strNames, l+strlen(name)+3, l2));
				int arrayPos=parseInt(substr(intNames, l+strlen(name)+3, l2));
				value=intVars[arrayPos];
			}
			else {
				theLog("Warning: int "+name+" is null");
				//value="NULL";
			}
		}
	}
	//theLog(name+":"+value);
	return value;
}
boolean processBool(string name, boolean eval) {
	boolean value=false;
	/*if(indexOf(numbers, substr(name, 0, 1), 0)>-1) {//it's a number) 
		theLog("Error: Input "+name+" does not match expected type (boolean).");
	}
	else if(name=="false" || name=="true") {
		value=name;
	}
	else {*/
	//name=replaceAll(name, " ", "");
	int l=indexOf(boolNames, "//"+name+"/", 0);
	int l2=indexOf(boolNames, "//", l+1);
	if(l>-1 && l2>-1) {
		int arrayPos=parseInt(substr(boolNames, l+2, l2+2));
		value=boolVars[arrayPos];
		if(indexOf(name, "$PARAM_", 0)==0 && eval==true) value=boolEval(value);
	}
	else {
		theLog("Warning: boolean "+name+" is null");
	}
	//}
	//theLog(name+":"+value);
	return value;
}
string processParam(string name) {
	string value="";
	name=replaceAll(name, " ", "");
	int l=indexOf(varData, "//"+name+"/", 0);
	int l2=indexOf(varData, "//", l+1);
	if(l>-1 && l2>-1) {
		value=replaceAll(substr(varData, l+strlen(name)+3, l2), "~~!SLASH!~~", "/");
	}
	return value;
}
string parameterFind(string text, boolean countIt, int type) {
	string checkingS=replaceAll(text, "()", "");
	string newText=text;
	if(indexOf(checkingS, "(", 0)>-1) {
	string openB="(";
	string closeB=")";
	if(type==1) { openB="{"; closeB="}"; }
	int Open=0; int Close=0; int x=indexOf(text, openB, 0);
int closeQ=0;
	int p1=x;
	int endParam=0;
	while(p1>-1) {
		int pos1=indexOf(text,openB,x);
		int pos2=indexOf(text, closeB, x);
		int q=indexOf(text, quote, x);
		int bx=x;
		boolean go=true;
		int pos2a=indexOf(text,closeB,p1);
		if(go) {
		if (pos1!=-1 && pos2!=-1 && pos1<pos2) {
			Open++;
			x=pos1+1;
		}
		if (pos1!=-1 && pos2!=-1 && pos1>pos2) {
			Close++;
			x=pos2+1;
		}
		if (pos1==-1 && pos2!=-1) {
			Close++;
			x=pos2+1;
		}
		//}
		if(q>-1) {
			int y=indexOf(text, quote, q+1);
			if(y>x && (y<pos1 || pos1==-1) && (y<pos2 || pos2==-1)) { x=y+1;}
		}
		if (pos2!=-1 && Open==Close) {
			endParam=pos2;
			string wholeStr="";
			if(p1+1<pos2) wholeStr=substr(text, p1, pos2+1);
			string theStr="";
			if(p1<pos2) theStr=substr(text, p1+1, pos2);
 
			if(indexOf(theStr, openB, 0)>-1) {
				theStr=parameterFind(theStr, false, type);
			}
			if(theStr=="") theStr="NULL";
			if(wholeStr!="") {
			varData+="$PARAM_"+pCount+"/"+theStr+"//";
			newText=replaceAll(newText, wholeStr, "$PARAM_"+pCount);
			if(countIt) { 
			funcCalls[calls]=pCount; funcC+=pCount+"/"; calls++; 
			}
			pCount++;
			}
			x=pos2+1;
			p1=indexOf(text, openB, x);
		}
	   }
	}
	}
	return newText;
}
string strFind(string text) {
	//if(!useGlobals) clearFile(dataFile);
	//if(useGlobals) setGlobal(dataGlobal,"");
	int sPos=indexOf(text, quote, 0);
	string theStr="";
	string wholeStr="";
	string currentSection="";
	while(sPos>-1) {
		int s2Pos=indexOf(text, quote, sPos+1);
		if(s2Pos>-1) {
			wholeStr=substr(text, sPos, s2Pos+1);
			theStr=substr(text, sPos+1, s2Pos);
			//if(theStr=="") theStr="NULL";
			//theStr=replaceAll(theStr,"//","??/??");
			defineStr("$STR_"+strCount, theStr);
 
			text=replaceAll(text, wholeStr, "$STR_"+strCount);
			strCount++;
			sPos=indexOf(text, quote, 0);
		}
		else {
			int lineNumber=strCount(substr(text,0,sPos),newLine)+1;
			theLog("Error: Quotation mark at line "+lineNumber+" has no corresponding closing quotation mark.");
			break;
		}
	}
	return text;
}
int strCount(string str, string s) {
	return strlen(str) - strlen(replaceAll(str, s, ""));
}
void echo(string key,string mess) {
	replaceContent(key, mess);
}
string getVIP() { 
return getVisitorIP();
}
string getHIP() {
return getHostIP();
}


HTTP Functions

void voidEval(string script) {
	string stuff[]=split(script, "[\s]+");
	int length=length(stuff);
	//File I/O
	if(stuff[0]=="clearFile") {
		clearFile(processStr(stuff[1], true));
	}
	else if(stuff[0]=="writeLine") {
		writeLine(processStr(stuff[1], true), processStr(stuff[2], true));
	}
	else if(stuff[0]=="writeFile") {
		writeFile(processStr(stuff[1], true), processStr(stuff[2], true));
	}
 
	if(stuff[0]=="eval") {
		//string bla=processStr(stuff[1], true);
		//theLog("Eval'ing "+bla);
		mainProcess(processStr(stuff[1], true), false);
	}
	//HTTP
	else if(stuff[0]=="replaceContent" || stuff[0]=="echo") {
		echo(processStr(stuff[1], true), processStr(stuff[2], true));
	}
	else if(stuff[0]=="message") {
		message(processStr(stuff[1], true), processStr(stuff[2], true));
	}
	else if(stuff[0]=="logMessage") theLog(processStr(stuff[1], true));
	//else if(stuff[0]=="popUp") theLog(processStr(stuff[1], true));
	else if(stuff[0]=="hideStore") hideStore();
	else if(stuff[0]=="while") {
		while(boolEval(processParam(stuff[1]))) {
			for(int z=2; z<length(stuff); z++) voidEval(processParam(stuff[z]));
		}
	}
	else if(stuff[0]=="cond") {
		for(int z=1;z<length;z+=2) {
			boolean outcome=false;
			if(stuff[z]=="else") outcome=true;
			if(stuff[z]!="else") outcome=boolEval(processParam(stuff[z]));
			if(outcome==true) { voidEval(processParam(stuff[z+1])); break; }
		}
	}
	else if(stuff[0]=="define") {
		if(indexOf(stuff[1], "$PARAM_", 0)==0) {//we are defining a function
			string def=processParam(stuff[1]);
			string params[]=split(def, " ");
			//params[0]=function name
			//after that, we have parameter names
			string fName=replaceAll(params[1], "/", "~~!SLASH!~~");
			string type=params[0];
			string parameters="";
			for(int z=2;z<length(params);z++) {
				parameters+=params[z]+" ";
			}
			parameters=replaceAll(substr(parameters, 0, strlen(parameters)-1), "/", "~~!SLASH!~~");
 
			string iii=processParam(stuff[2]);
			string inputTypes[]=split(iii, " ");
			string iTypes="";
			for(int z=0;z<length(inputTypes);z++) {
				iTypes+=inputTypes[z]+" ";
			}
			iTypes=replaceAll(substr(iTypes, 0, strlen(iTypes)-1), "/", "~~!SLASH!~~");
 
			string code="";
			for(int z=3; z<length(stuff); z++) {
				code+=stuff[z]+" ";
			}
			code=replaceAll(substr(code, 0, strlen(code)-1), "/", "~~!SLASH!~~");
 
			if(type=="void") voidFuncData+=fName+"/"+parameters+"//"+iTypes+"///"+code+"////";
			if(type=="int") intFuncData+=fName+"/"+parameters+"//"+iTypes+"///"+code+"////";
			if(type=="boolean") boolFuncData+=fName+"/"+parameters+"//"+iTypes+"///"+code+"////";
			if(type=="string") strFuncData+=fName+"/"+parameters+"//"+iTypes+"///"+code+"////";
			//theLog("Function data: "+fName+"/"+parameters+"//"+iTypes+"///"+code+"////");
		}
		else {//we are defining a variable
			if(stuff[1]=="string") defineStr(stuff[2], processStr(stuff[3], true));
			if(stuff[1]=="int") defineInt(stuff[2], processInt(stuff[3], true));
			if(stuff[1]=="boolean") defineBool(stuff[2], processBool(stuff[3], true));
		}
	}
	else if(indexOf(voidFuncData, "////"+stuff[0]+"/", 0)>-1) { //it's a custom function!
		int f=indexOf(voidFuncData, "////"+stuff[0]+"/", 0);
		int f2=indexOf(voidFuncData, "//", f+5);
		int f3=indexOf(voidFuncData, "///", f2+1);
		int f4=indexOf(voidFuncData, "////", f3+1);
 
		string parameters=substr(voidFuncData, f+strlen(stuff[0])+5, f2);
 		string inputTypes=substr(voidFuncData, f2+2, f3);
 		string code=substr(voidFuncData, f3+3, f4);
 
		string params[]=split(parameters, " ");
		string types[]=split(inputTypes, " ");
		for(int z=0;z<length(params); z++) {
			if(types[z]=="string") defineStr(params[z], processStr(stuff[z+1], true));
			if(types[z]=="int") defineInt(params[z], processInt(stuff[z+1], true));
			if(types[z]=="boolean") defineBool(params[z], processBool(stuff[z+1], true));
		}
		//theLog("Evaluating "+stuff[0]);
		voidEval(processParam(code));
	}
	else theLog("Error: voidEval of "+script+" failed to execute.");
}
string strEval(string script) {
	//if(log) theLog("evaluating: "+script);
	string stuff[]=split(script, "[\s]+");
	int length=length(stuff);
	string value="";
	//General Functions
	if(stuff[0]=="append-string") {
		for(int z=1; z<length; z++) value+=processStr(stuff[z], true);
	}
	else if(stuff[0]=="getTime") value=getTime();
	else if(stuff[0]=="getDate") value=getDate();
	else if(stuff[0]=="toUpper") value=toUpper(processStr(stuff[1], true));
	else if(stuff[0]=="toLower") value=toLower(processStr(stuff[1], true));
	else if(stuff[0]=="substr") {
		value=substr(processStr(stuff[1], true), processInt(stuff[2], true), processInt(stuff[3], true));
	}
	else if(stuff[0]=="char") {
		value=char(processInt(stuff[1], true));
	}
	else if(stuff[0]=="replaceAll") value=replaceAll(processStr(stuff[1], true), processStr(stuff[2], true), processStr(stuff[3], true));
	else if(stuff[0]=="intToStr") value=processInt(stuff[1], true)+"";
 
	//File I/O
	else if(stuff[0]=="readFile") value=readFile(processStr(stuff[1], true));
	else if(stuff[0]=="readLine") value=readLine(processStr(stuff[1], true), processInt(stuff[2], true));
 
	//HTTP
	else if(stuff[0]=="fetchGetVariable" || stuff[0]=="get") {
		value=fetchGetVariable(processStr(stuff[1], true));
	}
	//else if(stuff[0]=="getParameter" || stuff[0]=="get") value=getParameter(processStr(stuff[1], true));
 
	//Other
	else if(stuff[0]=="cond") {
		for(int z=1;z<length;z+=2) {
			boolean outcome=false;
			if(stuff[z]=="else") outcome=true;
			if(stuff[z]!="else") outcome=boolEval(processParam(stuff[z]));
			if(outcome==true) { value=strEval(processParam(stuff[z+1])); break; }
		}
	}
	else if(indexOf(strFuncData, "////"+stuff[0]+"/", 0)>-1) { //it's a custom function!
		int f=indexOf(strFuncData, "////"+stuff[0]+"/", 0);
		int f2=indexOf(strFuncData, "//", f+5);
		int f3=indexOf(strFuncData, "///", f2+1);
		int f4=indexOf(strFuncData, "////", f3+1);
 
		string parameters=substr(strFuncData, f+strlen(stuff[0])+5, f2);
		string inputTypes=substr(strFuncData, f2+2, f3);
		string code=substr(strFuncData, f3+3, f4);
 
		string params[]=split(parameters, " ");
		string types[]=split(inputTypes, " ");
		for(int z=0;z<length(params); z++) {
			if(types[z]=="string") defineStr(params[z], processStr(stuff[z+1], true));
			if(types[z]=="int") defineInt(params[z], processInt(stuff[z+1], true));
			if(types[z]=="boolean") defineBool(params[z], processBool(stuff[z+1], true));
		}
		//theLog("Evaluating "+stuff[0]);
		value=strEval(processParam(code));
	}
	else theLog("Error: strEval of "+script+" failed to execute.");
	return value;
}
int intEval(string script) {
	string stuff[]=split(script, "[\s]+");
	int length=length(stuff);
	int value="";
	//Math
	if(stuff[0]=="+") value=processInt(stuff[1], true)+processInt(stuff[2], true);
	else if(stuff[0]=="-") value=processInt(stuff[1], true)-processInt(stuff[2], true);
	else if(stuff[0]=="/") value=processInt(stuff[1], true)/processInt(stuff[2], true);
	else if(stuff[0]=="*") value=processInt(stuff[1], true)*processInt(stuff[2], true);
	//else if(stuff[0]=="sqrt") value=sqrt(processInt(stuff[1], true));
	//General
	else if(stuff[0]=="indexOf") value=indexOf(processStr(stuff[1], true), processStr(stuff[2], true), processInt(stuff[3], true));
	else if(stuff[0]=="strlen") {
		value=strlen(processStr(stuff[1], true));
	}
	else if(stuff[0]=="strToInt") {
		value=parseInt(processStr(stuff[1], true));
	}
 
	//File I/O
	else if(stuff[0]=="countLines") {
		value=countLines(processStr(stuff[1], true));
	}
	else if(stuff[0]=="cond") {
		for(int z=1;z<length;z+=2) {
			boolean outcome=false;
			if(stuff[z]=="else") outcome=true;
			if(stuff[z]!="else") outcome=boolEval(processParam(stuff[z]));
			if(outcome==true) { value=intEval(processParam(stuff[z+1])); break; }
		}
	}
	else if(indexOf(intFuncData, "////"+stuff[0]+"/", 0)>-1) { //it's a custom function!
		int f=indexOf(intFuncData, "////"+stuff[0]+"/", 0);
		int f2=indexOf(intFuncData, "//", f+5);
		int f3=indexOf(intFuncData, "///", f2+1);
		int f4=indexOf(intFuncData, "////", f3+1);
 
		string parameters=substr(intFuncData, f+strlen(stuff[0])+5, f2);
		string inputTypes=substr(intFuncData, f2+2, f3);
		string code=substr(intFuncData, f3+3, f4);
		string params[]=split(parameters, " ");
		string types[]=split(inputTypes, " ");
		for(int z=0;z<length(params); z++) {
			if(types[z]=="string") defineStr(params[z], processStr(stuff[z+1], true));
			if(types[z]=="int") defineInt(params[z], processInt(stuff[z+1], true));
			if(types[z]=="boolean") defineBool(params[z], processBool(stuff[z+1], true));
		}
		//theLog("Evaluating "+stuff[0]);
		value=intEval(processParam(code));
	}
	else theLog("Error: intEval of "+script+" failed to execute.");
	return value;
}
boolean boolEval(string script) {
	//if(log) theLog("evaluating: "+script);
	string stuff[]=split(script, "[\s]+");
	int length=length(stuff);
	boolean value=false;
	if(stuff[0]=="<") {
		if(processInt(stuff[1], true) < processInt(stuff[2], true)) value=true;
	}
	else if(stuff[0]=="=") {
		if(processInt(stuff[1], true) == processInt(stuff[2], true)) value=true;
	}
	else if(stuff[0]==">") {
		if(processInt(stuff[1], true) > processInt(stuff[2], true)) value=true;
	}
	else if(stuff[0]=="<=") {
		if(processInt(stuff[1], true) <= processInt(stuff[2], true)) value=true;
	}
	else if(stuff[0]==">=") {
		if(processInt(stuff[1], true) >= processInt(stuff[2], true)) value=true;
	}
	else if(stuff[0]=="not") {
		if(processBool(stuff[1], true)==false) value=true;
	}
	else if(stuff[0]=="and") {
		if(processBool(stuff[1], true)==true && processBool(stuff[2], true)==true) value=true;
	}
	else if(stuff[0]=="or") {
		if(processBool(stuff[1], true)==true || processBool(stuff[2], true)==true) value=true;
	}
	else if(stuff[0]=="string=?") {
		if(processStr(stuff[1], true)==processStr(stuff[2], true)) value=true;
	}
	else if(stuff[0]=="fileExists") {
		value=fileExists(processStr(stuff[1], true));
	}
	else if(stuff[0]=="cond") {
		for(int z=1;z<length;z+=2) {
			boolean outcome=false;
			if(stuff[z]=="else") outcome=true;
			if(stuff[z]!="else") outcome=boolEval(processParam(stuff[z]));
			if(outcome==true) { value=boolEval(processParam(stuff[z+1])); break; }
		}
	}
	else if(indexOf(boolFuncData, "////"+stuff[0]+"/", 0)>-1) { //it's a custom function!
		int f=indexOf(boolFuncData, "////"+stuff[0]+"/", 0);
		int f2=indexOf(boolFuncData, "//", f+5);
		int f3=indexOf(boolFuncData, "///", f2+1);
		int f4=indexOf(boolFuncData, "////", f3+1);
 
		string parameters=substr(boolFuncData, f+strlen(stuff[0])+5, f2);
		string inputTypes=substr(boolFuncData, f2+2, f3);
		string code=substr(boolFuncData, f3+3, f4);
		string params[]=split(parameters, " ");
		string types[]=split(inputTypes, " ");
		for(int z=0;z<length(params); z++) {
			if(types[z]=="string") defineStr(params[z], processStr(stuff[z+1], true));
			if(types[z]=="int") defineInt(params[z], processInt(stuff[z+1], true));
			if(types[z]=="boolean") defineBool(params[z], processBool(stuff[z+1], true));
		}
		//theLog("Evaluating "+stuff[0]);
		value=boolEval(processParam(code));
	}
	else theLog("Error: boolEval of "+script+" failed to execute.");
	return value;
}


HTTP Website Code Example

(define string welcome (append-string "Welcome, " visitorIP "!<br>"))
(echo "welcome" welcome)
(echo "links" "<a href='blubification.hw'>Main Page</a>&nbsp;|&nbsp;<a href='blubification.hw?page=source'>Source Code</a>&nbsp;|&nbsp;<a href='blubification.hw?page=functions'>Functions</a>&nbsp;|&nbsp;<a href='blubification.hw?page=test'>Test!</a>")
(cond
	(string=? (get "page") "")
	(echo "main" "I am currently testing and finishing up a brand new script interpreter.<br>
		I believe all there's left to do is add functions.
		<br>You may look at the current running code on the Source Code page.")
	(string=? (get "page") "source")
	(echo "main" (append-string "<textarea rows=30 cols=110 nowrap>" (replaceAll (readFile "script.txt") "</" "<?") "</textarea>"))
	(string=? (get "page") "functions")
	(echo "main" "<b>Functions</b><br><br>
	All HTTP functions<br>
	All File I/O Functions<br>
	append-string<br>
	voidCond/intCond/strCond/boolCond : Depends on the type of output.<br>
	define type value : value can be a function definition. See source code for example.<br>
	intToStr<br>
	strToInt<br>
	eval<br>"
		)
	(string=? (get "page") "test")
	(echo "main" "<form method='POST' Name='bla!'><textarea name='text' rows=10 cols=60></textarea><br><input type='submit' name='test' value='submit'></form>")
	else
	(echo "main" "This page does not exist!")
	)
Personal tools