-
Notifications
You must be signed in to change notification settings - Fork 3
/
statement-grammar.hpp
233 lines (206 loc) · 6.92 KB
/
statement-grammar.hpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*=============================================================================
Copyright (c) 2001-2009 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#if !defined(STATEMENT_GRAMMAR_HPP)
#define STATEMENT_GRAMMAR_HPP
#include "statement.hpp"
#include "error_handler.hpp"
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_container.hpp>
#include <boost/spirit/include/phoenix_statement.hpp>
///////////////////////////////////////////////////////////////////////////////
// Our statement grammar and compiler
///////////////////////////////////////////////////////////////////////////////
namespace{
struct InternalFunctions
:public boost::spirit::qi::symbols<char, int>
{
InternalFunctions()
{
add("print", op_print);
add("dump", op_dump);
add("debug", op_debug);
}
} internalFunctions;
}
namespace{
struct DataTypes
:public boost::spirit::qi::symbols<char, symbol_type>
{
DataTypes()
{
symbol_type type = {false, 1};
add("uint8_t", type);
type.sign = true;
type.size = 1;
add("int8_t", type);
type.sign = false;
type.size = 2;
add("uint16_t", type);
type.sign = true;
type.size = 2;
add("int16_t", type);
type.sign = false;
type.size = 4;
add("uint32_t", type);
type.sign = true;
type.size = 4;
add("int32_t", type);
type.sign = false;
type.size = 8;
add("uint64_t", type);
type.sign = true;
type.size = 8;
add("int64_t", type);
}
} dataTypes;
}
template <typename Iterator>
statement<Iterator>::statement(
std::vector<int>& code,
boost::spirit::qi::symbols<char, function_info>& functions)
: statement::base_type(statement_list)
, code(code)
, functions(functions)
, nvars(0)
, expr(code, vars, functions)
, add_var(var_adder(vars, nvars))
, add_var_2(var_adder2(vars, variables, nvars))
, op(code)
{
using boost::phoenix::ref;
using namespace boost::spirit::qi;
using namespace boost::phoenix;
identifier %=
raw[lexeme[alpha >> *(alnum | '_')]]
;
var_ref =
lexeme
[
vars [_val = _1]
>> !(alnum | '_') // make sure we have whole words
]
;
var_decl =
lexeme
[
dataTypes [_a = _1]
>> !(alnum | '_') // make sure we have whole words
]
> !var_ref // make sure the variable isn't redeclared
> identifier [add_var_2(_1, _a)]
> (';' | ('=' > assignment_rhs(ref(nvars)-1)))
;
assignment =
var_ref [_a = _1]
>> '='
> assignment_rhs(_a)
;
assignment_rhs =
expr
> lit(';') [op(op_store_32, _r1)]
;
if_statement =
lit("if")
>> '('
> expr [
op(op_jump_if, 0), // we shall fill this (0) in later
_a = boost::phoenix::size(boost::phoenix::ref(code))-1 // mark its position
]
> ')'
> statement_ [
// now we know where to jump to (after the if branch)
boost::phoenix::ref(code)[_a] = size(boost::phoenix::ref(code))
]
>>
-(
lexeme[
"else"
>> !(alnum | '_') // make sure we have whole words
] [
boost::phoenix::ref(code)[_a] += 2, // adjust for the "else" jump
op(op_jump, 0), // we shall fill this (0) in later
_a = size(boost::phoenix::ref(code))-1 // mark its position
]
> statement_ [
// now we know where to jump to (after the else branch)
boost::phoenix::ref(code)[_a] = size(boost::phoenix::ref(code))
]
)
;
while_statement =
lit("while") [
_a = size(boost::phoenix::ref(code)) // mark our position
]
>> '('
> expr [
op(op_jump_if, 0), // we shall fill this (0) in later
_b = size(boost::phoenix::ref(code))-1 // mark its position
]
> ')'
> statement_ [
op(op_jump, _a), // loop back
// now we know where to jump to (to exit the loop)
boost::phoenix::ref(code)[_b] = size(boost::phoenix::ref(code))
]
;
print_statement =
internalFunctions[_a = _1]
//lit("print")
>> lit('(')
> expr [op(_a)]
> lit(')')
> ';'
;
compound_statement =
'{' >> -statement_list >> '}'
;
return_statement =
lexeme[
"return"
>> !(alnum | '_') // make sure we have whole words
]
>> -(
eps(ref(has_return)) > expr [op(op_return)]
)
> ';'
;
statement_ =
var_decl
| print_statement
| assignment
| compound_statement
| if_statement
| while_statement
| return_statement
;
statement_list = +statement_;
identifier.name("identifier");
var_ref.name("variable-reference");
var_decl.name("variable-declaration");
assignment.name("assignment");
assignment_rhs.name("assignment-rhs");
if_statement.name("if-statement");
while_statement.name("while-statement");
print_statement.name("print-statement");
compound_statement.name("compound-statement");
return_statement.name("return-statement");
statement_.name("statement");
statement_list.name("statement-list");
// enable debugging
// boost::spirit::qi::debug(statement_list);
// boost::spirit::qi::debug(statement_);
// boost::spirit::qi::debug(if_statement);
// boost::spirit::qi::debug(while_statement);
// boost::spirit::qi::debug(compound_statement);
// boost::spirit::qi::debug(return_statement);
// boost::spirit::qi::debug(var_decl);
// boost::spirit::qi::debug(var_ref);
// boost::spirit::qi::debug(assignment);
// boost::spirit::qi::debug(assignment_rhs);
// boost::spirit::qi::debug(identifier);
on_error<fail>(statement_list, ::error_handler(_4, _3, _2));
}
#endif // STATEMENT_GRAMMAR_HPP