git » jacl.git » commit a96e486

StringStream

author Alan Dipert
2020-01-15 05:31:07 UTC
committer Alan Dipert
2020-01-15 05:31:07 UTC
parent cd8deaec26f8cd812e4773496c3649db8111beb9

StringStream

jacl.js +36 -13

diff --git a/jacl.js b/jacl.js
index 141ab1d..1008225 100644
--- a/jacl.js
+++ b/jacl.js
@@ -381,8 +381,29 @@ PACKAGE.value = Package.get('JACL');
 
 JACLPKG.usePackage(CLPKG);
 
+class StringStream {
+  constructor(str) {
+    this.ptr = 0;
+    this.str = str;
+  }
+  unread(ch) {
+    this.ptr--;
+  }
+  read() {
+    if (this.ptr < this.str.length) {
+      return this.str[this.ptr++];
+    } else {
+      return EOF;
+    }
+  }
+  *[Symbol.iterator]() {
+    while (true) yield this.read();
+  }
+}
+
 class BufferedStream {
   constructor() {
+    console.log("omg");
     this.buf = [];
     this.resolveQueue = [];
   }
@@ -396,11 +417,6 @@ class BufferedStream {
   writeEach(xs) {
     for (const x of xs) this.write(x);
   }
-  unreadEach(xs) {
-    for (let i = xs.length; i--; i >= 0) {
-      this.unread(xs[i]);
-    }
-  }
   unread(obj) {
     if (this.resolveQueue.length) {
       this.resolveQueue.shift()(obj)
@@ -1944,16 +1960,22 @@ const startRepl = async () => {
   }
 };
 
-const loadLispScripts = async () => {
+const loadLispScripts = async (profile = false) => {
   for (let i = 0; i < document.head.childNodes.length; i++) {
     const child = document.head.childNodes[i];
     if (child.nodeName === 'SCRIPT' && child.src.endsWith('.lisp')) {
-      const start = new Date();
-      const bs = new BufferedStream();
-      const rdr = new Reader(bs);
       const code = await (await fetch(child.src)).text();
-      bs.writeEach(code);
-      bs.write(EOF);
+      const start = new Date();
+      if (profile) console.profile(child.src);
+
+      const ss = new StringStream(code);
+      const rdr = new Reader(ss);
+
+      //const ss = new BufferedStream();
+      //const rdr = new Reader(ss);
+      //ss.writeEach(code);
+      //ss.write(EOF);
+
       for await(const obj of rdr) {
         if (obj === EOF) break;
         const node = analyze(emptyEnv, null, obj);
@@ -1962,13 +1984,14 @@ const loadLispScripts = async () => {
         eval(sb.toString());
       }
       console.log(`;Loaded ${child.src} in ${(new Date())-start} ms`);
+      if (profile) console.profileEnd(child.src);
     }
   }
 };
 
 window.addEventListener('load', async () => {
-  await loadLispScripts();
-  QUnit.start();
+  await loadLispScripts(false);
+  //QUnit.start();
   startRepl();
 });