Edits to chapter four.
[ragel.git] / rlgen-ruby / ruby-fflatcodegen.cpp
blob0f0df548a891694a131cdce02d91210e58ba138f
1 /*
2 * 2007 Victor Hugo Borja <vic@rubyforge.org>
3 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
4 */
6 /* This file is part of Ragel.
8 * Ragel is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * Ragel is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Ragel; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "ruby-fflatcodegen.h"
25 void RubyFFlatCodeGen::GOTO( ostream &out, int gotoDest, bool inFinish )
27 out <<
28 " begin\n"
29 " " << CS() << " = " << gotoDest << "\n"
30 " _goto_level = _again\n"
31 " next\n"
32 " end\n";
35 void RubyFFlatCodeGen::GOTO_EXPR( ostream &out, InlineItem *ilItem, bool inFinish )
37 out <<
38 " begin\n"
39 " " << CS() << " = (";
40 INLINE_LIST( out, ilItem->children, 0, inFinish );
41 out << ")\n";
42 out <<
43 " _goto_level = _again\n"
44 " next\n"
45 " end\n";
48 void RubyFFlatCodeGen::CALL( ostream &out, int callDest, int targState, bool inFinish )
50 if ( prePushExpr != 0 ) {
51 out << "begin\n";
52 INLINE_LIST( out, prePushExpr, 0, false );
55 out <<
56 " begin\n"
57 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
58 " " << TOP() << "+= 1\n"
59 " " << CS() << " = " << callDest << "\n"
60 " _goto_level = _again\n"
61 " next\n"
62 " end\n";
64 if ( prePushExpr != 0 )
65 out << "end\n";
68 void RubyFFlatCodeGen::CALL_EXPR(ostream &out, InlineItem *ilItem,
69 int targState, bool inFinish )
71 if ( prePushExpr != 0 ) {
72 out << "begin\n";
73 INLINE_LIST( out, prePushExpr, 0, false );
76 out <<
77 " begin\n"
78 " " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
79 " " << TOP() << " += 1\n"
80 " " << CS() << " = (";
81 INLINE_LIST( out, ilItem->children, targState, inFinish );
82 out << ")\n";
84 out <<
85 " _goto_level = _again\n"
86 " next\n"
87 " end\n";
89 if ( prePushExpr != 0 )
90 out << "end\n";
93 void RubyFFlatCodeGen::RET( ostream &out, bool inFinish )
95 out <<
96 " begin\n"
97 " " << TOP() << " -= 1\n"
98 " " << CS() << " = " << STACK() << "[" << TOP() << "]\n";
100 if ( postPopExpr != 0 ) {
101 out << "begin\n";
102 INLINE_LIST( out, postPopExpr, 0, false );
103 out << "end\n";
106 out <<
107 " _goto_level = _again\n"
108 " next\n"
109 " end\n";
112 void RubyFFlatCodeGen::BREAK( ostream &out, int targState )
114 out <<
115 " begin\n"
116 " _goto_level = _out\n"
117 " next\n"
118 " end\n";
122 int RubyFFlatCodeGen::TO_STATE_ACTION( RedStateAp *state )
124 int act = 0;
125 if ( state->toStateAction != 0 )
126 act = state->toStateAction->actListId+1;
127 return act;
130 int RubyFFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state )
132 int act = 0;
133 if ( state->fromStateAction != 0 )
134 act = state->fromStateAction->actListId+1;
135 return act;
138 int RubyFFlatCodeGen::EOF_ACTION( RedStateAp *state )
140 int act = 0;
141 if ( state->eofAction != 0 )
142 act = state->eofAction->actListId+1;
143 return act;
146 /* Write out the function for a transition. */
147 int RubyFFlatCodeGen::TRANS_ACTION( RedTransAp *trans )
149 int action = 0;
150 if ( trans->action != 0 )
151 action = trans->action->actListId+1;
152 return action;
155 /* Write out the function switch. This switch is keyed on the values
156 * of the func index. */
157 std::ostream &RubyFFlatCodeGen::TO_STATE_ACTION_SWITCH()
159 /* Loop the actions. */
160 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
161 if ( redAct->numToStateRefs > 0 ) {
162 /* Write the entry label. */
163 out << "\twhen " << redAct->actListId+1 << " then\n";
165 /* Write each action in the list of action items. */
166 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
167 ACTION( out, item->value, 0, false );
171 genLineDirective( out );
172 return out;
175 /* Write out the function switch. This switch is keyed on the values
176 * of the func index. */
177 std::ostream &RubyFFlatCodeGen::FROM_STATE_ACTION_SWITCH()
179 /* Loop the actions. */
180 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
181 if ( redAct->numFromStateRefs > 0 ) {
182 /* Write the entry label. */
183 out << "\twhen " << redAct->actListId+1 << " then\n";
185 /* Write each action in the list of action items. */
186 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
187 ACTION( out, item->value, 0, false );
191 genLineDirective( out );
192 return out;
195 std::ostream &RubyFFlatCodeGen::EOF_ACTION_SWITCH()
197 /* Loop the actions. */
198 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
199 if ( redAct->numEofRefs > 0 ) {
200 /* Write the entry label. */
201 out << "\twhen " << redAct->actListId+1 << " then\n";
203 /* Write each action in the list of action items. */
204 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
205 ACTION( out, item->value, 0, true );
209 genLineDirective( out );
210 return out;
213 /* Write out the function switch. This switch is keyed on the values
214 * of the func index. */
215 std::ostream &RubyFFlatCodeGen::ACTION_SWITCH()
217 /* Loop the actions. */
218 for ( ActionTableMap::Iter redAct = redFsm->actionMap; redAct.lte(); redAct++ ) {
219 if ( redAct->numTransRefs > 0 ) {
220 /* Write the entry label. */
221 out << "\twhen " << redAct->actListId+1 << " then\n";
223 /* Write each action in the list of action items. */
224 for ( ActionTable::Iter item = redAct->key; item.lte(); item++ )
225 ACTION( out, item->value, 0, false );
230 genLineDirective( out );
231 return out;
234 void RubyFFlatCodeGen::writeData()
236 if ( redFsm->anyConditions() ) {
237 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
238 COND_KEYS();
239 CLOSE_ARRAY() <<
240 "\n";
242 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
243 COND_KEY_SPANS();
244 CLOSE_ARRAY() <<
245 "\n";
247 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
248 CONDS();
249 CLOSE_ARRAY() <<
250 "\n";
252 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
253 COND_INDEX_OFFSET();
254 CLOSE_ARRAY() <<
255 "\n";
258 OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
259 KEYS();
260 CLOSE_ARRAY() <<
261 "\n";
263 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
264 KEY_SPANS();
265 CLOSE_ARRAY() <<
266 "\n";
268 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
269 FLAT_INDEX_OFFSET();
270 CLOSE_ARRAY() <<
271 "\n";
273 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
274 INDICIES();
275 CLOSE_ARRAY() <<
276 "\n";
278 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
279 TRANS_TARGS();
280 CLOSE_ARRAY() <<
281 "\n";
283 if ( redFsm->anyActions() ) {
284 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), TA() );
285 TRANS_ACTIONS();
286 CLOSE_ARRAY() <<
287 "\n";
290 if ( redFsm->anyToStateActions() ) {
291 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
292 TO_STATE_ACTIONS();
293 CLOSE_ARRAY() <<
294 "\n";
297 if ( redFsm->anyFromStateActions() ) {
298 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
299 FROM_STATE_ACTIONS();
300 CLOSE_ARRAY() <<
301 "\n";
304 if ( redFsm->anyEofActions() ) {
305 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActListId), EA() );
306 EOF_ACTIONS();
307 CLOSE_ARRAY() <<
308 "\n";
311 if ( redFsm->anyEofTrans() ) {
312 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex+1), ET() );
313 EOF_TRANS();
314 CLOSE_ARRAY() <<
315 "\n";
318 STATE_IDS();
321 void RubyFFlatCodeGen::writeExec()
323 out <<
324 "begin\n"
325 " testEof = false\n"
326 " _slen, _trans, _keys, _inds";
327 if ( redFsm->anyRegCurStateRef() )
328 out << ", _ps";
329 if ( redFsm->anyConditions() )
330 out << ", _cond, _conds, _widec";
331 if ( redFsm->anyToStateActions() || redFsm->anyRegActions()
332 || redFsm->anyFromStateActions() )
333 out << ", _acts, _nacts";
335 out << " = nil\n";
337 out <<
338 " _goto_level = 0\n"
339 " _resume = 10\n"
340 " _eof_trans = 15\n"
341 " _again = 20\n"
342 " _test_eof = 30\n"
343 " _out = 40\n";
345 out <<
346 " while true\n"
347 " if _goto_level <= 0\n";
349 if ( hasEnd ) {
350 out <<
351 " if " << P() << " == " << PE() << "\n"
352 " _goto_level = _test_eof\n"
353 " next\n"
354 " end\n";
357 if ( redFsm->errState != 0 ) {
358 out <<
359 " if " << CS() << " == " << redFsm->errState->id << "\n"
360 " _goto_level = _out\n"
361 " next\n"
362 " end\n";
365 /* The resume label. */
366 out <<
367 " end\n"
368 " if _goto_level <= _resume\n";
370 if ( redFsm->anyFromStateActions() ) {
371 out <<
372 " case " << FSA() << "[" << CS() << "] \n";
373 FROM_STATE_ACTION_SWITCH() <<
374 " end\n";
377 if ( redFsm->anyConditions() )
378 COND_TRANSLATE();
380 LOCATE_TRANS();
382 if ( redFsm->anyEofTrans() ) {
383 out <<
384 " end\n"
385 " if _goto_level <= _eof_trans\n";
388 if ( redFsm->anyRegCurStateRef() )
389 out << " _ps = " << CS() << "\n";
391 out << " " << CS() << " = " << TT() << "[_trans]\n";
393 if ( redFsm->anyRegActions() ) {
394 /* break _again */
395 out <<
396 " if " << TA() << "[_trans] != 0\n"
397 " case " << TA() << "[_trans]" << "\n";
398 ACTION_SWITCH() <<
399 " end\n"
400 " end\n";
403 /* The again label. */
404 out <<
405 " end\n"
406 " if _goto_level <= _again\n";
408 if ( redFsm->anyToStateActions() ) {
409 out <<
410 " case " << TSA() << "[" << CS() << "] \n";
411 TO_STATE_ACTION_SWITCH() <<
412 " end\n"
413 "\n";
416 if ( redFsm->errState != 0 ) {
417 out <<
418 " if " << CS() << " == " << redFsm->errState->id << "\n"
419 " _goto_level = _out\n"
420 " next\n"
421 " end\n";
424 out << " " << P() << " += 1\n";
426 if ( hasEnd ) {
427 out <<
428 " if " << P() << " != " << PE() << "\n"
429 " _goto_level = _resume\n"
430 " next\n"
431 " end\n";
433 else {
434 out <<
435 " _goto_level = _resume\n"
436 " next\n";
439 /* The test eof label. */
440 out <<
441 " end\n"
442 " if _goto_level <= _test_eof\n";
444 if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
445 out <<
446 " if " << P() << " == " << EOFV() << "\n";
448 if ( redFsm->anyEofTrans() ) {
449 out <<
450 " if " << ET() << "[" << CS() << "] > 0\n"
451 " _trans = " << ET() << "[" << CS() << "] - 1;\n"
452 " _goto_level = _eof_trans\n"
453 " next;\n"
454 " end\n";
457 if ( redFsm->anyEofActions() ) {
458 out <<
459 " case " << EA() << "[" << CS() << "]\n";
460 EOF_ACTION_SWITCH() <<
461 " end\n";
464 out <<
465 " end\n"
466 "\n";
469 out <<
470 " end\n"
471 " if _goto_level <= _out\n"
472 " break\n"
473 " end\n"
474 "end\n";
476 /* Wrapping the execute block. */
477 out << " end\n";
481 * Local Variables:
482 * mode: c++
483 * indent-tabs-mode: 1
484 * c-file-style: "bsd"
485 * End: