-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntaxicAnalysis.js
349 lines (250 loc) · 18.3 KB
/
SyntaxicAnalysis.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import { Lexer } from './Lexer.js';
import { Errorcalm } from './Errorcalm.js';
import { Assembler,FuncInterface } from "./Assembler.js";
export class SyntaxicAnalysis {
Syntaxiclist = []
constructor(input) {
let lexicalList = input;
for(let i = 0; i < lexicalList.length; i++){
// here operation with each line of code
// we must check if it is a label or an instruction
// if it is a label we have to check that the next element is a number and it has to be < from a number we fix
let firstword = lexicalList[i][0]
let firstwordtype = firstword.type
switch (firstwordtype) {
case 'LABEL':
const functLABEL = ()=> {
if (lexicalList[i].length == 3) {
if (lexicalList[i][2].type == 'NUMBER') {
if( lexicalList[i][2].value < Assembler.MAXNUM){
if(lexicalList[i][1].type == 'TEXT'){
if(Lexer.isValidString(lexicalList[i][1].value)){
// filters for text standards and validity of the text
// check if label already existing
var found = false ;
var labelname = firstword.value ;
Assembler.Labellist.forEach(element => {
if(element.name === labelname){
found = true
}
});
if (!found) {
//this.Syntaxiclist.push(lexicalList[i]);
//stop pushing here because we don't need it
Assembler.Labellist.push({ name: lexicalList[i][1].value, address: lexicalList[i][2].value, linedeclared:i })
}else{
this.Syntaxiclist.push(new Errorcalm("LABEL already declared",null,i))
}
}else{ this.Syntaxiclist.push(new Errorcalm("LABEL name is not valid",null,i)) }
}else{
this.Syntaxiclist.push(new Errorcalm("LABEL name not defined",null,i))
}
}else{
this.Syntaxiclist.push(new Errorcalm("Number size is bigger then MAXNUM",null,i))
}}else{
this.Syntaxiclist.push(new Errorcalm("LABEL must be a number",null,i))
}
}else{
if (Lexer.isValidString(lexicalList[i][2].value )) {
this.Syntaxiclist.push(new Errorcalm("LABEL must have only two operands",null,i))
}else
{
this.Syntaxiclist.push(new Errorcalm("LABEL name is not valid",null,i))
}
}
}
functLABEL();
break;
case 'INST0':
// No params instructions: INST0 ::= RET, PUSHA, POPA
// We must have no op after it
const functINST0 = ()=> {
if (lexicalList[i].length == 1) {
this.Syntaxiclist.push(lexicalList[i]);
}else{
this.Syntaxiclist.push(new Errorcalm("INST0 must have no operands",null,i))
}}
functINST0();
break ;
case 'INST1':
// ONE params instructions: INST1 ::= NEG, NOT, SHL, SHR, READ, WRITE, PUSH, POP, ROR, ROL, CALL, BE, BNE, BS, BI, BIE, BSE, BR
//| |
//| Must have only one other param: it must be valid |
//| or one param and other special chars: they must be valid also |
//| That other special char is used for addressing modes mainly |
//|------------------------------------------------------------------------------------------
const functINST1 = ()=> {
var firstparam = lexicalList[i][1]
if (lexicalList[i][0].value == 'WRITE' || lexicalList[i][0].value == 'READ') {
//read or write from or to register only
// Labels
}else{
// use it as function
// funcnum(lexicalList[i],i)
// add in the body firstparam definition
// var firstparam = lexicalList[i][1]
switch(firstparam.type){
case 'NUMBER' :
//check addressing
if (firstparam.value < Assembler.MAXNUM) {
switch (lexicalList[i].length) {
case 2:
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value: lexicalList[i][0].value, adrmode:0 },lexicalList[i][1]]);
break;
case 3:
// direct
// correct here add the operand type and value then put the adr mode with the instruction
if (lexicalList[i][2].value == '*') {
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value: lexicalList[i][0].value, adrmode:1 },lexicalList[i][1]]);
}else{
this.Syntaxiclist.push(new Errorcalm("Wrong character",null,i))
}
break;
case 4:
// indirect
if (lexicalList[i][2].value == '*' && lexicalList[i][3].value == '*') {
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value: lexicalList[i][0].value, adrmode:2 },lexicalList[i][1]]);
}else{
this.Syntaxiclist.push(new Errorcalm("Wrong character",null,i))
}
break;
default:
this.Syntaxiclist.push(new Errorcalm("Wrong number of operands",null,i))
break;
} }else{
this.Syntaxiclist.push(new Errorcalm("Number size is bigger then MAXNUM",null,i))
}
break;
case 'REGISTER' :
// define addressing mode
// or deplacement
//console.log("lexicalList[i][1].lenght ",lexicalList[i].length)
switch (lexicalList[i].length) {
case 2:
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value: lexicalList[i][0].value, adrmode:0 },lexicalList[i][1]]);
break;
case 3:
// direct
// correct here add the operand type and value then put the adr mode with the instruction
if (lexicalList[i][2].value == '*') {
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value: lexicalList[i][0].value, adrmode:1 },lexicalList[i][1]]);
}else{
this.Syntaxiclist.push(new Errorcalm("Wrong character",null,i))
}
break;
case 4:
// indirect
if (lexicalList[i][2].value == '*' && lexicalList[i][3].value == '*') {
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value: lexicalList[i][0].value, adrmode:2 },lexicalList[i][1]]);
}else{
this.Syntaxiclist.push(new Errorcalm("Wrong character",null,i))
}
break;
case 5:
//deplacement
if (lexicalList[i][2].value === '*' && lexicalList[i][3].value === '+' ) {
switch (lexicalList[i][4].type)
{
case 'NUMBER':
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value: lexicalList[i][0].value, adrmode:3 },lexicalList[i][1],lexicalList[i][4]]);
break;
case 'TEXT':
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value: lexicalList[i][0].value, adrmode:3 },lexicalList[i][1],FuncInterface.Label_To_Num(lexicalList[i][4].value,i)]);
break;
}}
break;
default:
this.Syntaxiclist.push(new Errorcalm("Wrong number of operands",null,i))
break;
}
break;
case 'TEXT' :
//+ ajouter opp avec labels, I guess DONE
// Do the needed operations after transformations and ADD TESTs it's not safe here !
// add addressing modes direct and indirect for labels
//check if it's present in label list
switch (lexicalList[i].length) {
case 2:
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value:lexicalList[i][0].value, adrmode:0 },{type:FuncInterface.Label_To_Num(firstparam.value,i).type, value:FuncInterface.Label_To_Num(firstparam.value,i).value}]);
break;
case 3:
// direct
if (lexicalList[i][2].value == '*') {
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value:lexicalList[i][0].value, adrmode:1 },{type:FuncInterface.Label_To_Num(firstparam.value,i).type, value:FuncInterface.Label_To_Num(firstparam.value,i).value}]);
}else{
this.Syntaxiclist.push(new Errorcalm("Wrong character",null,i))
}
break;
case 4:
// indirect
if (lexicalList[i][2].value == '*' && lexicalList[i][3].value == '*') {
this.Syntaxiclist.push([{type:lexicalList[i][0].type, value:lexicalList[i][0].value, adrmode:2 },{type:FuncInterface.Label_To_Num(firstparam.value,i).type, value:FuncInterface.Label_To_Num(firstparam.value,i).value}]);
}else{
this.Syntaxiclist.push(new Errorcalm("Wrong character",null,i))
}
break;
default:
this.Syntaxiclist.push(new Errorcalm("Wrong number of operands",null,i))
break;
}
}
}
}
functINST1();
break ;
case 'INST2':
//check if there is a comma if not throw error which is comma missing
var nocomma = true ;
lexicalList[i].forEach(element => {
if (element.value == ',') {
nocomma = false ;
}});
if (nocomma)
{
this.Syntaxiclist.push(new Errorcalm("Comma missing",null,i))
}
else{
var list1,list2 =[];
list1 = FuncInterface.addrmod(lexicalList[i].slice(1),i).list1 ;
//console.log("list1",list1[0].type)
list2 = FuncInterface.addrmod(lexicalList[i].slice(1),i).list2 ;
if(lexicalList[i][1].type=='NUMBER' && lexicalList[i][0].value == 'MOV') {
//console.log("here------------------------")
this.Syntaxiclist.push(new Errorcalm("Number can't be first operand",null,i))
Errorcalm.SyntaxicError.push(new Errorcalm("Number can't be first operand",null,i))
}else{
//console.log("\nlist1",list1,"\nlist2",list2)
//console.log("\nlist1",FuncInterface.defadrmod(list1),"\nlist2",FuncInterface.defadrmod(list2))
this.Syntaxiclist.push([lexicalList[i][0],FuncInterface.defadrmod(list1,i),FuncInterface.defadrmod(list2,i)]);
}}
break ;
default:
//error
break;
}
//console.log(this.Syntaxiclist)
}
}
//estandards for each instruction
// for label we only have to check that the next element is a number and it has to be < from a number we fix
// No params instructions: INST0 ::= RET, PUSHA, POPA
// We must have no op after it
// ONE params instructions: INST1 ::= NEG, NOT, SHL, SHR, READ, WRITE, PUSH, POP, ROR, ROL, CALL, BE, BNE, BS, BI, BIE, BSE, BR
//| |
//| Must have only one other param: it must be valid |
//| or one param and other special chars: they must be valid also |
//| That other special char is used for addressing modes mainly |
//|------------------------------------------------------------------------------------------
// TWO params instructions: NAND, CMP, MOV, ADD, SUB, MUL, DIV, AND, OR, XOR, NOR
// they may be only two operands or two operands with special chars
// check for the problem of first operand is a number
// define addressing mode
// some special errors for special instructions
// Label instformat LABEL num check this num if it is valid
}
/*
const MAXNUM =6000 // max adress for label
var input = ["LABEL imo 145537", "MOV R1, 14", " ADD R1,R2**","PUSHA 55"]
//console.log(new Lexer(input[0]).LexicalList)
var output = new syntaxicAnalysis(["LABEL 145537"])
console.log(output.Syntaxiclist)*/