ic10c-node 2.6.6

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,518 @@
1
+ // Copyright (c) 2026. All rights reserved.
2
+ // This source code is licensed under the CC BY-NC-SA
3
+ // (Creative Commons Attribution-NonCommercial-NoDerivatives) License, By Xiao Songtao.
4
+ // This software is protected by copyright law. Reproduction, distribution, or use for commercial
5
+ // purposes is prohibited without the author's permission. If you have any questions or require
6
+ // permission, please contact the author: edocsitahw@qq.com
7
+
8
+ /**
9
+ * @file unary.d.ts
10
+ * @author edocsitahw
11
+ * @version 1.1
12
+ * @date 2026/07/22 15:41
13
+ * @desc
14
+ * @copyright CC BY-NC-SA 2026. All rights reserved.
15
+ * */
16
+ import {
17
+ ErrorNode,
18
+ OperandNode,
19
+ RegisterOrIdentifierNode,
20
+ DeviceReferenceNode,
21
+ InstructionNode,
22
+ OperandType
23
+ } from "../ast";
24
+
25
+
26
+ // 一元指令(1 个操作数)
27
+
28
+ export interface UnaryInstruction extends InstructionNode {
29
+ type1: OperandType;
30
+ }
31
+
32
+ /**
33
+ * @summary 一元指令概述
34
+ *
35
+ * @desc 一元指令是包含单个操作数的指令。
36
+ * 根据操作数类型可分为以下几类:
37
+ * - **UnaryInstruction_RI**:寄存器或标识符作为操作数
38
+ * - **UnaryInstruction_DR**:设备引用作为操作数
39
+ * - **UnaryInstruction_O**:通用操作数
40
+ *
41
+ * @elseif en
42
+ * @summary Unary Instructions Overview
43
+ *
44
+ * @desc Unary instructions contain a single operand.
45
+ * They are categorized by operand type:
46
+ * - **UnaryInstruction_RI**: Register or identifier as operand
47
+ * - **UnaryInstruction_DR**: Device reference as operand
48
+ * - **UnaryInstruction_O**: General operand
49
+ */
50
+
51
+ /**
52
+ * @summary 含单个 RegisterOrIdentifier 操作数的一元指令基类
53
+ *
54
+ * @desc 操作数为寄存器或标识符节点。
55
+ * @elseif en
56
+ * @summary Base interface for unary instructions with single RegisterOrIdentifier operand
57
+ *
58
+ * @desc Operand is a register or identifier node.
59
+ *
60
+ * @public
61
+ */
62
+ interface UnaryInstruction_RI extends UnaryInstruction {
63
+ operand1: RegisterOrIdentifierNode;
64
+ }
65
+
66
+
67
+ /**
68
+ * @summary SNANZ(Set if Not-A-Number Non-Zero)指令节点
69
+ *
70
+ * @desc 如果操作数不是数字且非零,则将目标寄存器设置为 1,否则设置为 0。
71
+ * 用于检查特殊数值状态。
72
+ * @elseif en
73
+ * @summary SNANZ (Set if Not-A-Number Non-Zero) instruction node
74
+ *
75
+ * @desc Sets the target register to 1 if the operand is not a number and is non-zero, otherwise sets it to 0.
76
+ * Used to check special numeric status.
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * // SNANZ r0 r1
81
+ * // 如果 r1 是 NaN 且非零,则 r0 = 1
82
+ * ```
83
+ *
84
+ * @public
85
+ */
86
+ export interface SnanzInstructionNode extends UnaryInstruction_RI {
87
+ type: "snanzInstruction";
88
+ }
89
+
90
+
91
+ /**
92
+ * @summary PEEK(读取内存)指令节点
93
+ *
94
+ * @desc 从指定地址读取数据到目标寄存器。
95
+ * 用于读取堆栈或内存中的值。
96
+ * @elseif en
97
+ * @summary PEEK instruction node
98
+ *
99
+ * @desc Reads data from a specified address into the target register.
100
+ * Used to read values from stack or memory.
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * // PEEK r0 r1
105
+ * // 从地址 r1 读取数据到 r0
106
+ * ```
107
+ *
108
+ * @public
109
+ */
110
+ export interface PeekInstructionNode extends UnaryInstruction_RI {
111
+ type: "peekInstruction";
112
+ }
113
+
114
+
115
+ /**
116
+ * @summary RAND(随机数)指令节点
117
+ *
118
+ * @desc 生成一个随机数并存入目标寄存器。
119
+ * 随机数范围通常为 0 到 1 之间的浮点数。
120
+ * @elseif en
121
+ * @summary RAND (Random) instruction node
122
+ *
123
+ * @desc Generates a random number and stores it in the target register.
124
+ * The random number is typically a floating-point number between 0 and 1.
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * // RAND r0
129
+ * // 生成随机数并存入 r0
130
+ * ```
131
+ *
132
+ * @public
133
+ */
134
+ export interface RandInstructionNode extends UnaryInstruction_RI {
135
+ type: "randInstruction";
136
+ }
137
+
138
+
139
+ /**
140
+ * @summary SEQZ(Set if EQual Zero)指令节点
141
+ *
142
+ * @desc 如果操作数等于零,则将目标寄存器设置为 1,否则设置为 0。
143
+ * @elseif en
144
+ * @summary SEQZ (Set if EQual Zero) instruction node
145
+ *
146
+ * @desc Sets the target register to 1 if the operand equals zero, otherwise sets it to 0.
147
+ *
148
+ * @example
149
+ * ```typescript
150
+ * // SEQZ r0 r1
151
+ * // 如果 r1 == 0,则 r0 = 1
152
+ * ```
153
+ *
154
+ * @public
155
+ */
156
+ export interface SeqzInstructionNode extends UnaryInstruction_RI {
157
+ type: "seqzInstruction";
158
+ }
159
+
160
+
161
+ /**
162
+ * @summary SNEZ(Set if Not Equal Zero)指令节点
163
+ *
164
+ * @desc 如果操作数不等于零,则将目标寄存器设置为 1,否则设置为 0。
165
+ * @elseif en
166
+ * @summary SNEZ (Set if Not Equal Zero) instruction node
167
+ *
168
+ * @desc Sets the target register to 1 if the operand does not equal zero, otherwise sets it to 0.
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * // SNEZ r0 r1
173
+ * // 如果 r1 != 0,则 r0 = 1
174
+ * ```
175
+ *
176
+ * @public
177
+ */
178
+ export interface SnezInstructionNode extends UnaryInstruction_RI {
179
+ type: "snezInstruction";
180
+ }
181
+
182
+
183
+ /**
184
+ * @summary SGEZ(Set if Greater than or Equal Zero)指令节点
185
+ *
186
+ * @desc 如果操作数大于等于零,则将目标寄存器设置为 1,否则设置为 0。
187
+ * @elseif en
188
+ * @summary SGEZ (Set if Greater than or Equal Zero) instruction node
189
+ *
190
+ * @desc Sets the target register to 1 if the operand is greater than or equal to zero, otherwise sets it to 0.
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * // SGEZ r0 r1
195
+ * // 如果 r1 >= 0,则 r0 = 1
196
+ * ```
197
+ *
198
+ * @public
199
+ */
200
+ export interface SgezInstructionNode extends UnaryInstruction_RI {
201
+ type: "sgezInstruction";
202
+ }
203
+
204
+
205
+ /**
206
+ * @summary SGTZ(Set if Greater Than Zero)指令节点
207
+ *
208
+ * @desc 如果操作数大于零,则将目标寄存器设置为 1,否则设置为 0。
209
+ * @elseif en
210
+ * @summary SGTZ (Set if Greater Than Zero) instruction node
211
+ *
212
+ * @desc Sets the target register to 1 if the operand is greater than zero, otherwise sets it to 0.
213
+ *
214
+ * @example
215
+ * ```typescript
216
+ * // SGTZ r0 r1
217
+ * // 如果 r1 > 0,则 r0 = 1
218
+ * ```
219
+ *
220
+ * @public
221
+ */
222
+ export interface SgtzInstructionNode extends UnaryInstruction_RI {
223
+ type: "sgtzInstruction";
224
+ }
225
+
226
+
227
+ /**
228
+ * @summary SLEZ(Set if Less than or Equal Zero)指令节点
229
+ *
230
+ * @desc 如果操作数小于等于零,则将目标寄存器设置为 1,否则设置为 0。
231
+ * @elseif en
232
+ * @summary SLEZ (Set if Less than or Equal Zero) instruction node
233
+ *
234
+ * @desc Sets the target register to 1 if the operand is less than or equal to zero, otherwise sets it to 0.
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * // SLEZ r0 r1
239
+ * // 如果 r1 <= 0,则 r0 = 1
240
+ * ```
241
+ *
242
+ * @public
243
+ */
244
+ export interface SlezInstructionNode extends UnaryInstruction_RI {
245
+ type: "slezInstruction";
246
+ }
247
+
248
+
249
+ /**
250
+ * @summary SLTZ(Set if Less Than Zero)指令节点
251
+ *
252
+ * @desc 如果操作数小于零,则将目标寄存器设置为 1,否则设置为 0。
253
+ * @elseif en
254
+ * @summary SLTZ (Set if Less Than Zero) instruction node
255
+ *
256
+ * @desc Sets the target register to 1 if the operand is less than zero, otherwise sets it to 0.
257
+ *
258
+ * @example
259
+ * ```typescript
260
+ * // SLTZ r0 r1
261
+ * // 如果 r1 < 0,则 r0 = 1
262
+ * ```
263
+ *
264
+ * @public
265
+ */
266
+ export interface SltzInstructionNode extends UnaryInstruction_RI {
267
+ type: "sltzInstruction";
268
+ }
269
+
270
+
271
+ /**
272
+ * @summary SNAN(Set if Not-A-Number)指令节点
273
+ *
274
+ * @desc 如果操作数是 NaN(非数字),则将目标寄存器设置为 1,否则设置为 0。
275
+ * @elseif en
276
+ * @summary SNAN (Set if Not-A-Number) instruction node
277
+ *
278
+ * @desc Sets the target register to 1 if the operand is NaN (Not a Number), otherwise sets it to 0.
279
+ *
280
+ * @example
281
+ * ```typescript
282
+ * // SNAN r0 r1
283
+ * // 如果 r1 是 NaN,则 r0 = 1
284
+ * ```
285
+ *
286
+ * @public
287
+ */
288
+ export interface SnanInstructionNode extends UnaryInstruction_RI {
289
+ type: "snanInstruction";
290
+ }
291
+
292
+
293
+ /**
294
+ * @summary POP(弹出堆栈)指令节点
295
+ *
296
+ * @desc 从堆栈中弹出一个值并存入目标寄存器。
297
+ * 用于恢复之前保存的寄存器值。
298
+ * @elseif en
299
+ * @summary POP instruction node
300
+ *
301
+ * @desc Pops a value from the stack and stores it in the target register.
302
+ * Used to restore previously saved register values.
303
+ *
304
+ * @example
305
+ * ```typescript
306
+ * // POP r0
307
+ * // 弹出堆栈顶部值到 r0
308
+ * ```
309
+ *
310
+ * @public
311
+ */
312
+ export interface PopInstructionNode extends UnaryInstruction_RI {
313
+ type: "popInstruction";
314
+ }
315
+
316
+
317
+ /**
318
+ * @summary 含单个 DeviceReference 操作数的一元指令基类
319
+ *
320
+ * @desc 操作数为设备引用节点。
321
+ * @elseif en
322
+ * @summary Base interface for unary instructions with single DeviceReference operand
323
+ *
324
+ * @desc Operand is a device reference node.
325
+ *
326
+ * @public
327
+ */
328
+ interface UnaryInstruction_DR extends UnaryInstruction {
329
+ operand1: DeviceReferenceNode;
330
+ }
331
+
332
+
333
+ /**
334
+ * @summary CLR(Clear Device)指令节点
335
+ *
336
+ * @desc 清除指定设备的所有数据或状态。
337
+ * @elseif en
338
+ * @summary CLR (Clear Device) instruction node
339
+ *
340
+ * @desc Clears all data or state of the specified device.
341
+ *
342
+ * @example
343
+ * ```typescript
344
+ * // CLR console1
345
+ * // 清除 console1 设备的数据
346
+ * ```
347
+ *
348
+ * @public
349
+ */
350
+ export interface ClrInstructionNode extends UnaryInstruction_DR {
351
+ type: "clrInstruction";
352
+ }
353
+
354
+
355
+ /**
356
+ * @summary 含单个 Operand 操作数的一元指令基类
357
+ *
358
+ * @desc 操作数为通用操作数类型。
359
+ * @elseif en
360
+ * @summary Base interface for unary instructions with single Operand operand
361
+ *
362
+ * @desc Operand is a general operand type.
363
+ *
364
+ * @public
365
+ */
366
+ interface UnaryInstruction_O extends UnaryInstruction {
367
+ operand1: OperandNode;
368
+ }
369
+
370
+
371
+ /**
372
+ * @summary SLEEP(休眠)指令节点
373
+ *
374
+ * @desc 让 IC 芯片进入休眠状态一段时间。
375
+ * 休眠期间芯片不执行任何操作,用于节流或等待外部事件。
376
+ * @elseif en
377
+ * @summary SLEEP instruction node
378
+ *
379
+ * @desc Puts the IC chip into sleep mode for a specified duration.
380
+ * The chip does not execute any operations during sleep, used for throttling or waiting for external events.
381
+ *
382
+ * @example
383
+ * ```typescript
384
+ * // SLEEP 10
385
+ * // 休眠 10 个时间单位
386
+ * ```
387
+ *
388
+ * @public
389
+ */
390
+ export interface SleepInstructionNode extends UnaryInstruction_O {
391
+ type: "sleepInstruction";
392
+ }
393
+
394
+
395
+ /**
396
+ * @summary PUSH(压入堆栈)指令节点
397
+ *
398
+ * @desc 将操作数的值压入堆栈。
399
+ * 用于保存寄存器值以便后续恢复。
400
+ * @elseif en
401
+ * @summary PUSH instruction node
402
+ *
403
+ * @desc Pushes the value of the operand onto the stack.
404
+ * Used to save register values for later restoration.
405
+ *
406
+ * @example
407
+ * ```typescript
408
+ * // PUSH r0
409
+ * // 将 r0 的值压入堆栈
410
+ * ```
411
+ *
412
+ * @public
413
+ */
414
+ export interface PushInstructionNode extends UnaryInstruction_O {
415
+ type: "pushInstruction";
416
+ }
417
+
418
+
419
+ /**
420
+ * @summary JAL(Jump And Link)指令节点
421
+ *
422
+ * @desc 跳转到指定地址并将返回地址保存到链接寄存器(通常是 r15)。
423
+ * 用于函数调用。
424
+ * @elseif en
425
+ * @summary JAL (Jump And Link) instruction node
426
+ *
427
+ * @desc Jumps to the specified address and saves the return address to the link register (typically r15).
428
+ * Used for function calls.
429
+ *
430
+ * @example
431
+ * ```typescript
432
+ * // JAL main
433
+ * // 跳转到 main 标签,保存返回地址到 r15
434
+ * ```
435
+ *
436
+ * @public
437
+ */
438
+ export interface JalInstructionNode extends UnaryInstruction_O {
439
+ type: "jalInstruction";
440
+ }
441
+
442
+
443
+ /**
444
+ * @summary JR(Jump Register)指令节点
445
+ *
446
+ * @desc 跳转到寄存器中存储的地址。
447
+ * 用于间接跳转,通常配合 JAL 使用。
448
+ * @elseif en
449
+ * @summary JR (Jump Register) instruction node
450
+ *
451
+ * @desc Jumps to the address stored in a register.
452
+ * Used for indirect jumps, often used with JAL.
453
+ *
454
+ * @example
455
+ * ```typescript
456
+ * // JR r15
457
+ * // 跳转到 r15 中存储的地址(返回)
458
+ * ```
459
+ *
460
+ * @public
461
+ */
462
+ export interface JrInstructionNode extends UnaryInstruction_O {
463
+ type: "jrInstruction";
464
+ }
465
+
466
+
467
+ /**
468
+ * @summary J(Jump)指令节点
469
+ *
470
+ * @desc 无条件跳转到指定地址或标签。
471
+ * @elseif en
472
+ * @summary J (Jump) instruction node
473
+ *
474
+ * @desc Unconditionally jumps to the specified address or label.
475
+ *
476
+ * @example
477
+ * ```typescript
478
+ * // J loop
479
+ * // 跳转到 loop 标签
480
+ * ```
481
+ *
482
+ * @public
483
+ */
484
+ export interface JInstructionNode extends UnaryInstruction_O {
485
+ type: "jInstruction";
486
+ }
487
+
488
+
489
+ /**
490
+ * @summary 一元指令联合类型
491
+ *
492
+ * @desc 包含所有一元指令类型的联合,包括可能的错误节点。
493
+ * @elseif en
494
+ * @summary Unary instruction union type
495
+ *
496
+ * @desc Union of all unary instruction types, including possible error nodes.
497
+ *
498
+ * @public
499
+ */
500
+ export type UnaryInstructionNode =
501
+ | SnanzInstructionNode
502
+ | PeekInstructionNode
503
+ | RandInstructionNode
504
+ | SeqzInstructionNode
505
+ | SnezInstructionNode
506
+ | SgezInstructionNode
507
+ | SgtzInstructionNode
508
+ | SlezInstructionNode
509
+ | SltzInstructionNode
510
+ | SnanInstructionNode
511
+ | PopInstructionNode
512
+ | ClrInstructionNode
513
+ | SleepInstructionNode
514
+ | PushInstructionNode
515
+ | JalInstructionNode
516
+ | JrInstructionNode
517
+ | JInstructionNode
518
+ | ErrorNode;
@@ -0,0 +1,84 @@
1
+ // Copyright (c) 2026. All rights reserved.
2
+ // This source code is licensed under the CC BY-NC-SA
3
+ // (Creative Commons Attribution-NonCommercial-NoDerivatives) License, By Xiao Songtao.
4
+ // This software is protected by copyright law. Reproduction, distribution, or use for commercial
5
+ // purposes is prohibited without the author's permission. If you have any questions or require
6
+ // permission, please contact the author: edocsitahw@qq.com
7
+
8
+ /**
9
+ * @file parser.d.ts
10
+ * @author edocsitahw
11
+ * @version 1.1
12
+ * @date 2026/07/22 15:38
13
+ * @desc
14
+ * @copyright CC BY-NC-SA 2026. All rights reserved.
15
+ * */
16
+ import { Diagnostic } from "../common";
17
+ import { Token } from "../lexer/token";
18
+ import { Program } from "./program";
19
+
20
+
21
+ /**
22
+ * @summary 语法分析器类
23
+ *
24
+ * @desc 将 Token 序列解析为 AST(抽象语法树)。
25
+ * 支持调试模式,可以在解析过程中输出调试信息。
26
+ *
27
+ * @elseif en
28
+ * @summary Parser class
29
+ *
30
+ * @desc Parses a sequence of Tokens into an AST (Abstract Syntax Tree).
31
+ * Supports debug mode to output debug information during parsing.
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * // 创建语法分析器
36
+ * const parser = new ic10.Parser(tokens, true);
37
+ *
38
+ * // 解析 tokens
39
+ * const program = parser.parse();
40
+ * console.log(JSON.parse(program.toJSON()));
41
+ * ```
42
+ *
43
+ * @public
44
+ */
45
+ export class Parser {
46
+ /**
47
+ * @summary 构造函数
48
+ *
49
+ * @param tokens - Token 数组
50
+ * @param debug - 是否启用调试模式(可选,默认为 false)
51
+ *
52
+ * @desc 创建语法分析器实例。
53
+ * 调用 {@link parse} 方法执行实际的解析操作。
54
+ */
55
+ constructor(tokens: Token[], debug?: boolean);
56
+
57
+ /**
58
+ * @summary 解析 Token 序列
59
+ *
60
+ * @returns 程序节点
61
+ *
62
+ * @desc 执行实际的语法分析,将 Token 序列转换为 AST。
63
+ */
64
+ parse(): Program;
65
+
66
+ /**
67
+ * @summary 静态方法:解析 Token 序列
68
+ *
69
+ * @param tokens - Token 数组
70
+ * @param debug - 是否启用调试模式(可选,默认为 false)
71
+ * @returns 程序节点
72
+ *
73
+ * @desc 便捷方法,无需创建 Parser 实例即可解析。
74
+ */
75
+ parsing(tokens: Token[], debug?: boolean): Program;
76
+
77
+ /**
78
+ * @summary 诊断列表
79
+ *
80
+ * @desc 语法分析过程中产生的诊断信息(包含错误、警告、提示)。
81
+ * 每个诊断对象包含 level、id、start、end、message 字段。
82
+ */
83
+ get diagnostics(): Diagnostic[];
84
+ }