+
1
|
list
|
skin
|
login
|
editor
α-wwwiki
::
evaluator
user:none
(3955 bytes)
_h3 8-lines evaluator (& [[evaluator2]]) _p After [[dipert]], a still smaller s-expression evaluator written in 8 lines, working with a freely extendable dictionary of user javascript functions. How to use it : _ul copy a line from the examples, _ul paste it into the field below, _ul click on the button "evaluate". {input {@ id="input" type="texte" value="['sqrt', ['+', ['*',3,3], ['*',4,4]]]" style="width:99%; font:normal 1em courier; color:red;background:#ffc;" }} {input {@ type="submit" value="evaluate" onclick="°° // 1) evaluator var evaluate = function(str) { var expression = eval( str ), rest = []; if (!Array.isArray(expression)) return expression.toString(); var first = expression.shift(); if (!dico.hasOwnProperty(first)) return 'Sorry, [' + first + '] is not in dico !'; expression.map(function(a){rest.push(evaluate(a))}) return dico[first](rest); }; // 2) functions dictionary var dico = { '+': function(a) { return a.reduce(function(x,y){return Number(x)+Number(y)}) }, '*': function(a) { return a.reduce(function(x,y){return x*y}) }, '-': function(a) { return (a.length == 1)? -a[0] : a.reduce(function(x,y){return x-y}) }, '/': function(a) { return (a.length == 1)? 1/a[0] : a.reduce(function(x,y){return x/y}) }, 'sqrt' : function(a) { return Math.sqrt( a ) }, 'pow' : function(a) { return Math.pow( a[0], a[1] ) }, 'square': function(a) { return a[0]*a[0] }, 'hypo' : function(a) { return Math.sqrt( a[0]*a[0] + a[1]*a[1] ) }, 'fac' : function(a) { return (a< 2)? 1 : a*arguments.callee(a-1); } // and so on. }; // 3) HTML interface getId('output').innerHTML = evaluate( getId('input').value ); °°"}} {pre {@ id="output"}} _h3 examples {pre ['+',1,2,3,4,5,6] ['*',1,2,3,4,5,6] ['+', ['*',3,3], ['*',4,4]] ['*', ['+',3,3], ['+',4,4]] ['sqrt', 2] ['pow',2,8] ['sqrt', ['+', ['*',3,3], ['*',4,4]]] ['hypo',3,4] ['fac',6] And try new ones ... } _h3 lisp code {pre °° // 1) evaluator 1: var evaluate = function(str) { 2: var expression = eval( str ), rest = []; 3: if (!Array.isArray(expression)) return expression.toString(); 4: var first = expression.shift(); 5: if (!dico.hasOwnProperty(first)) return 'Sorry, [' + first + '] is not in dico !'; 6: expression.map(function(a){rest.push(evaluate(a))}) 7: return dico[first](rest); 8: }; // 2) functions dictionary var dico = { '+': function(a) { return a.reduce(function(x,y){return Number(x)+Number(y)}) }, '*': function(a) { return a.reduce(function(x,y){return x*y}) }, '-': function(a) { return (a.length == 1)? -a[0] : a.reduce(function(x,y){return x-y}) }, '/': function(a) { return (a.length == 1)? 1/a[0] : a.reduce(function(x,y){return x/y}) }, 'sqrt' : function(a) { return Math.sqrt( a ) }, 'pow' : function(a) { return Math.pow( a[0], a[1] ) }, 'square': function(a) { return a[0]*a[0] }, 'hypo' : function(a) { return Math.sqrt( a[0]*a[0] + a[1]*a[1] ) }, 'fac' : function(a) { return (a< 2)? 1 : a*arguments.callee(a-1); } // and so on. }; // 3) HTML interface getId('output').innerHTML = evaluate( getId('input').value ); °°} _h3 next step _ul quote, lambda and define, in short : inline extending the dico _ul replace eval() by a specific lexer (see [[psil2]]) _ul a multiline editor (see [[psil2]]) _ul ... and finally, rediscover [[amlisp]]. An endless wheel ! _h3 if you want to play with this code _ul click on the top-left '+' button, then open the editor, _ul copy the page's code (all the code) in the clipboard, _ul go the page [[sandbox]], _ul click on the top-left '+' button, then open the editor, _ul select all the code and paste the clipboard content, _ul play with it ... _ul don't forget to write your opinion (and ideas for improvment) in page [[forum]] ! _h3 see also _p A console outside the wiki infrastructure with a basic lambda functionality : [[dipert|data/dipert_lisp]].