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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "types.h"
#define size(arr) sizeof(arr) / sizeof(arr[0])
char *keywords[] = {
"int",
"char",
"return",
"for",
"while",
"if",
"else",
"elif"
};
char *saveptr = NULL;
int lexchar(lexobj *l, char *line, char c) {
if (line[0] == c) {
l->data = NULL;
l->t = c;
saveptr = line + 1;
return 1;
}
return 0;
}
int lexchars(lexobj *l, char *line, char *cs) {
for (int i = 0; i < strlen(cs); i++) {
if (lexchar(l, line, cs[i])) return 1;
}
return 0;
}
lexobj lex(char *line) {
removepadding:
if (line[0] == ' ') line++;
if (line[0] == ' ') goto removepadding;
static lexobj l;
if (isdigit(line[0])) {
static char num[256];
int ptr = 0;
while (isdigit(line[0])) {
num[ptr] = line[0];
ptr++;
line++;
}
num[ptr] = 0;
saveptr = line;
l.data = num;
l.t = INTLIT;
return l;
}
if (lexchars(&l, line, "(){};+-*/")) return l;
for (int i = 0; i < size(keywords); i++) {
if (strstr(line, keywords[i]) == line) {
line += strlen(keywords[i]);
saveptr = line;
if (line[0] != ' ') goto retry;
l.data = keywords[i];
l.t = KEYWORD;
return l;
}
retry:
continue;
}
char *data = strchr(line, '(');
*data = 0;
l.data = strdup(line);
line += data - line;
*data = '(';
saveptr = line;
return l;
}
void stripwhitespace(char *line) {
char *out = malloc(256);
int ptr = 0;
for (int i = 0; i < strlen(line); i++) {
if (line[i] == '\t' || line[i] == '\n'
|| line[i] == '\v') continue;
else {
out[ptr] = line[i];
ptr++;
}
}
out[ptr] = 0;
memcpy(line, out, 256);
}
|