-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.js
More file actions
141 lines (113 loc) · 3.88 KB
/
Copy pathParser.js
File metadata and controls
141 lines (113 loc) · 3.88 KB
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
class Parser {
lexercode = []
i = 0
constructor(lex) {
this.lexercode = lex
}
parse() {
const body = [];
while (this.peek().type !== 'EOF') {
body.push(this.parseExpression());
}
return {
type: 'Program',
body: body
};
}
peek() { return this.lexercode[this.i]; }
advance() {
let a = this.lexercode[this.i];
this.i++;
return a;
}
parseExpression() {
const token = this.peek();
if (token.type === 'NUMBER') {
return this.Number();
}
if (token.type === 'IDENTIFIER_$') {
this.advance();
this.consume('LEFT_BRACKET', "Oczekiwano '[' po znaku $");
const indexExpr = this.parseExpression();
this.consume('RIGHT_BRACKET', "Oczekiwano ']' po indeksie pamięci");
return {
type: 'MemoryAccess',
index: indexExpr
};
}
if (token.type === 'LEFT_PAREN') {
this.advance();
const operator = this.advance();
if (operator.type === 'OUT_FUNCTION') return this.outExpression();
if (operator.type === 'IN_FUNCTION') return this.inExpression();
if (operator.type === 'PLUS') return this.plusExpression();
if (operator.type === 'MINUS') return this.minusExpression();
if (operator.type === 'EQUAL') return this.eqExpression();
if (operator.type === 'REPEAT_FUNCTION') return this.repeatExpression(operator);
const args = [];
while (this.peek().type !== 'RIGHT_PAREN') {
args.push(this.parseExpression());
}
this.advance();
return {
type: 'CallExpression',
operator: operator,
arguments: args
};
}
throw new Error(`Nieoczekiwany token: ${token.type}`);
}
Number() {
const token = this.advance();
return {
type: 'Literal',
value: token.val
};
}
outExpression() {
const expression = this.parseExpression();
this.consume('RIGHT_PAREN', "Oczekiwano ')' po out");
return { type: 'PrintStatement', expression };
}
inExpression() {
this.consume('RIGHT_PAREN', "Oczekiwano ')' po in");
return { type: 'InputStatement' };
}
minusExpression() {
const left = this.parseExpression();
const right = this.parseExpression();
this.consume('RIGHT_PAREN', "Oczekiwano ')' po -");
return { type: 'BinaryExpression', operator: '-', left, right };
}
plusExpression() {
const left = this.parseExpression();
const right = this.parseExpression();
this.consume('RIGHT_PAREN', "Oczekiwano ')' po operacji +");
return { type: 'BinaryExpression', operator: '+', left, right };
}
eqExpression() {
const target = this.parseExpression();
const value = this.parseExpression();
this.consume('RIGHT_PAREN', "Oczekiwano ')' po operacji =");
return { type: 'AssignmentStatement', target, value };
}
repeatExpression(operatorToken) {
const body = [];
while (this.peek().type !== 'RIGHT_PAREN') {
body.push(this.parseExpression());
}
this.consume('RIGHT_PAREN', "Oczekiwano ')' po bloku repeat");
return {
type: 'RepeatStatement',
condition: operatorToken.condition,
body
};
}
consume(type, message) {
if (this.peek().type === type) {
return this.advance();
}
throw new Error(message + ". Znaleziono: " + this.peek().type);
}
}
module.exports = Parser;