To learn and test the ListScript above, I wrote a command environment
<script>
NIL = [];
= function()
{
if( <= 0) return "NIL";
var str = "";
for (var i = 0; i < ; i++)
{
if(this instanceof Array)
str += "," + ();
else str += "," + this;
}
return "[" + (1) + "]";
};
(function(){
LispScript = {
Run : run
};
function run(code)
{
if(code instanceof Array)
{
var elements = new Array();
for (var i = 0; i < ; i++)
{
code = run(code); //Recursively read downward
if(code instanceof Function) //Parse the expression
{
if( <= 0)//The function without parameters can be omitted[] It is called directly with the function name
{
code = (null);
}
else if(i == 0) //Calling the function with parameters [funcall,args...]
{
return (null, (1));
}
}
}
return code;
}
return Element(code);
};
})();
function Assert(msg, cond)
{
if(cond)
return true;
else
{
alert(msg);
throw new Error(msg);
}
};
function Element(arg)
{
if(arg == null)
return [];
else if(arg instanceof Function && <= 0)
return (null);
else
return arg;
};
__funList = new Array();
/////////////////
quote = _ = function(args)
{
if( < 1)
return [];
else if( >= 1)
{
return arguments[0];
}
};
//[atom,x] returns atom true. If the value of x is an atom or an empty table, otherwise it returns []. In Lisp, we use atom true to represent true according to convention, and use empty table to represent false.
atom = function(arg)
{
var tmp = (arg); //First evaluate the parameters
if(!(tmp instanceof Array) || <= 0)
return true;
else
return [];
};
//[eq,x,y] returns t If the values of x and y are the same atom or are both empty tables, otherwise return [].
equal = eq = function(arg1, arg2)
{
var tmp1 = (arg1);
var tmp2 = (arg2); //First evaluate the parameters
if(!(tmp1 instanceof Array) && !(tmp2 instanceof Array) &&
() == () ||
(tmp1 instanceof Function) && (tmp2 instanceof Function) && () == () ||
(tmp1 instanceof Array) && (tmp2 instanceof Array) && ( == 0) && ( == 0))
return true;
else
return [];
};
//[car,x] expects the value of x to be a table and returns the first element of x.
car = function(arg)
{
var tmp = (arg); //First evaluate the parameters
if(tmp instanceof Array && > 0)
return tmp[0];
else
return [];
};
//[cdr,x] expects the value of x to be a table and returns all elements after the first element of x.
cdr = function(arg)
{
var tmp = (arg); //First evaluate the parameters
if(tmp instanceof Array && > 0)
return (1);
else
return [];
};
//[cons,x,y] expects the value of y to be a table and returns a new table. Its first element is the value of x, followed by each element of the value of y.
cons = function(arg1, arg2)
{
var tmp1 = (arg1);
var tmp2 = (arg2); //First evaluate the parameters
if(tmp2 instanceof Array)
{
var list = new Array();
(tmp1);
return (tmp2);
}
else
return [];
};
/*
[cond [...] ...[...]] The evaluation rules are as follows. The p expression is evaluated in sequence until there is a return t. If such a p expression can be found, the value of the corresponding e expression is used as the return value of the entire cond expression.
*/
cond = function(args)
{
for (var i = 0; i < ; i++)
{
if(arguments instanceof Array)
{
var cond = (arguments[0]); //Evaluate the parameters first
//alert(cond);
if(cond == true && arguments[1] != null)
return (arguments[1]);
}
}
return [];
};
//This is called a function call. Its value is calculated as follows. Each expression is evaluated first, and then e is evaluated. During the evaluation process of e, each value that appears in e is the corresponding value in the most recent function call.
lambda = function(args, code)
{
if(code instanceof Array)
{
var fun = new Function(args,
"for(var i = 0; i < ; i++) arguments = (arguments);return ("+()+");");
var globalFuncName = __funList.pop();
fun._funName = globalFuncName;
if(globalFuncName != null)
self[globalFuncName] = fun;
return fun;
}
return [];
};
</script>
<script>
var prompt = '>';
function initPrompt()
{
if( == '')
= prompt;
}
function runLastCmd()
{
if( == 13)
{
= false;
var idx = ('\n') + 1;
idx += ;
var strCmd = (idx);
var output = '';
try
{
var lisp = eval(strCmd);
if(lisp instanceof Array)
{
output = (lisp);
}
else
{
output = 'Unknown Command';
}
}
catch(e)
{
output = 'Syntax error';
}
if(output instanceof Array)
{
output = '[' + output + ']';
}
+= '\n' + output + '\n' + prompt;
}
}
</script>
<textarea id=cmd style="width:600px;height:600px" onkeydown="runLastCmd()" onclick="initPrompt()">
</textarea>