-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.js
executable file
·127 lines (124 loc) · 3.46 KB
/
Lexer.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
import { Assembler } from './Assembler.js';
import { Errorcalm } from './Errorcalm.js';
import { SyntaxicAnalysis } from './SyntaxicAnalysis.js';
export class Lexer {
static Errors = [];
static isValidString(str) {
// Check if the string contains any special characters
if (/[^a-zA-Z0-9_]/.test(str)) {
return false;
}
// Check if the string begins with a number
if (/^\d/.test(str)) {
return false;
}
// Check if the string is in the excluded list
if (Assembler.excludedStrings.includes(str)) {
return false;
}
// If none of the above conditions are met, the string is valid
return true;
}
constructor(code,line) {
//console.log(code.match(/([a-zA-Z0-9]+\d*(?:[a-zA-Z09]+)?|\*|,|\+)/g))
this.LexicalList = code.match(/([a-zA-Z0-9]+\d*(?:[a-zA-Z0-9]+)?|\*|,|\+,,|\~|,|\`|,|\!|,|\@|,|\#|,|\$|,|\%|,|\^|,|\&|,|\*|,|\(|,|\)|,|\-|,|\_|,|\=|,|\+|,|\[|,|\]|,|\{|,|\}|,|\;|,|\:|,|\'|,|\"|,|\,|,|\.|,|\<|,|\>|,|\?|,|\\|)/g).filter(function (t) {
return t.length > 0;
}).map(function (t) {
if (isNaN(t)) {
switch (t) {
case 'R1':
case 'R2':
case 'R3':
case 'R4':
case 'ACC':
case 'BR':
case 'IR':
case 'SR':
case 'R1R':
case 'R2R':
case 'R3R':
case 'ACCR':
case 'R1L':
case 'R2L':
case 'R3L':
case 'ACCL':
return {
type: 'REGISTER',
value: t
};
case 'RET':
case 'PUSHA':
case 'POPA':
return{
type: 'INST0',
value: t
};
case 'NEG':
case 'NOT':
case 'SHL':
case 'SHR':
case 'READ':
case 'WRITE':
case 'PUSH':
case 'POP':
case 'ROR':
case 'ROL':
case 'CALL':
case 'BE':
case 'BNE':
case 'BS':
case 'BI':
case 'BIE':
case 'BSE':
case 'BR':
return {
type: 'INST1',
value: t
};
case 'NAND':
case 'CMP':
case 'MOV':
case 'ADD':
case 'SUB':
case 'MUL':
case 'DIV':
case 'AND':
case 'OR':
case 'XOR':
case 'NOR':
return{
type: 'INST2',
value: t
};
case '*':
case ',':
case '+':
return {
type: 'SPECIAL CHARACTER',
value: t
};
case 'LABEL':
return {
type: 'LABEL'
};
break;
default:
if (Lexer.isValidString(t)) {
return {
type: 'TEXT',
value: t
};
}else{
Lexer.Errors.push(new Errorcalm("Invalid string", "LEXER", line)) ; //change this 0 to the line number
Errorcalm.set_LexicalError(new Errorcalm("Invalid string", "LEXER", line));
}
}
} else {
return {
type: 'NUMBER',
value: t
};
}
});
}
}