Merge pull request #31 from lctrt/feat/lisp-utils

Feat/lisp utils
This commit is contained in:
Лu Лinveгa 2019-07-14 09:11:28 +12:00 committed by GitHub
commit 32898e4930
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 0 deletions

View File

@ -24,6 +24,56 @@ function Library (ronin) {
// TODO: Closes Ronin // TODO: Closes Ronin
} }
// Logic
this.gt = (a, b) => {
return a > b
}
this.lt = (a, b) => {
return a < b
}
this.eq = (a, b) => {
return a === b
}
this.and = (...args) => {
for (let i = 0; i < args.length; i++) {
if (!args[i]) {
return args[i]
}
}
return args[args.length - 1]
}
this.or = (...args) => {
for (let i = 0; i < args.length; i++) {
if (args[i]) {
return args[i]
}
}
return args[args.length - 1]
}
// Arrays
this.map = (fn, arr) => {
return arr.map(fn)
}
this.filter = (fn, arr) => {
return arr.filter(fn)
}
this.first = (arr) => {
return arr[0]
}
this.rest = ([_, ...arr]) => {
return arr
}
// Rects // Rects
this.pos = (x, y, t = 'pos') => { this.pos = (x, y, t = 'pos') => {
@ -105,7 +155,12 @@ function Library (ronin) {
this.mul = function (a, b) { this.mul = function (a, b) {
return a * b return a * b
} }
this.div = function (a, b) { this.div = function (a, b) {
return a / b return a / b
} }
this.mod = function (a, b) {
return a % b
}
} }

View File

@ -45,6 +45,13 @@ function Lisp (input, lib) {
return interpret(input[2], new Context(lambdaScope, context)) return interpret(input[2], new Context(lambdaScope, context))
} }
},
if: function (input, context) {
if (interpret(input[1], context)) {
return interpret(input[2], context)
}
return interpret(input[3], context)
} }
} }

19
examples/arrays.lisp Normal file
View File

@ -0,0 +1,19 @@
(echo (map (lambda (a) (add a 1)) (1 2 3)))
(
(echo (first (1 2 3)))
(echo (rest (1 2 3)))
)
(echo
(filter
(lambda (a) (eq 0 (mod a 2)))
(1 2 3 4 5))
)
(
(clear)
(map (lambda (a)
(stroke (rect (mul a 30) 20 50 50) 1 "red"))
(5 10 15 20))
)

11
examples/logic.lisp Normal file
View File

@ -0,0 +1,11 @@
(
(echo (lt 3 4))
(echo (and 1 2 true 4))
(echo (and 1 false 2))
(echo (or false false 2 false))
(if (gt 1 2) (echo "ok") (echo "not ok"))
)