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
4 changed files with 92 additions and 0 deletions

View File

@@ -24,6 +24,56 @@ function Library (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
this.pos = (x, y, t = 'pos') => {
@@ -105,7 +155,12 @@ function Library (ronin) {
this.mul = function (a, b) {
return a * b
}
this.div = function (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))
}
},
if: function (input, context) {
if (interpret(input[1], context)) {
return interpret(input[2], context)
}
return interpret(input[3], context)
}
}