-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallax.l
173 lines (156 loc) · 4.96 KB
/
parallax.l
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
%{
#include "parallax.h"
#include "parallax.tab.h"
const char* token_name(int current_token) {
static char buffer[100];
switch (current_token) {
case IDENTIFIER:
snprintf(buffer, sizeof(buffer), "IDENTIFIER: %s", yytext);
break;
case NUMBER:
snprintf(buffer, sizeof(buffer), "NUMBER: %s", yytext);
break;
case STRING:
snprintf(buffer, sizeof(buffer), "STRING: %s", yytext);
break;
case TYPE:
snprintf(buffer, sizeof(buffer), "TYPE: %s", yytext);
break;
case KEYWORD:
snprintf(buffer, sizeof(buffer), "KEYWORD: %s", yytext);
break;
case BOOL:
snprintf(buffer, sizeof(buffer), "BOOL: %s", yytext);
break;
case PLUS:
snprintf(buffer, sizeof(buffer), "PLUS: +");
break;
case MINUS:
snprintf(buffer, sizeof(buffer), "MINUS: -");
break;
case MULT:
snprintf(buffer, sizeof(buffer), "MULT: *");
break;
case DIV:
snprintf(buffer, sizeof(buffer), "DIV: /");
break;
case MOD:
snprintf(buffer, sizeof(buffer), "MOD: %%");
break;
case AND_OP:
snprintf(buffer, sizeof(buffer), "AND_OP: &&");
break;
case OR_OP:
snprintf(buffer, sizeof(buffer), "OR_OP: ||");
break;
case EQ_OP:
snprintf(buffer, sizeof(buffer), "EQ_OP: ==");
break;
case NE_OP:
snprintf(buffer, sizeof(buffer), "NE_OP: !=");
break;
case LT_OP:
snprintf(buffer, sizeof(buffer), "LT_OP: <");
break;
case GT_OP:
snprintf(buffer, sizeof(buffer), "GT_OP: >");
break;
case LE_OP:
snprintf(buffer, sizeof(buffer), "LE_OP: <=");
break;
case GE_OP:
snprintf(buffer, sizeof(buffer), "GE_OP: >=");
break;
case ASSIGN:
snprintf(buffer, sizeof(buffer), "ASSIGN: =");
break;
case ADD_ASSIGN:
snprintf(buffer, sizeof(buffer), "ADD_ASSIGN: +=");
break;
case SUB_ASSIGN:
snprintf(buffer, sizeof(buffer), "SUB_ASSIGN: -=");
break;
case MUL_ASSIGN:
snprintf(buffer, sizeof(buffer), "MULT_ASSIGN: *=");
break;
case DIV_ASSIGN:
snprintf(buffer, sizeof(buffer), "DIV_ASSIGN: /=");
break;
case SEMICOLON:
snprintf(buffer, sizeof(buffer), "SEMICOLON: ;");
break;
case COMMA:
snprintf(buffer, sizeof(buffer), "COMMA: ,");
break;
case LBRACE:
snprintf(buffer, sizeof(buffer), "LBRACE: {");
break;
case RBRACE:
snprintf(buffer, sizeof(buffer), "RBRACE: }");
break;
case LPAREN:
snprintf(buffer, sizeof(buffer), "LPAREN: (");
break;
case RPAREN:
snprintf(buffer, sizeof(buffer), "RPAREN: )");
break;
default:
snprintf(buffer, sizeof(buffer), "Unknown token: %d", current_token);
}
return buffer;
}
%}
%%
"void" { return TYPE; }
"u8" { return TYPE; }
"u16" { return TYPE; }
"u32" { return TYPE; }
"u64" { return TYPE; }
"i8" { return TYPE; }
"i16" { return TYPE; }
"i32" { return TYPE; }
"i64" { return TYPE; }
"string" { return TYPE; }
"fn" { return KEYWORD; }
"return" { return KEYWORD; }
"if" { return KEYWORD; }
"else" { return KEYWORD; }
"while" { return KEYWORD; }
"for" { return KEYWORD; }
"break" { return KEYWORD; }
"continue" { return KEYWORD; }
"true" { return BOOL; }
"false" { return BOOL; }
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULT; }
"/" { return DIV; }
"%" { return MOD; }
"&&" { return AND_OP; }
"||" { return OR_OP; }
"!" { return NOT_OP; }
"==" { return EQ_OP; }
"!=" { return NE_OP; }
"<=" { return LE_OP; }
">=" { return GE_OP; }
"<" { return LT_OP; }
">" { return GT_OP; }
"=" { return ASSIGN; }
"+=" { return ADD_ASSIGN; }
"-=" { return SUB_ASSIGN; }
"*=" { return MUL_ASSIGN; }
"/=" { return DIV_ASSIGN; }
"%=" { return MOD_ASSIGN; }
";" { return SEMICOLON; }
"," { return COMMA; }
"{" { return LBRACE; }
"}" { return RBRACE; }
"(" { return LPAREN; }
")" { return RPAREN; }
[0-9]+ { yylval.num = atoi(yytext); return NUMBER; }
\"([^\\"]|\\.)*\" { yylval.str = strdup(yytext); return STRING; }
[a-zA-Z_][a-zA-Z0-9_]* { return IDENTIFIER; }
[ \t\r\n]+ { /* Ignore whitespace and newlines */ }
. { printf("Unrecognized character: %s\n", yytext); }
%%
int yywrap() { return 1; }