-
Notifications
You must be signed in to change notification settings - Fork 25
/
crowbar.l
125 lines (112 loc) · 3.01 KB
/
crowbar.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
%{
#include <stdio.h>
#include <string.h>
#include "DBG.h"
#include "crowbar.h"
#include "y.tab.h"
int
yywrap(void)
{
return 1;
}
static void
increment_line_number(void)
{
crb_get_current_interpreter()->current_line_number++;
}
%}
%start COMMENT STRING_LITERAL_STATE
%%
<INITIAL>"function" return FUNCTION;
<INITIAL>"if" return IF;
<INITIAL>"else" return ELSE;
<INITIAL>"elsif" return ELSIF;
<INITIAL>"while" return WHILE;
<INITIAL>"for" return FOR;
<INITIAL>"return" return RETURN_T;
<INITIAL>"break" return BREAK;
<INITIAL>"continue" return CONTINUE;
<INITIAL>"null" return NULL_T;
<INITIAL>"true" return TRUE_T;
<INITIAL>"false" return FALSE_T;
<INITIAL>"global" return GLOBAL_T;
<INITIAL>"(" return LP;
<INITIAL>")" return RP;
<INITIAL>"{" return LC;
<INITIAL>"}" return RC;
<INITIAL>";" return SEMICOLON;
<INITIAL>"," return COMMA;
<INITIAL>"&&" return LOGICAL_AND;
<INITIAL>"||" return LOGICAL_OR;
<INITIAL>"=" return ASSIGN;
<INITIAL>"==" return EQ;
<INITIAL>"!=" return NE;
<INITIAL>">" return GT;
<INITIAL>">=" return GE;
<INITIAL>"<" return LT;
<INITIAL>"<=" return LE;
<INITIAL>"+" return ADD;
<INITIAL>"-" return SUB;
<INITIAL>"*" return MUL;
<INITIAL>"/" return DIV;
<INITIAL>"%" return MOD;
<INITIAL>[A-Za-z_][A-Za-z_0-9]* {
yylval.identifier = crb_create_identifier(yytext);
return IDENTIFIER;
}
<INITIAL>([1-9][0-9]*)|"0" {
Expression *expression = crb_alloc_expression(INT_EXPRESSION);
sscanf(yytext, "%d", &expression->u.int_value);
yylval.expression = expression;
return INT_LITERAL;
}
<INITIAL>[0-9]+\.[0-9]+ {
Expression *Expression = crb_alloc_expression(DOUBLE_EXPRESSION);
sscanf(yytext, "%lf", &expression->u.double_value);
yylval.Expression = expression;
return DOUBLE_LITERAL;
}
<INITIAL>\" {
crb_open_string_literal();
BEGIN STRING_LITERAL_STATE;
}
# skip whitespace
<INITIAL>[ \t] ;
<INITIAL>\n {increment_line_number();}
# 所有其它的字符都报错
<INITIAL>. {
char buf[LINE_BUF_SIZE];
if (isprint(yytext[0])) {
buf[0] = yytext[0];
buf[1] = '\0';
} else {
sprintf(buf, "0x%02x", (unsigned char)yytext[0]);
}
crb_compile_error(CHARACTER_INVALID_ERR,
STRING_MESSAGE_ARGUMENT,
"bad_char",
buf,
MESSAGE_ARGUMENT_END);
}
<COMMENT>\n {
increment_line_number();
BEGIN INITIAL;
}
<COMMENT>. ;
<STRING_LITERAL_STATE>\" {
Expression *Expression = crb_alloc_expression(STRING_EXPRESSION);
Expression->u.string_value = crb_close_string_literal();
yylval.expression = expression;
BEGIN INITIAL;
return STRING_LITERAL;
}
<STRING_LITERAL_STATE>\n {
crb_add_string_literal('\n');
increment_line_number();
}
<STRING_LITERAL_STATE>\\\" crb_add_string_literal('"');
<STRING_LITERAL_STATE>\\n crb_add_string_literal('\n');
<STRING_LITERAL_STATE>\\t crb_add_string_literal('\t');
<STRING_LITERAL_STATE>\\\\ crb_add_string_literal('\\');
<STRING_LITERAL_STATE>. crb_add_string_literal(yytext[0]);
%%