-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.js
More file actions
147 lines (135 loc) · 3.87 KB
/
parser.js
File metadata and controls
147 lines (135 loc) · 3.87 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
142
143
144
145
146
147
let tokenize = (code) => {
let tokens = [],
current = 0
// pobranie jednego tokena
let getToken = function () {
let letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'],
digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'],
token = {
type: '',
value: '',
pos: 0
}
while (token.type == '' && current < code.length) {
token.pos = current
let c = code[current]; current++;
token.value = c
if (letters.indexOf(c) !== -1) {
token.type = 'ident';
while (letters.indexOf(code[current]) !== -1) {
c = code[current]; current++
token.value += c
}
}else if (digits.indexOf(c) !== -1) {
token.type = 'number';
while (digits.indexOf(code[current]) !== -1) {
c = code[current]; current++
token.value += c
}
token.value = parseInt(token.value)
} else if (c === '+' || c === '-' || c === '*' || c === '/' || c === '=') {
token.type = 'operator'
} else if (c === ' ' || c === '\t' || c === '\r') {
while (code[current] === ' ' || code[current] === '\t') {
c = code[current]; current++
token.value += c
}
} else if (c === '(') {
token.type = 'lparensis'
token.value = c
} else if (c === ')') {
token.type = 'rparensis'
token.value = c
} else if (c === '\n') {
token.type = 'nl'
// usuwam powtorzenia
while (code[current] === '\n') {
c = code[current]; current++
}
} else {
throw 'Tokenizer: Nieprawidlowy token ' + c + ' na pozycji ' + current
}
}
return token.type ? token : null;
}
let currentToken;
while (currentToken = getToken()) {
tokens.push(currentToken);
}
return tokens
}
let parse = (tokens) => {
let current = 0
let factor = function () {
let token = tokens[current++];
if (token.type === 'number' || token.type === 'ident') {
return token
} else if (token.type === 'lparensis') {
let result = expression()
if (tokens[current].type === 'rparensis') {
current++
return result
} else {
throw "Spodziewany ')' na pozycji " + current
}
} else {
throw 'Spodziewany number lub zmienna lub ( na pozycji ' + token.pos
}
}
let term = function () {
let result = factor();
let op = tokens[current]; // uwaga nie zwiekszamy jeszcze
while (op && op.type === 'operator' && (op.value === '*' || op.value === '/')) {
current++
op.left = result
op.right = factor()
result = op
op = tokens[current]
}
return result
}
let expression = function() {
let result = term()
let op = tokens[current]; // uwaga nie zwiekszamy jeszcze
while (op && op.type === 'operator' && (op.value === '+' || op.value === '-')) {
current++
op.left = result
op.right = term()
result = op
op = tokens[current]
}
return result
}
let assign = function () {
let result = undefined
let variable = tokens[current++]
if (variable.value === 'mut') {
variable = tokens[current++]
variable.mutable = true
}
let op = tokens[current]
if (op && op.type === 'operator' && op.value === '=') {
current++
op.left = variable
op.right = expression()
result = op
}
return result;
}
let instruction = function() {
// TODO zabezpieczyc przed 1 = 3 + 3 -> wpada w expression i zatrzymuje sie na '='
return tokens[current].type === 'ident' ? assign() : expression();
}
let instructions = function () {
let result = [instruction()]
let nextist = tokens[current] // TODO fix nl and inc nextinst
while (nextist && nextist.type === 'nl' && tokens[current + 1]) {
current++
result.push (instruction())
}
console.dir(result)
window.instructions = result;
return result;
}
return instructions()
}