diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index c962ad5..fb505f9 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -265,6 +265,17 @@ function Library (ronin) { this.TWO_PI = Math.PI * 2 + this.random = (...args) => { + if (args.length >= 2) { + // (random start end) + return args[0] + Math.random() * (args[1] - args[0]) + } else if (args.length === 1) { + // (random max) + return Math.random() * args[0] + } + return Math.random() + } + // Generics this.echo = (...args) => { @@ -299,4 +310,8 @@ function Library (ronin) { // Livecoding this.time = Date.now + + // javascript interop + this.js = window + } diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index e308c52..a7c35af 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -38,6 +38,17 @@ function Lisp (input, lib) { context.scope[input[1].value] = interpret(input[2], context) return input[2] }, + defn: function(input, context) { + context.scope[input[1].value] = function() { + const lambdaArguments = arguments + const lambdaScope = input[2].reduce(function (acc, x, i) { + acc[x.value] = lambdaArguments[i] + return acc + }, {}) + + return interpret(input[3], new Context(lambdaScope, context)) + } + }, lambda: function (input, context) { return function () { const lambdaArguments = arguments diff --git a/examples/benchmark.lisp b/examples/benchmark.lisp index a1be15d..ba1265c 100644 --- a/examples/benchmark.lisp +++ b/examples/benchmark.lisp @@ -46,8 +46,13 @@ (def addOne (lambda (a) (add a 1))) (test "def - value" aaa 123) (test "def - func" (addOne 4) 5) + (defn addTwo (a) (add 2 a)) + (test "defn" (addTwo 4) 6) ; Generics (test "str" (str 1 4 "-" (add 3 4) ".jpg") "14-7.jpg") + +; Interop + (test "interop" ((of (of js "Math") "max") 2 4) 4) ) \ No newline at end of file