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,136 @@
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 program.d.ts
10
+ * @author edocsitahw
11
+ * @version 1.1
12
+ * @date 2026/07/22 15:52
13
+ * @desc
14
+ * @copyright CC BY-NC-SA 2026. All rights reserved.
15
+ * */
16
+ import {Pos} from "../common";
17
+ import {LabelDefNode, PreprocessorDirectiveNode, ErrorNode} from "./ast";
18
+ import {
19
+ NullaryInstructionNode,
20
+ UnaryInstructionNode,
21
+ BinaryInstructionNode,
22
+ TernaryInstructionNode,
23
+ QuaternaryInstructionNode, QuinaryInstructionNode, SenaryInstructionNode
24
+ } from "./instructions";
25
+
26
+
27
+ /**
28
+ * @summary 可执行指令联合类型
29
+ *
30
+ * @desc 包含所有可执行指令类型的联合,包括零元、一元、二元、三元、四元、五元和六元指令。
31
+ * 这是 IC10 程序中所有可执行指令的完整集合。
32
+ *
33
+ * @elseif en
34
+ * @summary Executable instruction union type
35
+ *
36
+ * @desc Union of all executable instruction types, including nullary, unary, binary, ternary, quaternary, quinary, and senary instructions.
37
+ * This is the complete set of all executable instructions in IC10 programs.
38
+ *
39
+ * @public
40
+ */
41
+ export type ExecutableInstructionNode =
42
+ | NullaryInstructionNode
43
+ | UnaryInstructionNode
44
+ | BinaryInstructionNode
45
+ | TernaryInstructionNode
46
+ | QuaternaryInstructionNode
47
+ | QuinaryInstructionNode
48
+ | SenaryInstructionNode;
49
+
50
+ export type PureExeInstructionNode = Exclude<ExecutableInstructionNode, ErrorNode>;
51
+
52
+ /**
53
+ * @summary 语句联合类型
54
+ *
55
+ * @desc 包含 IC10 程序中所有语句类型的联合:
56
+ * - 可执行指令({@link ExecutableInstructionNode})
57
+ * - 标签定义({@link LabelDefNode})
58
+ * - 预处理指令({@link PreprocessorDirectiveNode})
59
+ *
60
+ * @elseif en
61
+ * @summary Statement union type
62
+ *
63
+ * @desc Union of all statement types in IC10 programs:
64
+ * - Executable instructions ({@link ExecutableInstructionNode})
65
+ * - Label definitions ({@link LabelDefNode})
66
+ * - Preprocessor directives ({@link PreprocessorDirectiveNode})
67
+ *
68
+ * @public
69
+ */
70
+ export type StatementNode =
71
+ | ExecutableInstructionNode
72
+ | LabelDefNode
73
+ | PreprocessorDirectiveNode;
74
+
75
+
76
+ /**
77
+ * @summary 程序节点类
78
+ *
79
+ * @desc 表示 IC10 程序的根节点。
80
+ * 包含程序的所有语句和结束位置信息。
81
+ *
82
+ * @elseif en
83
+ * @summary Program node class
84
+ *
85
+ * @desc Represents the root node of an IC10 program.
86
+ * Contains all statements of the program and end position information.
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * // 解析 IC10 代码
91
+ * const parser = new ic10.Parser(tokens);
92
+ * const program = parser.parse();
93
+ *
94
+ * // 访问程序语句
95
+ * console.log(`语句数量: ${program.statements.length}`);
96
+ * ```
97
+ *
98
+ * @public
99
+ */
100
+ export class Program {
101
+ /**
102
+ * @summary 节点名称
103
+ * @desc 通常为 "Program"
104
+ */
105
+ nodeName: string;
106
+
107
+ /**
108
+ * @summary 程序语句列表
109
+ * @desc 包含程序中的所有语句(指令、标签、预处理指令等)
110
+ */
111
+ statements: StatementNode[];
112
+
113
+ /**
114
+ * @summary 结束位置
115
+ * @desc 程序在源代码中的结束位置
116
+ */
117
+ end: Pos;
118
+
119
+ /**
120
+ * @summary 返回可读字符串表示
121
+ *
122
+ * @returns 格式化的程序字符串
123
+ *
124
+ * @desc 用于调试目的,返回人类可读的程序表示。
125
+ */
126
+ toString(): string;
127
+
128
+ /**
129
+ * @summary 返回 JSON 字符串表示
130
+ *
131
+ * @returns JSON 格式的程序表示
132
+ *
133
+ * @desc 返回包含 nodeName、statements 和 end 的 JSON 对象字符串。
134
+ */
135
+ toJSON(): string;
136
+ }
@@ -0,0 +1,98 @@
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 analyser.d.ts
10
+ * @author edocsitahw
11
+ * @version 1.1
12
+ * @date 2026/07/22 16:01
13
+ * @desc
14
+ * @copyright CC BY-NC-SA 2026. All rights reserved.
15
+ * */
16
+
17
+ import {Diagnostic} from "../common";
18
+ import {Program} from "../parser";
19
+ import {SymbolTable} from "./semantic";
20
+ import {TypeTable} from "./type_table";
21
+
22
+
23
+ /**
24
+ * @summary 静态分析器类
25
+ *
26
+ * @desc 对 IC10 程序进行静态分析,包括:
27
+ * - 符号表构建
28
+ * - 语义检查
29
+ * - 类型检查
30
+ * - 错误检测
31
+ *
32
+ * @elseif en
33
+ * @summary Static analyser class
34
+ *
35
+ * @desc Performs static analysis of IC10 programs, including:
36
+ * - Symbol table construction
37
+ * - Semantic checking
38
+ * - Type checking
39
+ * - Error detection
40
+ *
41
+ * @example
42
+ * ```typescript
43
+ * // 创建分析器并分析程序
44
+ * const analyser = new ic10.Analyser();
45
+ * analyser.visit(program);
46
+ *
47
+ * // 检查是否有诊断信息
48
+ * if (analyser.diagnostics.length > 0) {
49
+ * console.log('诊断信息:', analyser.diagnostics);
50
+ * }
51
+ *
52
+ * // 获取符号表
53
+ * const symbolTable = analyser.symbolTable;
54
+ * ```
55
+ *
56
+ * @public
57
+ */
58
+ export class Analyser {
59
+ /**
60
+ * @summary 符号表
61
+ *
62
+ * @desc 分析过程中构建的符号表,包含所有定义的符号信息。
63
+ */
64
+ get symbolTable(): SymbolTable;
65
+
66
+ /**
67
+ * @summary 类型表
68
+ *
69
+ * @desc 分析过程中构建的类型表,包含所有自定义类型(设备类型和枚举类型)。
70
+ */
71
+ get typeTable(): TypeTable;
72
+
73
+ /**
74
+ * @summary 诊断列表
75
+ *
76
+ * @desc 分析过程中产生的诊断信息(包含错误、警告、提示)。
77
+ * 每个诊断对象包含 level、id、start、end、message 字段。
78
+ */
79
+ get diagnostics(): Diagnostic[];
80
+
81
+ /**
82
+ * @summary 静态方法:分析程序
83
+ *
84
+ * @param program - 要分析的 Program 节点
85
+ *
86
+ * @desc 便捷方法,直接分析程序而不需要创建 Analyser 实例。
87
+ */
88
+ static analyse(program: Program): void;
89
+
90
+ /**
91
+ * @summary 访问程序节点
92
+ *
93
+ * @param program - 要访问的 Program 节点
94
+ *
95
+ * @desc 执行实际的静态分析,更新符号表和诊断列表。
96
+ */
97
+ visit(program: Program): Promise<void>;
98
+ }
@@ -0,0 +1,19 @@
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 index.d.ts
10
+ * @author edocsitahw
11
+ * @version 1.1
12
+ * @date 2026/07/22 17:16
13
+ * @desc
14
+ * @copyright CC BY-NC-SA 2026. All rights reserved.
15
+ * */
16
+ export * from "./analyser";
17
+ export * from "./semantic";
18
+ export * from "./types";
19
+ export * from "./type_table";
@@ -0,0 +1,81 @@
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 semantic.d.ts
10
+ * @author edocsitahw
11
+ * @version 1.1
12
+ * @date 2026/07/22 17:13
13
+ * @desc
14
+ * @copyright CC BY-NC-SA 2026. All rights reserved.
15
+ * */
16
+ import {BasicType} from "./types";
17
+ import {TypeCategory} from "./types";
18
+
19
+
20
+ /**
21
+ * @summary 符号表类
22
+ *
23
+ * @desc 存储 IC10 程序中的符号信息(如标签、别名定义等)。
24
+ * 用于静态分析和代码验证。
25
+ *
26
+ * @elseif en
27
+ * @summary Symbol table class
28
+ *
29
+ * @desc Stores symbol information in IC10 programs (such as labels, alias definitions, etc.).
30
+ * Used for static analysis and code validation.
31
+ *
32
+ * @example
33
+ * ```typescript
34
+ * // 执行静态分析
35
+ * ic10.Analyser.analyse(program);
36
+ *
37
+ * // 获取符号表
38
+ * const symbolTable = analyser.symbolTable;
39
+ * console.log(JSON.parse(symbolTable.toJSON()));
40
+ * ```
41
+ *
42
+ * @public
43
+ */
44
+ export class SymbolTable {
45
+ /**
46
+ * @summary 返回 JSON 字符串表示
47
+ *
48
+ * @returns JSON 格式的符号表表示
49
+ *
50
+ * @desc 返回包含所有符号信息的 JSON 对象字符串。
51
+ */
52
+ toJSON(): string;
53
+ }
54
+
55
+
56
+ /**
57
+ * @summary 符号信息
58
+ *
59
+ * @desc 表示符号表中的单个符号,由 `SymbolTable.toJSON()` 序列化输出。
60
+ *
61
+ * @public
62
+ */
63
+ export interface Symbol {
64
+ /** 符号名称 */
65
+ name: string;
66
+ /** 基本类型(BasicType 枚举值) */
67
+ type: BasicType;
68
+ /** 类型类别(TypeCategory 枚举值) */
69
+ category: TypeCategory;
70
+ /** 类型名称(可选,如设备类型名) */
71
+ typeName?: string;
72
+ /** 符号值(可选) */
73
+ value?: string;
74
+ /** 描述信息(可选) */
75
+ desc?: string;
76
+ }
77
+
78
+
79
+ export interface SymbolMap {
80
+ [key: string]: Symbol;
81
+ }
@@ -0,0 +1,119 @@
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 type_table.d.ts
10
+ * @author edocsitahw
11
+ * @version 1.0
12
+ * @date 2026/07/26
13
+ * @desc
14
+ * @copyright CC BY-NC-SA 2026. All rights reserved.
15
+ * */
16
+
17
+ /**
18
+ * @summary 类型表类
19
+ *
20
+ * @desc 管理 IC10 程序中的自定义类型定义(设备类型和枚举类型)。
21
+ * 支持 JSON 序列化。
22
+ *
23
+ * @elseif en
24
+ * @summary Type table class
25
+ *
26
+ * @desc Manages custom type definitions in IC10 programs (device types and enum types).
27
+ * Supports JSON serialization.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const analyser = new ic10.Analyser();
32
+ * await analyser.visit(program);
33
+ * const typeTable = analyser.typeTable;
34
+ * console.log(JSON.parse(typeTable.toJSON()));
35
+ * ```
36
+ *
37
+ * @public
38
+ */
39
+ export class TypeTable {
40
+ /**
41
+ * @summary 返回 JSON 字符串表示
42
+ *
43
+ * @returns JSON 格式的类型表表示
44
+ *
45
+ * @desc 返回包含所有自定义类型信息的 JSON 对象字符串。
46
+ */
47
+ toJSON(): string;
48
+ }
49
+
50
+ /** 描述值 */
51
+ export interface DescValue {
52
+ kind: "text" | "link";
53
+ value: string;
54
+ }
55
+
56
+ /** 设备插槽 */
57
+ export interface DeviceSlot {
58
+ index: string;
59
+ direction: "input" | "output";
60
+ desc?: DescValue;
61
+ }
62
+
63
+ /** 设备逻辑 */
64
+ export interface DeviceLogic {
65
+ name: string;
66
+ access: "R" | "W" | "RW";
67
+ }
68
+
69
+ /** 设备模式 */
70
+ export interface DeviceMode {
71
+ index: string;
72
+ desc?: DescValue;
73
+ }
74
+
75
+ /** 设备逻辑插槽 */
76
+ export interface DeviceLogicSlot {
77
+ name: string;
78
+ }
79
+
80
+ /** 设备连接 */
81
+ export interface DeviceConnect {
82
+ index: string;
83
+ desc?: DescValue;
84
+ }
85
+
86
+ /** 设备类型 (from toJSON()) */
87
+ export interface DeviceType {
88
+ type: "device";
89
+ name: string;
90
+ desc?: DescValue;
91
+ slots: DeviceSlot[];
92
+ logics: DeviceLogic[];
93
+ modes: DeviceMode[];
94
+ logicSlots: string[];
95
+ connects: DeviceConnect[];
96
+ }
97
+
98
+ /** 枚举值条目 */
99
+ export interface EnumValueEntry {
100
+ name: string;
101
+ value: string;
102
+ desc?: DescValue;
103
+ }
104
+
105
+ /** 枚举类型 (from toJSON()) */
106
+ export interface EnumType {
107
+ type: "enum";
108
+ name: string;
109
+ desc?: DescValue;
110
+ values: EnumValueEntry[];
111
+ }
112
+
113
+ /** 自定义类型 */
114
+ export type CustomType = DeviceType | EnumType;
115
+
116
+ /** 类型表 JSON 映射 */
117
+ export interface TypeTableMap {
118
+ [key: string]: CustomType;
119
+ }
@@ -0,0 +1,100 @@
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 types.d.ts
10
+ * @author edocsitahw
11
+ * @version 1.1
12
+ * @date 2026/07/22 17:13
13
+ * @desc
14
+ * @copyright CC BY-NC-SA 2026. All rights reserved.
15
+ * */
16
+
17
+ /**
18
+ * @summary 基础类型枚举
19
+ *
20
+ * @desc 定义 IC10 语言支持的基础数据类型。
21
+ *
22
+ * @public
23
+ */
24
+ export enum BasicType {
25
+ /** 字符串类型 */
26
+ STRING = 0,
27
+ /** 整数类型 */
28
+ INTEGER,
29
+ /** 浮点类型 */
30
+ FLOAT,
31
+ /** 寄存器类型 (r0-r15) */
32
+ REGISTER,
33
+ /** 设备类型 (@开头) */
34
+ DEVICE,
35
+ /** 未知类型(解析错误) */
36
+ UNKNOWN,
37
+ /** 枚举类型 */
38
+ ENUM
39
+ }
40
+
41
+
42
+ /**
43
+ * @summary 类型类别枚举
44
+ *
45
+ * @desc 定义类型的语义类别,用于区分不同语义用途的类型。
46
+ *
47
+ * @public
48
+ */
49
+ export enum TypeCategory {
50
+ /** 标签类别 */
51
+ LABEL = 0,
52
+ /** STR宏调用类别 */
53
+ STR_CALL,
54
+ /** HASH宏调用类别 */
55
+ HASH_CALL,
56
+ /** 常量类别 */
57
+ CONSTANT,
58
+ /** 数字类别 */
59
+ NUMBER,
60
+ /** 基础类别 */
61
+ BASIC
62
+ }
63
+
64
+
65
+ /**
66
+ * @summary 节点类型映射接口
67
+ *
68
+ * @desc 定义 AST 节点类型名到 BasicType 和 TypeCategory 的映射。
69
+ *
70
+ * @public
71
+ */
72
+ export interface TypeOfNodeEntry {
73
+ /** 基础类型 */
74
+ kind: BasicType;
75
+ /** 类型类别 */
76
+ category: TypeCategory;
77
+ }
78
+
79
+
80
+ /**
81
+ * @summary AST 节点类型映射
82
+ *
83
+ * @desc 将节点类型名映射到对应的类型信息。
84
+ *
85
+ * @public
86
+ */
87
+ export const TypeOfNode: {
88
+ Integer: TypeOfNodeEntry;
89
+ Float: TypeOfNodeEntry;
90
+ String: TypeOfNodeEntry;
91
+ StrCall: TypeOfNodeEntry;
92
+ HashCall: TypeOfNodeEntry;
93
+ Register: TypeOfNodeEntry;
94
+ Device: TypeOfNodeEntry;
95
+ LabelDef: TypeOfNodeEntry;
96
+ ErrorNode: TypeOfNodeEntry;
97
+ Constant: TypeOfNodeEntry;
98
+ BinaryNumber: TypeOfNodeEntry;
99
+ HexNumber: TypeOfNodeEntry;
100
+ };