author | Alan Dipert
<alan@dipert.org> 2019-08-14 20:57:39 UTC |
committer | Alan Dipert
<alan@dipert.org> 2019-08-14 20:57:39 UTC |
parent | e4b264c4ad79e0fa1d984743700f25f63a74f975 |
jacl.js | +15 | -58 |
diff --git a/jacl.js b/jacl.js index 00a7378..120f238 100644 --- a/jacl.js +++ b/jacl.js @@ -43,16 +43,6 @@ class LispSymbol { this.value = UNDEFINED; this.fvalue = UNDEFINED; this.stack = []; - this.printPiped = true; - this.packageSingle = true; - } - setPrintPiped(x) { - this.printPiped = x; - return this; - } - setPackageSingle(x) { - this.packageSingle = x; - return this; } val() { if (this.value === UNDEFINED) @@ -78,9 +68,9 @@ class LispSymbol { throw new Error(`Symbol name must not be number: '${name}'`); return Package.get(packageName, true).intern(name); } - static fromString(token, printPiped = true) { + static fromString(token) { if (/^:[^:]+$/.test(token)) { - return LispSymbol.intern('KEYWORD', token.substring(1)).setPrintPiped(printPiped); + return LispSymbol.intern('KEYWORD', token.substring(1)); } const singleMarkerRe = /^([^:]+):([^:]+)$/; @@ -88,36 +78,20 @@ class LispSymbol { let match = token.match(singleMarkerRe); if (match) { - return LispSymbol - .intern(...match.slice(1)) - .setPrintPiped(printPiped); + return LispSymbol.intern(...match.slice(1)) } match = token.match(doubleMarkerRe); if (match) { - return LispSymbol.intern(...match.slice(1)) - .setPrintPiped(printPiped) - .setPackageSingle(false); + return LispSymbol.intern(...match.slice(1)); } if (token.indexOf(':') < 0) { - return PACKAGE.val().intern(token).setPrintPiped(printPiped); + return PACKAGE.val().intern(token); } throw new Error(`Unknown symbol syntax: '${token}'`); } - toString() { - let str = '' - if (this.printPiped) str += '|'; - if (this.package && this.package != PACKAGE.value.name) { - str += this.package; - if (this.package && this.packageSingle) str += ':'; - if (this.package && !this.packageSingle) str += '::'; - } - str += this.name; - if (this.printPiped) str += '|'; - return str; - } } class LispString extends Array { @@ -234,13 +208,9 @@ class ReadTable { const READTABLE = Package.get('CL').intern('*READTABLE*'); class Token extends String { - constructor(printPiped, str) { - super(str); - this.printPiped = printPiped; - } // TODO figure out implications of jacl:undefined/jacl:true/jacl:false etc here interpret() { - return readInteger(this) || LispSymbol.fromString(this, this.printPiped) + return readInteger(this) || LispSymbol.fromString(this); } static is(x, y) { return (x instanceof Token) && x.valueOf() === y; @@ -308,31 +278,19 @@ READTABLE.value = new ReadTable() const isWhitespace = ch => ' \t\n\r\b'.indexOf(ch) > -1; -const consumeWhitespace = async stream => { - for await(const ch of stream) { - if (isWhitespace(ch)) { - continue; - } else { - stream.unread(ch); - break; - } - } -} - const isConstituent = ch => { return /[A-Za-z]/.test(ch) || '!$%&*+-./:<=>?@[]^_{}~'.indexOf(ch) > -1 || /[0-9]/.test(ch); } -const readMultiEscaped = async function(stream, token, printPiped) { +const readMultiEscaped = async function(stream, token) { for await(const y of stream) { if (isConstituent(y) || READTABLE.val().isTerminating(y) || isWhitespace(y)) { token += y; continue; } else if (y === '\\') { token += await stream.read(); - printPiped = true; continue; } else if (y === '|') { return readSingleEscaped(stream, token); @@ -348,20 +306,19 @@ const readInteger = token => { } }; -const readSingleEscaped = async function(stream, token, printPiped) { +const readSingleEscaped = async function(stream, token) { for await(const y of stream) { if (isConstituent(y)) { token += y.toUpperCase(); continue; } else if (y === '\\') { token += await stream.read(); - printPiped = true; continue; } else if (y === '|') { return readMultiEscaped(stream, token); } else if (READTABLE.val().isTerminating(y) || isWhitespace(y)) { stream.unread(y); - return new Token(printPiped, token); + return new Token(token); } else { throw new Error(`Illegal character: '${y}'`); } @@ -393,11 +350,11 @@ class Tokenizer { } } else if (x === '\\') { let y = await this.stream.read(); - return readSingleEscaped(this.stream, y, true); + return readSingleEscaped(this.stream, y); } else if (x === '|') { - return readMultiEscaped(this.stream, '', true); + return readMultiEscaped(this.stream, ''); } else if (isConstituent(x)) { - return readSingleEscaped(this.stream, x.toUpperCase(), false); + return readSingleEscaped(this.stream, x.toUpperCase()); } else { throw new Error(`Illegal character: '${x}'`); } @@ -438,9 +395,9 @@ class Reader { } } -var buf = new BufferedStream(); -var tok = new Tokenizer(buf); -var rdr = new Reader(tok); +var buf = new BufferedStream(), + tok = new Tokenizer(buf), + rdr = new Reader(tok); (async function() { for await(const obj of rdr) {