Skip to content

Commit ff79389

Browse files
committed
refactor(parser): split parser.ts into ast-types.ts and generate.ts
1 parent 9dd4a4d commit ff79389

13 files changed

Lines changed: 542 additions & 517 deletions

src/ast-types.ts

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
/**
2+
* AST 节点类型定义
3+
*/
4+
5+
export interface NumberLiteral {
6+
type: "NumberLiteral";
7+
value: number;
8+
raw: string;
9+
}
10+
11+
export interface StringLiteral {
12+
type: "StringLiteral";
13+
value: string;
14+
quote: "'" | '"' | "`";
15+
}
16+
17+
export interface BooleanLiteral {
18+
type: "BooleanLiteral";
19+
value: boolean;
20+
}
21+
22+
export interface NullLiteral {
23+
type: "NullLiteral";
24+
}
25+
26+
export interface Identifier {
27+
type: "Identifier";
28+
name: string;
29+
}
30+
31+
export interface BinaryExpr {
32+
type: "BinaryExpr";
33+
operator: string;
34+
left: ASTNode;
35+
right: ASTNode;
36+
}
37+
38+
export interface UnaryExpr {
39+
type: "UnaryExpr";
40+
operator: string;
41+
argument: ASTNode;
42+
prefix: boolean;
43+
}
44+
45+
export interface ConditionalExpr {
46+
type: "ConditionalExpr";
47+
test: ASTNode;
48+
consequent: ASTNode;
49+
alternate: ASTNode;
50+
}
51+
52+
export interface MemberExpr {
53+
type: "MemberExpr";
54+
object: ASTNode;
55+
property: ASTNode;
56+
computed: boolean;
57+
optional: boolean;
58+
}
59+
60+
export interface CallExpr {
61+
type: "CallExpr";
62+
callee: ASTNode;
63+
arguments: ASTNode[];
64+
optional: boolean;
65+
}
66+
67+
export interface ArrayExpr {
68+
type: "ArrayExpr";
69+
elements: ASTNode[];
70+
}
71+
72+
export interface ObjectExpr {
73+
type: "ObjectExpr";
74+
properties: ObjectProperty[];
75+
}
76+
77+
export interface ObjectProperty {
78+
key: ASTNode;
79+
value: ASTNode;
80+
computed: boolean;
81+
shorthand: boolean;
82+
}
83+
84+
export interface ArrowFunctionExpr {
85+
type: "ArrowFunctionExpr";
86+
params: Identifier[];
87+
body: ASTNode;
88+
}
89+
90+
// AST 节点类型定义
91+
export type ASTNode =
92+
| NumberLiteral
93+
| StringLiteral
94+
| BooleanLiteral
95+
| NullLiteral
96+
| Identifier
97+
| BinaryExpr
98+
| UnaryExpr
99+
| ConditionalExpr
100+
| MemberExpr
101+
| CallExpr
102+
| ArrayExpr
103+
| ObjectExpr
104+
| ArrowFunctionExpr;
105+
106+
// 运算符优先级(从低到高)
107+
export const PRECEDENCE: Record<string, number> = {
108+
"||": 1,
109+
"??": 1,
110+
"&&": 2,
111+
"|": 3,
112+
"^": 4,
113+
"&": 5,
114+
"==": 6,
115+
"!=": 6,
116+
"===": 6,
117+
"!==": 6,
118+
"<": 7,
119+
">": 7,
120+
"<=": 7,
121+
">=": 7,
122+
in: 7,
123+
instanceof: 7,
124+
"<<": 8,
125+
">>": 8,
126+
">>>": 8,
127+
"+": 9,
128+
"-": 9,
129+
"*": 10,
130+
"/": 10,
131+
"%": 10,
132+
"**": 11,
133+
};
134+
135+
// 右结合运算符
136+
export const RIGHT_ASSOCIATIVE = new Set(["**"]);
137+
138+
// 内置构造函数列表
139+
export const BUILTIN_CONSTRUCTORS = new Set([
140+
"Date",
141+
"RegExp",
142+
"URL",
143+
"URLSearchParams",
144+
"Map",
145+
"Set",
146+
"Int8Array",
147+
"Uint8Array",
148+
"Uint8ClampedArray",
149+
"Int16Array",
150+
"Uint16Array",
151+
"Int32Array",
152+
"Uint32Array",
153+
"Float32Array",
154+
"Float64Array",
155+
"BigInt64Array",
156+
"BigUint64Array",
157+
"ArrayBuffer",
158+
"DataView",
159+
]);

src/compile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { generate, transformIdentifiers, type ASTNode } from "./parser";
1+
import type { ASTNode } from "./ast-types";
2+
import { generate, transformIdentifiers } from "./generate";
23
import { serializeArgumentToAST } from "./proxy-variable";
34
import type { BranchNode, CompiledData, CompiledExpression, JumpNode, LambdaBodyResult, PhiNode } from "./types";
45
import { getVariableId } from "./variable";

src/expr.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { parse, transformIdentifiers, type ASTNode } from "./parser";
1+
import type { ASTNode } from "./ast-types";
2+
import { transformIdentifiers } from "./generate";
3+
import { parse } from "./parser";
24
import { getProxyMetadata } from "./proxy-metadata";
35
import { createProxyExpressionWithAST } from "./proxy-variable";
46
import type { InferExpressionResult, ValidateExpression } from "./type-parser";

0 commit comments

Comments
 (0)