kei-lisp 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,380 @@
1
+ import { Interface } from 'node:readline';
2
+
3
+ /**
4
+ * @class
5
+ * @classdesc Iterator class for Cons.
6
+ * @author Keisuke Ikeda
7
+ * @this {Loop}
8
+ */
9
+ declare class Loop {
10
+ aCons: Cons;
11
+ length: number;
12
+ index: number;
13
+ /**
14
+ * Constructor.
15
+ * @param aCons the Cons to iterate over
16
+ */
17
+ constructor(aCons: Cons);
18
+ /**
19
+ * Returns this instance.
20
+ */
21
+ iterator(): this;
22
+ /**
23
+ * Returns whether a next element exists.
24
+ */
25
+ hasNext(): boolean;
26
+ /**
27
+ * Returns the next element.
28
+ */
29
+ next(): LispValue;
30
+ /**
31
+ * Implementation of the iterable protocol.
32
+ * Enables iteration with for...of and similar constructs.
33
+ */
34
+ [Symbol.iterator](): Iterator<LispValue>;
35
+ /**
36
+ * Implementation of the async iterable protocol.
37
+ * Enables iteration with for await...of and similar constructs.
38
+ */
39
+ [Symbol.asyncIterator](): AsyncIterator<LispValue>;
40
+ /**
41
+ * Advances to the next element.
42
+ */
43
+ remove(): null;
44
+ }
45
+
46
+ /**
47
+ * @class
48
+ * @classdesc Class that mimics a Cons cell.
49
+ * @author Keisuke Ikeda
50
+ * @this {Cons}
51
+ */
52
+ declare class Cons {
53
+ static readonly nil: Cons;
54
+ car: LispValue;
55
+ cdr: LispValue;
56
+ /**
57
+ * Constructor.
58
+ * @constructor
59
+ * @param car the car; defaults to nil when no argument is given.
60
+ * @param cdr the cdr; defaults to nil when no argument is given.
61
+ */
62
+ constructor(car?: LispValue, cdr?: LispValue);
63
+ /**
64
+ * Appends the given element to the end of this Cons.
65
+ * @param anObject the object to append
66
+ * @return the Cons with the element appended
67
+ */
68
+ add(anObject: LispValue): this;
69
+ /**
70
+ * Clones this Cons and returns the clone.
71
+ * @return the cloned Cons
72
+ */
73
+ clone(): Cons;
74
+ /**
75
+ * Clones the given value (a Cons element) and returns the clone.
76
+ * @param value a Cons element
77
+ * @return the cloned element
78
+ */
79
+ static cloneValue(value: LispValue): LispValue;
80
+ /**
81
+ * Returns whether this Cons equals the given object.
82
+ * @param anObject the object to compare against
83
+ * @return a boolean
84
+ */
85
+ equals(anObject: LispValue): boolean;
86
+ /**
87
+ * Returns whether both arguments are Cons cells and are equal.
88
+ * @param left the object to compare
89
+ * @param right the object to compare
90
+ * @return a boolean
91
+ */
92
+ equalsAUX(left: LispValue, right: LispValue): boolean;
93
+ /**
94
+ * Returns whether the given argument is an Atom.
95
+ */
96
+ static isAtom(anObject: LispValue): boolean;
97
+ /**
98
+ * Returns whether the given argument is a Cons.
99
+ */
100
+ static isCons(anObject: LispValue): anObject is Cons;
101
+ /**
102
+ * Returns whether the given argument is a List.
103
+ */
104
+ static isList(anObject: LispValue): boolean;
105
+ /**
106
+ * Returns whether the given argument is Nil.
107
+ */
108
+ static isNil(anObject: LispValue): boolean;
109
+ /**
110
+ * Returns whether the given argument is not a Cons.
111
+ */
112
+ static isNotCons(anObject: LispValue): boolean;
113
+ /**
114
+ * Returns whether the given argument is not a List.
115
+ */
116
+ static isNotList(anObject: LispValue): boolean;
117
+ /**
118
+ * Returns whether the given argument is not Nil.
119
+ */
120
+ static isNotNil(anObject: LispValue): boolean;
121
+ /**
122
+ * Returns whether the given argument is not an interpreted symbol.
123
+ */
124
+ static isNotSymbol(anObject: LispValue): boolean;
125
+ /**
126
+ * Returns whether the given argument is a number.
127
+ */
128
+ static isNumber(anObject: LispValue): anObject is number;
129
+ /**
130
+ * Returns whether the given argument is a string.
131
+ */
132
+ static isString(anObject: LispValue): anObject is string;
133
+ /**
134
+ * Returns whether the given argument is an interpreted symbol.
135
+ */
136
+ static isSymbol(anObject: LispValue): anObject is InterpretedSymbol;
137
+ /**
138
+ * Returns whether the given argument is an environment.
139
+ */
140
+ static isTable(anObject: LispValue): anObject is Table;
141
+ /**
142
+ * Returns the last cell of this Cons.
143
+ * @return this Cons's last cell
144
+ */
145
+ last(): Cons;
146
+ /**
147
+ * Returns an iterator over this Cons.
148
+ * @return an iterator over this Cons
149
+ */
150
+ loop(): Loop;
151
+ /**
152
+ * Returns the length (depth) of this Cons.
153
+ * @return the length (depth) of this Cons
154
+ */
155
+ length(): number;
156
+ /**
157
+ * Concatenates the given Cons and returns this Cons.
158
+ * @param aCons the Cons to concatenate
159
+ * @return this Cons
160
+ */
161
+ nconc(aCons: Cons): this;
162
+ /**
163
+ * Returns the nth element of this Cons.
164
+ * @param aNumber the index to retrieve
165
+ * @return the element at the given index
166
+ */
167
+ nth(aNumber: number): LispValue;
168
+ /**
169
+ * Lexes the given string into a Cons and returns it.
170
+ * @param aString the string to lex
171
+ */
172
+ static parse(aString: string): LispValue;
173
+ /**
174
+ * Sets the car.
175
+ */
176
+ setCar(anObject: LispValue): null;
177
+ /**
178
+ * Sets the cdr.
179
+ */
180
+ setCdr(anObject: LispValue): null;
181
+ /**
182
+ * Sets both the car and the cdr.
183
+ */
184
+ setCons(car: LispValue, cdr: LispValue): this;
185
+ /**
186
+ * Returns a formatted string representation of this Cons.
187
+ */
188
+ toString(): string;
189
+ /**
190
+ * Returns a formatted string representation of the given object.
191
+ * @param anObject the object to format
192
+ */
193
+ static toString(anObject: LispValue): string;
194
+ }
195
+
196
+ /**
197
+ * Union of every value the interpreter can store or evaluate.
198
+ *
199
+ * - `Cons` — pairs and lists (including `Cons.nil`)
200
+ * - `InterpretedSymbol` — interned Lisp symbols
201
+ * - `Table` — environment frame (internal use)
202
+ * - `number` / `string` — primitive atoms
203
+ * - `null` — internal sentinel, distinct from Lisp `nil` (which is `Cons.nil`)
204
+ */
205
+ type LispValue = Cons | InterpretedSymbol | Table | number | string | null;
206
+
207
+ /**
208
+ * @class
209
+ * @classdesc Class that manages bindings of interpreted symbols.
210
+ * @author Keisuke Ikeda
211
+ * @this {Table}
212
+ */
213
+ declare class Table extends Map<unknown, LispValue> {
214
+ source: Table | null;
215
+ root: boolean;
216
+ /**
217
+ * Constructor.
218
+ * @param aTable the environment in which this environment was created
219
+ */
220
+ constructor(aTable?: Table | null);
221
+ /**
222
+ * Clones this Table and returns the clone.
223
+ */
224
+ clone(): Table;
225
+ /**
226
+ * Returns whether anything is bound to the given property (key).
227
+ */
228
+ has(aSymbol: unknown): boolean;
229
+ /**
230
+ * Returns whether this instance equals the given object.
231
+ */
232
+ equals(anObject: unknown): boolean;
233
+ /**
234
+ * Returns the value bound to the given interpreted symbol.
235
+ */
236
+ get(aSymbol: unknown): LispValue;
237
+ /**
238
+ * Returns whether this instance is the root of the environment chain.
239
+ */
240
+ isRoot(): boolean;
241
+ /**
242
+ * Reassigns the symbol bound in the innermost scope (equivalent to Common Lisp's setq).
243
+ * If a binding exists in the current scope, update it and return; otherwise recurse into the parent scope.
244
+ */
245
+ setIfExist(aSymbol: unknown, anObject: LispValue): LispValue;
246
+ /**
247
+ * Sets whether this instance is the root of its environment chain.
248
+ */
249
+ setRoot(aBoolean: boolean): null;
250
+ /**
251
+ * Sets the parent environment.
252
+ */
253
+ setSource(aTable: Table | null): null;
254
+ /**
255
+ * Returns a formatted string representation of this instance.
256
+ */
257
+ toString(): string;
258
+ }
259
+
260
+ /**
261
+ * @class
262
+ * @classdesc Interpreted symbol with uniqueness, where each printed name maps to a single canonical instance (identity equals equality). A class that mimics canonical strings, distinct from JS's standard Symbol.
263
+ * @author Keisuke Ikeda
264
+ * @this {InterpretedSymbol}
265
+ */
266
+ declare class InterpretedSymbol {
267
+ #private;
268
+ static get table(): Table;
269
+ name: string;
270
+ /**
271
+ * Constructor.
272
+ * @param name printed name
273
+ */
274
+ constructor(name?: string);
275
+ /**
276
+ * Compares this interpreted symbol with the given one by printed name.
277
+ * @param aSymbol the symbol to compare against
278
+ * @return the difference in string length
279
+ */
280
+ compareTo(aSymbol: InterpretedSymbol): number;
281
+ /**
282
+ * Returns whether this symbol equals the given object.
283
+ */
284
+ equals(anObject: unknown): boolean;
285
+ /**
286
+ * Returns the same interpreted symbol for a given printed name.
287
+ * @param aString printed name
288
+ */
289
+ static of(aString: string): InterpretedSymbol;
290
+ /**
291
+ * Returns the string representation of this symbol.
292
+ */
293
+ toString(): string;
294
+ }
295
+
296
+ type Stream = NodeJS.WritableStream | null;
297
+ /**
298
+ * @class
299
+ * @classdesc
300
+ * @author Keisuke Ikeda
301
+ * @this {StreamManager}
302
+ */
303
+ declare class StreamManager {
304
+ isTrace: boolean;
305
+ streamTable: Map<string, Stream>;
306
+ spyTable: Map<InterpretedSymbol, string>;
307
+ traceStream: Stream;
308
+ constructor();
309
+ getStream(): Stream;
310
+ /**
311
+ * Initializes the instance variables.
312
+ */
313
+ initialize(): null;
314
+ isSpy(aSymbol: InterpretedSymbol | null): boolean;
315
+ noSpy(aSymbol: InterpretedSymbol): null;
316
+ noTrace(): null;
317
+ setIsTrace(aBoolean: boolean): null;
318
+ setTraceStream(aStream: Stream): null;
319
+ spy(aSymbol: InterpretedSymbol, aString: string): null;
320
+ spyStream(aSymbol: InterpretedSymbol | null): Stream | string;
321
+ spyTable_(): Map<InterpretedSymbol, string>;
322
+ trace(): null;
323
+ }
324
+
325
+ /**
326
+ * @class
327
+ * @classdesc Class for the interpreter.
328
+ * @author Keisuke Ikeda
329
+ * @this {LispInterpreter}
330
+ */
331
+ declare class LispInterpreter {
332
+ root: Table;
333
+ streamManager: StreamManager;
334
+ rl: Interface;
335
+ constructor();
336
+ /**
337
+ * Starts the interpreter.
338
+ */
339
+ run(): null;
340
+ /**
341
+ * Evaluates the given list and returns the evaluation result.
342
+ */
343
+ eval(aCons: LispValue): LispValue;
344
+ /**
345
+ * Parses the source string, evaluates every expression it contains, and returns the results as an array.
346
+ */
347
+ evalAll(source: string): LispValue[];
348
+ /**
349
+ * Parses and evaluates the source string and returns the value of the last expression.
350
+ */
351
+ evalString(source: string): LispValue;
352
+ /**
353
+ * Parses the given string into a list and returns it.
354
+ */
355
+ parse(aString: string): LispValue;
356
+ /**
357
+ * Sets the given environment as the root of the environment chain.
358
+ */
359
+ setRoot(environment: Table): null;
360
+ /**
361
+ * Initializes the environment table.
362
+ */
363
+ initializeTable(): Table;
364
+ }
365
+
366
+ /**
367
+ * @class
368
+ * @classdesc Error class representing a graceful exit triggered by a Lisp (exit) call. Catch it at the REPL or library boundary to run cleanup before terminating.
369
+ * @author Keisuke Ikeda
370
+ * @this {ExitError}
371
+ */
372
+ declare class ExitError extends Error {
373
+ /**
374
+ * Constructor.
375
+ * @constructor
376
+ */
377
+ constructor();
378
+ }
379
+
380
+ export { Cons, ExitError, InterpretedSymbol, LispInterpreter, type LispValue };