git » jacl.git » commit 2a7fcb4

&rest support works!?

author Alan Dipert
2019-11-03 22:08:37 UTC
committer Alan Dipert
2019-11-03 22:08:37 UTC
parent 7ab8866d2f0b90b2b3affd55e225ca985025d1e5

&rest support works!?

jacl.js +16 -1

diff --git a/jacl.js b/jacl.js
index dc45d26..fe9f85d 100644
--- a/jacl.js
+++ b/jacl.js
@@ -11,11 +11,14 @@ class Cons {
     if (!(cons instanceof Cons)) return false;
     return Cons.isProperList(cons.cdr);
   }
-  static listOf(...xs) {
+  static fromArray(xs) {
     let list = null;
     for(let i = xs.length-1; i >= 0; i--) list = new Cons(xs[i], list);
     return list;
   }
+  static listOf(...xs) {
+    return Cons.fromArray(xs);
+  }
   static length(cons) {
     return cons.cdr === null ? 1 : 1 + Cons.length(cons.cdr);
   }
@@ -1492,6 +1495,18 @@ const emitNode = (print, node) => {
         print('break;\n');
         print('}\n');
       }
+      // &rest
+      if (node.lambdaList.rest) {
+        print(mungeSym(node.lambdaList.rest, 'local'));
+        print('=');
+        const restStart = node.lambdaList.required.length
+          + node.lambdaList.optional.length;
+        // TODO Here we cons up a list from an array. It would be better if we
+        // could avoid copying and maybe had a 'cdr-coded' type, or array-backed
+        // pseudo-cons type thing. Then we could creat one of those in constant
+        // time.
+        print(`Cons.fromArray(Array.prototype.slice.call(arguments, ${restStart}));\n`);
+      }
       emitBlock(print, node.statements, node.ret);
       print('})');
       break;