Merge branch 'master' into feat/math-extension
This commit is contained in:
commit
bf69849d78
@ -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 (...args) {
|
||||
return args.reduce((sum, val) => sum * val)
|
||||
}
|
||||
|
||||
this.div = function (...args) {
|
||||
return args.reduce((sum, val) => sum / val)
|
||||
}
|
||||
|
||||
this.mod = function (a, b) {
|
||||
return a % b
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
function Lisp (input, lib) {
|
||||
const path = require('path')
|
||||
const fs = require('fs')
|
||||
|
||||
const TYPES = { identifier: 0, number: 1, string: 2, bool: 3 }
|
||||
const Context = function (scope, parent) {
|
||||
this.scope = scope
|
||||
@ -16,6 +19,13 @@ function Lisp (input, lib) {
|
||||
}
|
||||
|
||||
const special = {
|
||||
run: (input, context) => {
|
||||
const file = fs.readFileSync(
|
||||
path.resolve(input[1].value),
|
||||
{encoding: "utf-8"})
|
||||
|
||||
return interpret(this.parse(file), context)
|
||||
},
|
||||
let: function (input, context) {
|
||||
const letContext = input[1].reduce(function (acc, x) {
|
||||
acc.scope[x[0].value] = interpret(x[1], context)
|
||||
@ -35,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)
|
||||
}
|
||||
}
|
||||
|
||||
|
19
examples/arrays.lisp
Normal file
19
examples/arrays.lisp
Normal 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
11
examples/logic.lisp
Normal 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"))
|
||||
)
|
Loading…
x
Reference in New Issue
Block a user