From b87e2cf965b63cadf5fb0ba899f1406b0e00db02 Mon Sep 17 00:00:00 2001
From: Quentin Leonetti <q.leonetti@gmail.com>
Date: Sat, 20 Jul 2019 16:52:50 +0200
Subject: [PATCH] add '() lambda shorthand

---
 desktop/sources/scripts/lisp.js | 23 ++++++++++++++++++++++-
 examples/lambda.lisp            |  6 ++++++
 2 files changed, 28 insertions(+), 1 deletion(-)
 create mode 100644 examples/lambda.lisp

diff --git a/desktop/sources/scripts/lisp.js b/desktop/sources/scripts/lisp.js
index 7459f97..1f9b40f 100644
--- a/desktop/sources/scripts/lisp.js
+++ b/desktop/sources/scripts/lisp.js
@@ -60,6 +60,16 @@ function Lisp (input, lib) {
         return interpret(input[2], new Context(lambdaScope, context))
       }
     },
+    fn: function (input, context) {
+      return async function () {
+        const lambdaArguments = arguments
+        const lambdaScope = [].reduce(function (acc, x, i) {
+          acc[x.value] = lambdaArguments[i]
+          return acc
+        }, {})
+        return interpret(input.slice(1), new Context(lambdaScope, context))
+      }
+    },
     if: async function (input, context) {
       if (await interpret(input[1], context)) {
         return interpret(input[2], context)
@@ -110,6 +120,10 @@ function Lisp (input, lib) {
     const token = input.shift()
     if (token === undefined) {
       return list.pop()
+    } else if (token === '\'(') {
+      input.unshift('fn')
+      list.push(parenthesize(input, []))
+      return parenthesize(input, list)
     } else if (token === '(') {
       list.push(parenthesize(input, []))
       return parenthesize(input, list)
@@ -121,7 +135,14 @@ function Lisp (input, lib) {
   }
 
   const tokenize = function (input) {
-    return input.replace(/^\;.*\n?/gm, '').split('"').map(function (x, i) { return i % 2 === 0 ? x.replace(/\(/g, ' ( ').replace(/\)/g, ' ) ') : x.replace(/ /g, '!whitespace!') }).join('"').trim().split(/\s+/).map(function (x) { return x.replace(/!whitespace!/g, ' ') })
+    const i = input.replace(/^\;.*\n?/gm, '').split('"')
+    return i.map(function (x, i) { 
+      return i % 2 === 0 ? 
+        x.replace(/\(/g, ' ( ').replace(/\)/g, ' ) ').replace(/' \( /g, ' \'( ')
+        : x.replace(/ /g, '!whitespace!') 
+    })
+    .join('"').trim().split(/\s+/)
+    .map(function (x) { return x.replace(/!whitespace!/g, ' ') })
   }
 
   this.parse = function (input) {
diff --git a/examples/lambda.lisp b/examples/lambda.lisp
new file mode 100644
index 0000000..2c1be4c
--- /dev/null
+++ b/examples/lambda.lisp
@@ -0,0 +1,6 @@
+(
+
+(echo (map '(echo 1 2 3) (4 5 6))
+
+)
+