-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblang.l
195 lines (152 loc) · 5.4 KB
/
blang.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
%{
#include <string>
#include <iostream>
#include "SizeType.h"
#include "astnode.h"
#include "blang.tab.hpp"
using namespace std;
#define YY_DECL extern "C" int yylex()
#define SAVE_STRING yylval.str = new string(yytext, yyleng);
#define TOKEN(t) t
Blang::SizeType numToST(const char* tok);
char* stripChar(const char* tok);
char* stripStr(const char* tok );
unsigned int _line = 0;
%}
%option stack
%x IN_COMMENT
%%
[ \t] ;
"if" { return TOKEN(IF); }
"else" { return TOKEN(ELSE); }
"while" { return TOKEN(WHILE); }
"switch" { return TOKEN(SWITCH); }
"case" { return TOKEN(CASE); }
"break" { return TOKEN(BREAK); }
"default" { return TOKEN(DEFAULT); }
"auto" { return TOKEN(AUTO); }
"extrn" { return TOKEN(EXTRN); }
"goto" { return TOKEN(GOTO); }
"return" { return TOKEN(RETURN); }
[a-zA-Z_][a-zA-Z0-9_.]* { SAVE_STRING; return IDENT; }
[0-9]+ { yylval.word = numToST(yytext);
return WORD; }
\"(\\.|[^\\"])*\" { SAVE_STRING; puts(yylval.str->c_str()); return STRING; }
\'(\\.|[^\\']){1,8}\' { SAVE_STRING; printf("CHAR: %s\n", yylval.str->c_str()); return CHAR; }
"=|" { return TOKEN(ASN_OR); }
"=&" { return TOKEN(ASN_AND); }
"=^" { return TOKEN(ASN_XOR); }
"===" { return TOKEN(ASN_EQ); }
"=!=" { return TOKEN(ASN_NQ); }
"=<" { return TOKEN(ASN_LT); }
"=<=" { return TOKEN(ASN_LTE); }
"=>" { return TOKEN(ASN_GT); }
"=>=" { return TOKEN(ASN_GTE); }
"=<<" { return TOKEN(ASN_LSH); }
"=>>" { return TOKEN(ASN_RSH); }
"=+" { return TOKEN(ASN_ADD); }
"=-" { return TOKEN(ASN_SUB); }
"=*" { return TOKEN(ASN_MUL); }
"=/" { return TOKEN(ASN_DIV); }
"=%" { return TOKEN(ASN_MOD); }
"++" { return TOKEN(UN_INC); }
"--" { return TOKEN(UN_DEC); }
">=" { return TOKEN(LOG_GTE); }
"<<" { return TOKEN(BIT_LSH); }
">>" { return TOKEN(BIT_RSH); }
"<=" { return TOKEN(LOG_LTE); }
"==" { return TOKEN(LOG_EQ); }
"!=" { return TOKEN(LOG_NQ); }
"=" { return TOKEN('='); }
"-" { return TOKEN('-'); }
"!" { return TOKEN('!'); }
"|" { return TOKEN('|'); }
"&" { return TOKEN('&'); }
"^" { return TOKEN('^'); }
"~" { return TOKEN('~'); }
"<" { return TOKEN('<'); }
">" { return TOKEN('>'); }
"+" { return TOKEN('+'); }
"%" { return TOKEN('%'); }
"*" { return TOKEN('*'); }
"/" { return TOKEN('/'); }
"?" { return TOKEN('?'); }
":" { return TOKEN(':'); }
"{" { return TOKEN('{'); }
"}" { return TOKEN('}'); }
"[" { return TOKEN('['); }
"]" { return TOKEN(']'); }
"(" { return TOKEN('('); }
")" { return TOKEN(')'); }
";" { return TOKEN(';'); }
"," { return TOKEN(','); }
\n { _line++; }
"/*" { yy_push_state(IN_COMMENT); }
<IN_COMMENT>
{
[^/*\n]+ ;// eat comment in chunks
"/*" { yy_push_state(IN_COMMENT); }
"*/" { yy_pop_state(); }
[*/] ;
\n {_line++;}
}
%%
Blang::SizeType
numToST(const char* tok)
{
char* p;
Blang::SizeType out;
int base = tok[0] == '0' ? 8 : 10;
#ifdef BLANG_BUILD_64BIT
out = strtoll(tok, &p, base);
#else
out = strtol(tok, &p, base);
#endif
return out;
}
char*
stripChar(const char* tok)
{
char* out;
int i = 1;
out = (char*)malloc(strlen(tok) - 1);
for(; i < strlen(tok)-1; i++)
{
switch(tok[i])
{
case '*':
i++;
switch(tok[i])
{
case 'n':
out[i-2] = '\n';
break;
case 'e':
out[i-2] = -1;
break;
case '0':
out[i-2] = 0;
break;
case '{':
case '}':
case '\'':
case '"':
case '*':
out[i-2] = tok[i];
break;
}
break;
case '\'': //ignore closing '
continue;
default: out[i-1] = tok[i];
break;
}
}
out[i] = -1;
return out;
}
char*
stripStr(const char* tok)
{
return NULL;
}