-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ts
155 lines (125 loc) · 4.51 KB
/
parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import {
Statement,
Program,
Expression,
BinaryExpression,
NumericLiteral,
Identifier,
VariableDeclaration,
AssignmentExpression,
} from "./ast.ts";
import { tokenize, Token, TokenType } from "./tokenizer.ts";
export default class Parser {
private tokens: Token[] = [];
public buildAST(input: string): Program {
this.tokens = tokenize(input);
const program: Program = {
kind: "Program",
body: [],
};
while (this.tokens[0].type !== TokenType.EOF) {
program.body.push(this.parseStatement());
}
return program;
}
private getCurrentToken(): Token {
return this.tokens.shift() as Token;
}
private parseStatement(): Statement {
switch (this.tokens[0].type) {
case TokenType.VariableDeclaration:
case TokenType.Const:
return this.parseVariableDeclaration();
default:
return this.parseExpression();
}
}
private parseVariableDeclaration(): Statement {
const isConstant = this.getCurrentToken().type === TokenType.Const;
const identifier = this.getCurrentToken().value;
if (!identifier) {
throw new Error("Expected identifier");
}
if (this.tokens[0].type == TokenType.Semicolon) {
this.getCurrentToken();
if (isConstant) {
throw new Error("Expected assignment");
}
return { kind: "VariableDeclaration", identifier, constant: false } as VariableDeclaration;
}
if (this.tokens[0].type !== TokenType.Equals) {
throw new Error("Expected assignment");
}
this.getCurrentToken();
const declaration = {
kind: "VariableDeclaration",
identifier,
value: this.parseExpression(),
constant: isConstant,
} as VariableDeclaration;
if (this.tokens[0].type !== TokenType.Semicolon) {
throw new Error("Expected semicolon");
}
this.getCurrentToken();
return declaration;
}
private parseExpression(): Expression {
return this.parseAssignmentExpression();
}
private parseAssignmentExpression(): Expression {
const left = this.parseAdditiveExpression();
if (this.tokens[0].type === TokenType.Equals) {
this.getCurrentToken();
const value = this.parseAssignmentExpression();
return { value, assigne: left, kind: "AssignmentExpression" } as AssignmentExpression;
}
return left;
}
private parseAdditiveExpression(): Expression {
let left = this.parseMultiplicativeExpression();
while (this.tokens[0].value === "+" || this.tokens[0].value === "-") {
const operator = this.getCurrentToken().value;
const right = this.parseMultiplicativeExpression();
left = {
kind: "BinaryExpression",
operator,
left,
right,
} as BinaryExpression;
}
return left;
}
private parseMultiplicativeExpression(): Expression {
let left = this.parsePrimaryExpression();
while (this.tokens[0].value === "*" || this.tokens[0].value === "/" || this.tokens[0].value === "%") {
const operator = this.getCurrentToken().value;
const right = this.parsePrimaryExpression();
left = {
kind: "BinaryExpression",
operator,
left,
right,
} as BinaryExpression;
}
return left;
}
private parsePrimaryExpression(): Expression {
const token = this.tokens[0].type;
switch (token) {
case TokenType.Identifier:
return { kind: "Identifier", symbol: this.getCurrentToken().value } as Identifier;
case TokenType.Number:
return { kind: "NumericLiteral", value: parseFloat(this.getCurrentToken().value) } as NumericLiteral;
case TokenType.OpenParen:
this.getCurrentToken();
const expression = this.parseExpression();
if (this.tokens[0].type !== TokenType.CloseParen) {
throw new Error("Expected closing parenthesis");
}
this.getCurrentToken();
return expression;
default:
throw new Error(`Unexpected token: ${token}`);
}
}
}