-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.c
More file actions
executable file
·99 lines (84 loc) · 2 KB
/
Copy pathlexer.c
File metadata and controls
executable file
·99 lines (84 loc) · 2 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
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
#include "util.h"
int lexer_lineNr;
struct {
const char *keyword;
int token;
} keywords[] = {
{ "define", DEFINE },
{ "func", FUNC },
{ "block", BLOCK },
{ "begin", BEGIN },
{ "match", MATCH },
{ "switch", SWITCH },
{ "branch", BRANCH },
{ "case", CASE },
{ "else", ELSE },
{ "set", SET },
{ "return", RETURN },
{ "goto", GOTO },
};
void lexer_init(void)
{
lexer_lineNr = 1;
}
int yylex(void)
{
int c;
char *buf;
int i;
int isNumber;
buf = yylval.text;
for (;;) {
do {
c = fgetc(stdin);
if (c == '\n')
lexer_lineNr++;
} while (c == ' ' || c == '\n');
if (c != '#')
break;
do c = fgetc(stdin); while (c != '\n');
lexer_lineNr++;
}
if (c == EOF)
return EOF;
if (c == '(' || c == ')')
return c;
if (c == '"') {
for (i = 0; ; buf[i++] = c) {
if (i == sizeof(yylval.text))
die("String is too large.");
c = fgetc(stdin);
if (c == '"')
break;
if (c == EOF)
die("Incomplete input.");
}
buf[i] = '\0';
yylval.syntax = runtime_makeString(buf);
return STRING;
}
if (!isalnum(c))
die("Bad token.");
isNumber = isdigit(c);
i = 0;
do {
buf[i++] = (char)c;
if (i == sizeof(yylval.text))
die("Token is too large.");
c = fgetc(stdin);
if (isNumber && isalpha(c))
die("Bad token.");
} while(isalnum(c));
buf[i] = '\0';
if (ungetc(c, stdin) == EOF)
die("File stream error.");
if (isNumber) {
yylval.syntax = runtime_makeNumber(atol(buf));
return NUMBER;
}
for (i = 0; i < ARRAY_SIZE(keywords); i++)
if (!strcmp(keywords[i].keyword, buf))
return keywords[i].token;
yylval.syntax = runtime_makeTuple1(CLASS_Id, runtime_makeString(buf));
return ID;
}