add interop, defn, random

This commit is contained in:
Quentin Leonetti 2019-07-15 21:30:15 +02:00
parent ce3cf9985e
commit f353b674b3
3 changed files with 31 additions and 0 deletions

View File

@ -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
}

View File

@ -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

View File

@ -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)
)