add interop, defn, random
This commit is contained in:
parent
ce3cf9985e
commit
f353b674b3
@ -265,6 +265,17 @@ function Library (ronin) {
|
|||||||
|
|
||||||
this.TWO_PI = Math.PI * 2
|
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
|
// Generics
|
||||||
|
|
||||||
this.echo = (...args) => {
|
this.echo = (...args) => {
|
||||||
@ -299,4 +310,8 @@ function Library (ronin) {
|
|||||||
// Livecoding
|
// Livecoding
|
||||||
|
|
||||||
this.time = Date.now
|
this.time = Date.now
|
||||||
|
|
||||||
|
// javascript interop
|
||||||
|
this.js = window
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,17 @@ function Lisp (input, lib) {
|
|||||||
context.scope[input[1].value] = interpret(input[2], context)
|
context.scope[input[1].value] = interpret(input[2], context)
|
||||||
return input[2]
|
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) {
|
lambda: function (input, context) {
|
||||||
return function () {
|
return function () {
|
||||||
const lambdaArguments = arguments
|
const lambdaArguments = arguments
|
||||||
|
@ -46,8 +46,13 @@
|
|||||||
(def addOne (lambda (a) (add a 1)))
|
(def addOne (lambda (a) (add a 1)))
|
||||||
(test "def - value" aaa 123)
|
(test "def - value" aaa 123)
|
||||||
(test "def - func" (addOne 4) 5)
|
(test "def - func" (addOne 4) 5)
|
||||||
|
(defn addTwo (a) (add 2 a))
|
||||||
|
(test "defn" (addTwo 4) 6)
|
||||||
|
|
||||||
; Generics
|
; Generics
|
||||||
|
|
||||||
(test "str" (str 1 4 "-" (add 3 4) ".jpg") "14-7.jpg")
|
(test "str" (str 1 4 "-" (add 3 4) ".jpg") "14-7.jpg")
|
||||||
|
|
||||||
|
; Interop
|
||||||
|
(test "interop" ((of (of js "Math") "max") 2 4) 4)
|
||||||
)
|
)
|
Loading…
x
Reference in New Issue
Block a user