From 805e9ccb28c744574752ed4adbaf422affb0cfd7 Mon Sep 17 00:00:00 2001 From: Quentin Leonetti Date: Sat, 13 Jul 2019 17:58:04 +0200 Subject: [PATCH 1/5] 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 2/5] 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 3/5] 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 4/5] 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 5/5] 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