-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathlex.c
More file actions
59 lines (56 loc) · 2.13 KB
/
lex.c
File metadata and controls
59 lines (56 loc) · 2.13 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
#include "rubi.h"
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int32_t lex(char *code)
{
int32_t codeSize = strlen(code), line = 1;
int is_crlf = 0;
for (int32_t i = 0; i < codeSize; i++) {
if (tok.size <= i)
tok.tok = realloc(tok.tok, (tok.size += 512 * sizeof(Token)));
if (isdigit(code[i])) { // number?
for (; isdigit(code[i]); i++)
strncat(tok.tok[tok.pos].val, &(code[i]), 1);
tok.tok[tok.pos].nline = line;
i--;
tok.pos++;
} else if (isalpha(code[i])) { // ident?
char *str = tok.tok[tok.pos].val;
for (; isalpha(code[i]) || isdigit(code[i]) || code[i] == '_'; i++)
*str++ = code[i];
tok.tok[tok.pos].nline = line;
i--;
tok.pos++;
} else if (code[i] == ' ' || code[i] == '\t') { // space or tab?
} else if (code[i] == '#') { // comment?
for (i++; code[i] != '\n'; i++) { } line++;
} else if (code[i] == '"') { // string?
strcpy(tok.tok[tok.pos].val, "\"");
tok.tok[tok.pos++].nline = line;
for (i++; code[i] != '"' && code[i] != '\0'; i++)
strncat(tok.tok[tok.pos].val, &(code[i]), 1);
tok.tok[tok.pos].nline = line;
if (code[i] == '\0')
error("%d: expected expression '\"'",
tok.tok[tok.pos].nline);
tok.pos++;
} else if (code[i] == '\n' ||
(is_crlf = (code[i] == '\r' && code[i + 1] == '\n'))) {
i += is_crlf;
strcpy(tok.tok[tok.pos].val, ";");
tok.tok[tok.pos].nline = line++;
tok.pos++;
} else {
strncat(tok.tok[tok.pos].val, &(code[i]), 1);
if (code[i + 1] == '=' || (code[i] == '+' && code[i + 1] == '+') ||
(code[i] == '-' && code[i + 1] == '-'))
strncat(tok.tok[tok.pos].val, &(code[++i]), 1);
tok.tok[tok.pos].nline = line;
tok.pos++;
}
}
tok.tok[tok.pos].nline = line;
tok.size = tok.pos - 1;
return 0;
}