eigenfunction <
emeka...@yahoo.com> wrote:
> how do I use shen.js in a webpage?
> Let's say i want (+ 1 1) to be shown as alert(2) in a webpage, how do I go
> about that?
> thx.
First you need to load `shen.js` but before define a global variable
`shenjs_external_repl` and set it to `true` to prevent shenjs to try to
start a repl.
Then you need a function to evaluate Shen code from a string. The
simpliest way is to define such a function in Shen:
(define shenjs-evalstr-aux
X -> (eval (head (compile (function shen-<st_input>)
(map string->n (explode X))))))
Then translate it to javascript:
(pr (js-from-kl
(js-kl-from-shen
[define shenjs-evalstr-aux
X -> [eval [head [compile [function shen-<st_input>]
[map string->n [explode X]]]]]])))
You will get something like:
shenjs_evalstr_aux = [shen_type_func,
function shen_user_lambda709(Arg708) {
if (Arg708.length < 1) return [shen_type_func, shen_user_lambda709, 1, Arg708];
var Arg708_0 = Arg708[0];
return (function() {
return shenjs_call_tail(shen_eval, [shenjs_call(shen_head, [shenjs_call(shen_compile, [[shen_type_symbol, "shen-<st_input>"], shenjs_call(shen_map, [[shen_type_symbol, "string->n"], shenjs_call(shen_explode, [Arg708_0])]), []])])]);})},
1,
[],
"shenjs-evalstr-aux"];
shenjs_functions["shen_shenjs-evalstr-aux"] = shenjs_evalstr_aux;
And finally add some routine for convenience:
function shenjs_evalstr(str) {
var x = shenjs_call(shenjs_evalstr_aux, [str]);
return shenjs_call(shen_intmake_string, [\"~A\", [shen_tuple, x, []]]);
}
Which then can be used like this:
alert(shenjs_evalstr("(+ 1 1)")
Hope this helps.