-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.c
229 lines (224 loc) · 6.41 KB
/
lexer.c
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
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
#include "lexer.h"
#include "helpers.h"
//char numbers[] = {'0','1','2','3','4','5','6','7','8','9'};
void lex(char *sCode,struct token *rootToken){
struct token *cToken = rootToken; // current token
int n = 0;
while (cToken->next != NULL){
cToken = cToken->next;
n++;
}
//struct token nxt_token = {.type=NULL,.value=NULL,.next=NULL};
//cToken->next = &nxt_token;
//cToken = cToken->next;
size_t sz = strlen(sCode);
int tokenReturned = 0;
for (int x=0;x<sz;x++){
tokenReturned = 1;
struct token *nxt_token = malloc(sizeof(struct token));
nxt_token->type = 0;
nxt_token->value = NULL;
nxt_token->next = NULL;
if ((!(sCode[x] == ' ') && !isspace(sCode[x])) || sCode[x] == 39 || sCode[x] == '"'){
bool inApostrophe = sCode[x] == 39 || sCode[x] == '"';
char *str;
char *value = NULL;
char *type;
if (isalpha(sCode[x]) || inApostrophe){
int eI;
if (inApostrophe){
eI = stringLaH(sCode,x,sz,0,true); // ending index of string
} else {
eI = stringLaH(sCode,x,sz,0,false); // ending index of string
}
value = malloc(sizeof(char)*(eI-x));
nxt_token->value = value;
int error = sliceString(sCode,x,eI,sz,value);
if (error == -1){
printf("Error: Invalid variable name\n");
break;
}
if (sCode[eI] == '('){
nxt_token->type = FUNCTION_CALL;
nxt_token->value = value;
} else if (strcmp(value,"function") == 0){
nxt_token->type = FUNCTION;
nxt_token->value = value;
} else if (
strcmp(value,"if") == 0 ||
strcmp(value,"elif") == 0 ||
strcmp(value,"else") == 0
){
nxt_token->type = CONDITIONAL;
} else if (inApostrophe){
nxt_token->type = STRING;
} else if (strcmp(value,"end") == 0){
nxt_token->type = END;
} else if (
strcmp(value,"or") == 0 ||
strcmp(value,"and") == 0
){
nxt_token->type = OPERATOR;
} else if (strcmp(value,"var") == 0){
nxt_token->type = DECLARATION;
} else if (strcmp(value,"while") == 0){
nxt_token->type = WHILE;
} else if (strcmp(value,"int") == 0 ||
strcmp(value,"float") == 0 ||
strcmp(value,"string") == 0){
nxt_token->type = TYPE;
nxt_token->value = value;
} else if (strcmp(value,"return") == 0){
nxt_token->type = RETURN;
nxt_token->value = value;
} else {
nxt_token->type = SYMBOL;
}
x = eI - 1; // idk why it doesn't work without -1 :'(
} else if (sCode[x] == ';' || sCode[x] == ':'){
char *val = malloc(sizeof(char)*2);
val[0] = sCode[x];
val[1] = '\0';
if (sCode[x] == ';'){
nxt_token->type = SEMICOLON;
} else {
nxt_token->type = COLON;
}
nxt_token->value = val;
} else if (
(sCode[x] == '>' && sCode[x+1] == '=') ||
(sCode[x] == '<' && sCode[x+1] == '=') ||
(sCode[x] == '=' && sCode[x+1] == '=') ||
(sCode[x] == '/' && sCode[x+1] == '/')
){
value = malloc(sizeof(char)*3);
value[0] = sCode[x];
value[1] = sCode[x+1];
value[2] = '\0';
if (sCode[x] == '/' && sCode[x+1] == '/'){
nxt_token->type = COMMENT;
while (sCode[x+2] != '\n'){ // Ignore everything after '//'
x++;
};
} else {
nxt_token->type = OPERATOR;
}
nxt_token->value = value;
x++;
} else if (sCode[x] == '=' || sCode[x] == '(' || sCode[x] == ')' || sCode[x] == '+' || sCode[x] == '*' ||
sCode[x] == '-' || sCode[x] == '/' || sCode[x] == '<' || sCode[x] == '>' || sCode[x] == ','){
if ((sCode[x] == '-' || sCode[x] == '+' || sCode[x] == '/' || sCode[x] == '*') && sCode[x+1] == '='){
value = malloc(sizeof(char)*3);
value[0] = sCode[x];
value[1] = sCode[x+1];
value[2] = '\0';
nxt_token->type = ASSIGNMENT_OPERATOR;
nxt_token->value = value;
x++;
} else {
value = malloc(sizeof(char)*2);
value[0] = sCode[x];
value[1] = '\0';
if (sCode[x] == '='){
nxt_token->type = ASSIGNMENT;
} else if (sCode[x] == '+' || sCode[x] == '-' || sCode[x] == '/' ||
sCode[x] == '<' || sCode[x] == '>' || sCode[x] == '*'){
nxt_token->type = OPERATOR;
} else if (sCode[x] == '(' || sCode[x] == ')'){
nxt_token->type = PARENTHESIS;
} else if (sCode[x] == ','){
nxt_token->type = COMMA;
}
nxt_token->value = value;
}
} else if (isdigit(sCode[x])) {
int isFloat = 0;
int eI = numberLaH(sCode,x,sz,&isFloat);
if (eI == 500){ // 500 means that number is followed by letters which result in bad variable name error
printf("Error: Invalid variable name\n");
break;
}
str = malloc(sizeof(char)*(eI-x));
sliceString(sCode,x,eI,sz,str);
nxt_token->value = str;
type = malloc(sizeof(int));
if (isFloat){
nxt_token->type = FLOAT_;
} else {
nxt_token->type = INTEGER;
}
x = eI - 1; // idk why it doesn't work without -1 :'(
} else {
printf("Syntax error --> %c\n",sCode[x]);
tokenReturned = 0;
}
if (tokenReturned){
cToken->next = nxt_token;
cToken = cToken->next;
n++;
}
}
}
}
int numberLaH(char string[],int sI,size_t sz,int *isFloat){ // number lookahead
// sI is starting index
int eI = sI; // eI is ending index ( the index where the last number is)
for (int x=sI;x<sz;x++){
if (string[x] == '.' && isdigit(string[x+1])){
*isFloat = 1;
} else if (isalpha(string[x])){
return 500;
}
if (string[x] < 48 || string[x] > 57 || string[x] == ';') {
if (string[x] != '.'){
break;
}
}
eI++;
}
return eI;
}
int stringLaH(char string[],int sI,size_t sz,int type,bool inApostrophe){
// type 0 = symbol
// type 1 = number
int eI = sI; // eI is ending index ( the index where the last char is)
char oApostrophe; // opening apostrophe
if (type == 0){
if (!(isalpha(string[0]))){
if (!(string[0] == 39 || string[0] == '"')){
return -1;
}
}
}
if (inApostrophe){
oApostrophe = string[sI];
}
for (int x=sI;x<sz;x++){
if (inApostrophe && string[x+1] == oApostrophe){ // 39 is ascii for signle quote
eI += 2;
break;
} else if (!inApostrophe) {
if (isspace(string[x]) ||
string[x] == '=' ||
string[x] == ')' ||
string[x] == '(' ||
string[x] == ',' ||
string[x] == '+' ||
string[x] == '>' ||
string[x] == '<' ||
string[x] == '-' ||
string[x] == '/' ||
string[x] == ':' ||
string[x] == ';'){
break;
}
}
eI++;
}
return eI;
}