+
1
|
list
|
skin
|
login
|
editor
α-wwwiki
::
dipert
user:none
(3129 bytes)
_h2 15 lines (see also [[evaluator]]) _p Following [[psil2]], this is a very very small lisp (s-expressionss) interpreter written in 15 lines. The code is adapted from [[Alan Dipert|http://alan.dipert.org/post/7193534410/discover-lisp-in-your-web-browser-with-javascript]] who gives a very clean and clear explanation about the lisp fundamental principle. In this tiny code, there are no defines, no lambdas, nothing but the 4 basic operators "+,-,*,/", the Javascript Math functions ... and any user function predefined in Javascript. From this point of view, it's endless, powerful and evil (because eval). You may : _ul copy a line from the examples, _ul paste it into the field below, _ul and click on the button "evaluate". {input {@ id="input" type="texte" value="['Math.sqrt', ['+', ['*',3,3], ['*',4,4]]]" style="width:100%; font:normal 1em courier; color:red;" }} {input {@ type="submit" value="evaluate" onclick="°° // LISP CODE START : var operators = {'+':0, '-':0, '*':0, '/':0}; var evaluate = function(expression) { if (!Array.isArray(expression)) return expression.toString(); var func_name = expression.shift(); var func_args = expression; var compiled_args = []; for (var i=0; i< func_args.length; i++) compiled_args.push( eval( evaluate( func_args[i] ) ) ); if (operators.hasOwnProperty(func_name)) return compiled_args.join(func_name); else // it's a JS math function return func_name + '(' + compiled_args.join(',') + ')'; } function do_evaluate(input) { return eval(evaluate(eval(input))); } // LISP CODE END // user JS functions can be called function fac (n) { return (n< 2)? 1 : n*fac(n-1); } function hypo (a,b) { return Math.sqrt( a*a + b*b ); } // ... getId('output').innerHTML = do_evaluate(getId('input').value); °°"}} {pre {@ id="output"}} _h3 some examples to copy/paste {pre ['+',1,2,3,4,5,6] ['*',1,2,3,4,5,6] ['+', ['*',3,3], ['*',4,4]] -> 25 ['*', ['+',3,3], ['+',4,4]] -> 48 ['Math.sqrt', 2] ['Math.pow',2,8] ['Math.sqrt', ['+', ['*',3,3], ['*',4,4]]] ['hypo',3,4] ['fac',6] ... and so on. } _h3 lisp code _p Here are the 15 lines of Javascript : {pre °° var operators = {'+':0, '-':0, '*':0, '/':0}; var evaluate = function(expression) { if (!Array.isArray(expression)) return expression.toString(); var func_name = expression.shift(); var func_args = expression; var compiled_args = []; for (var i=0; i< func_args.length; i++) compiled_args.push( eval( evaluate( func_args[i] ) ) ); if (operators.hasOwnProperty(func_name)) return compiled_args.join(func_name); else return func_name + '(' + compiled_args.join(',') + ')'; } function do_evaluate(input) { return eval(evaluate(eval(input))); } // user JS functions can be called function fac (n) { return (n< 2)? 1 : n*fac(n-1); } function hypo (a,b) { return Math.sqrt( a*a + b*b ); } // ... °°} _h3 next step _p lambdas, defines, ... _h3 see also _p A console outside the wiki infrastructure with a basic lambda functionality : [[dipert|data/dipert_lisp]].