git » jacl.git » commit 0817703

added LispSymbol.findSymbol()

author Alan Dipert
2019-09-06 06:52:20 UTC
committer Alan Dipert
2019-09-06 06:52:20 UTC
parent a13e850147a761d84a27a7afb1b5b113a6211735

added LispSymbol.findSymbol()

jacl.js +31 -1

diff --git a/jacl.js b/jacl.js
index 12bc52d..aeecad8 100644
--- a/jacl.js
+++ b/jacl.js
@@ -100,6 +100,10 @@ class LispSymbol {
 
     throw new Error(`Unknown symbol syntax: '${token}'`);
   }
+  static kw(kwName, upcase = true) {
+    kwName = upcase ? kwName.toUpperCase() : kwName;
+    return LispSymbol.intern('KEYWORD', kwName);
+  }
   // intern: whether or not the symbol should be interned
   static createFromString(str, intern) {
     if (typeof str !== 'string') throw new Error(`Can only intern strings`);
@@ -149,7 +153,7 @@ class Package {
     this.symbols = new Map();
     // Set of names (keys of this.symbols) that are exported
     this.exports = new Set();
-    // List of packages, the symbols of which to also search
+    // The 'use list'; list of packages, the symbols of which to also search
     this.use = [];
   }
   intern(name) {
@@ -170,6 +174,32 @@ class Package {
   isExported(name) {
     return this.exports.has(name);
   }
+  findSymbol(name) {
+    if (this.exports.has(name)) {
+      return new Values(
+        this.symbols.get(name),
+        LispSymbol.kw('external')
+      );
+    }
+
+    if (this.symbols.has(name)) {
+      return new Values(
+        this.symbols.get(name),
+        LispSymbol.kw('internal')
+      );
+    }
+
+    for (const usedPkg in this.use) {
+      if (usedPkg.isExported(name)) {
+        return new Values(
+          usedPkg.symbols.get(name),
+          LispSymbol.kw('inherited')
+        );
+      }
+    }
+
+    return null;
+  }
   static intern(packageName, name) {
     const pkg = Package.get(packageName, true);
     return pkg.intern(name);