-
Notifications
You must be signed in to change notification settings - Fork 3
/
Scanner.l
131 lines (111 loc) · 3.76 KB
/
Scanner.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
%option reentrant
%option nounput
%option noinput
%option noyywrap
%option 8bit
%option yylineno
%option bison-bridge bison-locations
%option prefix="prjm_eval_"
%option extra-type="struct prjm_eval_scanner_state *"
%option never-interactive
%top{
#include "CompilerTypes.h"
#include "Compiler.h"
#include <stdio.h>
#define YY_NO_UNISTD_H
#define YY_STDINIT
#define YY_USER_ACTION \
{ \
int old_column = yycolumn; \
yycolumn += strlen(yytext); \
yylloc_param->first_line = yylineno; \
yylloc_param->first_column = old_column; \
yylloc_param->last_line = yylineno; \
yylloc_param->last_column = yycolumn; \
}
struct prjm_eval_scanner_state;
}
/* States */
%x ML_COMMENT
%x IL_COMMENT
/* Rule definitions */
DIGIT [0-9]
HEX [0-9a-fA-F]
SIGN [+-]
EXPO [eE]{SIGN}?{DIGIT}+
FLOAT ({DIGIT}+|{DIGIT}+\.{DIGIT}*|{DIGIT}*\.{DIGIT}+){EXPO}?
NAME [_a-zA-Z][_a-zA-Z0-9]*
%%
/* Comments */
"/*" { BEGIN(ML_COMMENT); }
<ML_COMMENT>"*/" { BEGIN(INITIAL); }
<ML_COMMENT>\n { }
"//" { BEGIN(IL_COMMENT); }
<IL_COMMENT>\n { BEGIN(INITIAL); }
<IL_COMMENT>. { }
/* Constants */
"$"[Xx]{HEX}+ {
char* p = yytext + 2;
yylval->NUM = (PRJM_EVAL_F)strtoul(yytext + 2, &p, 16);
return NUM;
}
"$'"."'" { yylval->NUM = *(char*)(yytext + 2); return NUM; }
"$"(?i:pi) { yylval->NUM = 3.141592653589793f; return NUM; }
"$"[eE] { yylval->NUM = 2.71828183f; return NUM; }
"$"(?i:phi) { yylval->NUM = 1.61803399f; return NUM; }
(?i:gmem) { return GMEM; }
{FLOAT} {
yylval->NUM = atof(yytext);
return NUM;
}
{NAME} {
if (prjm_eval_compiler_name_is_function(cctx, yytext))
{
yylval->FUNC = strdup(yytext);
return FUNC;
}
yylval->VAR = strdup(yytext);
return VAR;
}
/* Operators */
"+=" { return ADDOP; }
"-=" { return SUBOP; }
"%=" { return MODOP; }
"|=" { return OROP; }
"&=" { return ANDOP; }
"/=" { return DIVOP; }
"*=" { return MULOP; }
"^=" { return POWOP; }
"==" { return EQUAL; }
"<=" { return BELEQ; }
">=" { return ABOEQ; }
"!=" { return NOTEQ; }
"||" { return BOOLOR; }
"&&" { return BOOLAND; }
"<" { return '<'; }
">" { return '>'; }
"+" { return '+'; }
"-" { return '-'; }
"*" { return '*'; }
"/" { return '/'; }
"%" { return '%'; }
"^" { return '^'; }
"&" { return '%'; }
"|" { return '^'; }
"!" { return '!'; }
"=" { return '='; }
/* Syntactic elements */
"(" { return '('; }
")" { return ')'; }
"[" { return '['; }
"]" { return ']'; }
"?" { return '?'; }
":" { return ':'; }
"," { return ','; }
/* Expression terminator */
";" { return ';'; }
/* Ignored whitespace */
[ \r\n\t\v\f] { }
/* All other tokens are invalid */
. { return PRJM_EVAL_UNDEF; }
%%