From 55d6a409a3f3628d2e63513807b316e405c7e0e8 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 13 Jul 2019 14:49:53 +0200 Subject: [PATCH 1/7] add run command --- desktop/sources/scripts/lisp.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index c54763e..6ce5316 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) @@ -47,6 +57,7 @@ function Lisp (input, lib) { } const interpret = function (input, context) { + console.log(input, context) if (context === undefined) { return interpret(input, new Context(lib)) } else if (input instanceof Array) { From 9bf643e1a12a830052e481ae5e50d5085512ca6a Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 13 Jul 2019 14:51:09 +0200 Subject: [PATCH 2/7] remove console.log --- desktop/sources/scripts/lisp.js | 1 - 1 file changed, 1 deletion(-) diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index 6ce5316..f30fde6 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -57,7 +57,6 @@ function Lisp (input, lib) { } const interpret = function (input, context) { - console.log(input, context) if (context === undefined) { return interpret(input, new Context(lib)) } else if (input instanceof Array) { From 805e9ccb28c744574752ed4adbaf422affb0cfd7 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 13 Jul 2019 17:58:04 +0200 Subject: [PATCH 3/7] adds print and map --- desktop/sources/scripts/library.js | 8 ++++++++ examples/arrays.lisp | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 examples/arrays.lisp diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 647d6a6..083282e 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -24,6 +24,14 @@ function Library (ronin) { // TODO: Closes Ronin } + this.print = (...args) => { + console.log(...args) + } + + this.map = (fn, arr) => { + return arr.map(fn) + } + // Rects this.pos = (x, y, t = 'pos') => { diff --git a/examples/arrays.lisp b/examples/arrays.lisp new file mode 100644 index 0000000..97fad47 --- /dev/null +++ b/examples/arrays.lisp @@ -0,0 +1,8 @@ +(print (map (lambda (a) (add a 1)) (1 2 3))) + +( + (clear) + (map (lambda (a) + (stroke (rect (mul a 30) 20 50 50) 1 "red")) + (5 10 15 20)) +) \ No newline at end of file From 5de7d7fdc45f59d95bca6eba62174ea67863bbe6 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 13 Jul 2019 18:18:35 +0200 Subject: [PATCH 4/7] adds first and rest --- desktop/sources/scripts/library.js | 9 +++++++++ examples/arrays.lisp | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 083282e..f054041 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -28,10 +28,19 @@ function Library (ronin) { console.log(...args) } + // Arrays this.map = (fn, arr) => { return arr.map(fn) } + this.first = (arr) => { + return arr[0] + } + + this.rest = ([_, ...arr]) => { + return arr + } + // Rects this.pos = (x, y, t = 'pos') => { diff --git a/examples/arrays.lisp b/examples/arrays.lisp index 97fad47..76306e6 100644 --- a/examples/arrays.lisp +++ b/examples/arrays.lisp @@ -1,5 +1,10 @@ (print (map (lambda (a) (add a 1)) (1 2 3))) +( + (print (first (1 2 3))) + (print (rest (1 2 3))) +) + ( (clear) (map (lambda (a) From d5225a3d68eac93caee1885fd0f4c553da778196 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 13 Jul 2019 20:57:12 +0200 Subject: [PATCH 5/7] Add logic functions --- desktop/sources/scripts/library.js | 33 ++++++++++++++++++++++++++++++ examples/logic.lisp | 9 ++++++++ 2 files changed, 42 insertions(+) create mode 100644 examples/logic.lisp diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index f054041..a685542 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -28,7 +28,40 @@ function Library (ronin) { console.log(...args) } + // 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) } diff --git a/examples/logic.lisp b/examples/logic.lisp new file mode 100644 index 0000000..f45f176 --- /dev/null +++ b/examples/logic.lisp @@ -0,0 +1,9 @@ +( + (print (lt 3 4)) + + (print (and 1 2 true 4)) + + (print (and 1 false 2)) + + (print (or false false 2 false)) +) \ No newline at end of file From 56402870fc622069d01310962b1ddbd488a1bce3 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 13 Jul 2019 21:15:03 +0200 Subject: [PATCH 6/7] adds if and filter --- desktop/sources/scripts/library.js | 9 +++++++++ desktop/sources/scripts/lisp.js | 7 +++++++ examples/arrays.lisp | 6 ++++++ examples/logic.lisp | 2 ++ 4 files changed, 24 insertions(+) diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index a685542..532dc89 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -66,6 +66,10 @@ function Library (ronin) { return arr.map(fn) } + this.filter = (fn, arr) => { + return arr.filter(fn) + } + this.first = (arr) => { return arr[0] } @@ -155,7 +159,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 + } } diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js index c54763e..31ea659 100644 --- a/desktop/sources/scripts/lisp.js +++ b/desktop/sources/scripts/lisp.js @@ -35,6 +35,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 index 76306e6..3389fa1 100644 --- a/examples/arrays.lisp +++ b/examples/arrays.lisp @@ -5,6 +5,12 @@ (print (rest (1 2 3))) ) +(print + (filter + (lambda (a) (eq 0 (mod a 2))) + (1 2 3 4 5)) +) + ( (clear) (map (lambda (a) diff --git a/examples/logic.lisp b/examples/logic.lisp index f45f176..2c612d9 100644 --- a/examples/logic.lisp +++ b/examples/logic.lisp @@ -6,4 +6,6 @@ (print (and 1 false 2)) (print (or false false 2 false)) + + (if (gt 1 2) (print "ok") (print "not ok")) ) \ No newline at end of file From 578efdd523e5e4b482f40fae5a10386fdc1943d3 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 13 Jul 2019 21:17:34 +0200 Subject: [PATCH 7/7] remove duplicated command print --- desktop/sources/scripts/library.js | 4 ---- examples/arrays.lisp | 8 ++++---- examples/logic.lisp | 10 +++++----- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/desktop/sources/scripts/library.js b/desktop/sources/scripts/library.js index 532dc89..43649e4 100644 --- a/desktop/sources/scripts/library.js +++ b/desktop/sources/scripts/library.js @@ -24,10 +24,6 @@ function Library (ronin) { // TODO: Closes Ronin } - this.print = (...args) => { - console.log(...args) - } - // Logic this.gt = (a, b) => { diff --git a/examples/arrays.lisp b/examples/arrays.lisp index 3389fa1..fa87526 100644 --- a/examples/arrays.lisp +++ b/examples/arrays.lisp @@ -1,11 +1,11 @@ -(print (map (lambda (a) (add a 1)) (1 2 3))) +(echo (map (lambda (a) (add a 1)) (1 2 3))) ( - (print (first (1 2 3))) - (print (rest (1 2 3))) + (echo (first (1 2 3))) + (echo (rest (1 2 3))) ) -(print +(echo (filter (lambda (a) (eq 0 (mod a 2))) (1 2 3 4 5)) diff --git a/examples/logic.lisp b/examples/logic.lisp index 2c612d9..dc22f79 100644 --- a/examples/logic.lisp +++ b/examples/logic.lisp @@ -1,11 +1,11 @@ ( - (print (lt 3 4)) + (echo (lt 3 4)) - (print (and 1 2 true 4)) + (echo (and 1 2 true 4)) - (print (and 1 false 2)) + (echo (and 1 false 2)) - (print (or false false 2 false)) + (echo (or false false 2 false)) - (if (gt 1 2) (print "ok") (print "not ok")) + (if (gt 1 2) (echo "ok") (echo "not ok")) ) \ No newline at end of file