diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 3b4debca..b4fe272 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -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 + } } diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index c54763e..0cb4b73 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -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) } } diff --git a/examples/arrays.lisp b/examples/arrays.lisp new file mode 100644 index 0000000..fa87526 --- /dev/null +++ b/examples/arrays.lisp @@ -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)) +) \ No newline at end of file diff --git a/examples/logic.lisp b/examples/logic.lisp new file mode 100644 index 0000000..dc22f79 --- /dev/null +++ b/examples/logic.lisp @@ -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")) +) \ No newline at end of file