Started working on the direct construction of the backend inline item lists.
[external/ragel.git] / rlgen-ruby / ruby-flatcodegen.cpp
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@complang.org>
3  *  Copyright 2007 Victor Hugo Borja <vic@rubyforge.org>
4  */
5
6 /*  This file is part of Ragel.
7  *
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.
12  * 
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.
17  * 
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 
21  */
22
23 #include "ruby-flatcodegen.h"
24 #include "rlgen-ruby.h"
25 #include "redfsm.h"
26 #include "gendata.h"
27
28 using std::ostream;
29 using std::string;
30
31 std::ostream &RubyFlatCodeGen::TO_STATE_ACTION_SWITCH()
32 {
33         /* Walk the list of functions, printing the cases. */
34         for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
35                 /* Write out referenced actions. */
36                 if ( act->numToStateRefs > 0 ) {
37                         /* Write the case label, the action and the case break */
38                         out << "\twhen " << act->actionId << " then\n";
39                         ACTION( out, act, 0, false );
40                 }
41         }
42
43         genLineDirective( out );
44         return out;
45 }
46
47 std::ostream &RubyFlatCodeGen::FROM_STATE_ACTION_SWITCH()
48 {
49         /* Walk the list of functions, printing the cases. */
50         for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
51                 /* Write out referenced actions. */
52                 if ( act->numFromStateRefs > 0 ) {
53                         /* Write the case label, the action and the case break */
54                         out << "\twhen " << act->actionId << " then\n";
55                         ACTION( out, act, 0, false );
56                 }
57         }
58
59         genLineDirective( out );
60         return out;
61 }
62
63 std::ostream &RubyFlatCodeGen::EOF_ACTION_SWITCH()
64 {
65         /* Walk the list of functions, printing the cases. */
66         for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
67                 /* Write out referenced actions. */
68                 if ( act->numEofRefs > 0 ) {
69                         /* Write the case label, the action and the case break */
70                         out << "\twhen " << act->actionId << " then\n";
71                         ACTION( out, act, 0, true );
72                 }
73         }
74
75         genLineDirective( out );
76         return out;
77 }
78
79 std::ostream &RubyFlatCodeGen::ACTION_SWITCH()
80 {
81         /* Walk the list of functions, printing the cases. */
82         for ( GenActionList::Iter act = actionList; act.lte(); act++ ) {
83                 /* Write out referenced actions. */
84                 if ( act->numTransRefs > 0 ) {
85                         /* Write the case label, the action and the case break */
86                         out << "\twhen " << act->actionId << " then\n";
87                         ACTION( out, act, 0, false );
88                 }
89         }
90
91         genLineDirective( out );
92         return out;
93 }
94
95
96 std::ostream &RubyFlatCodeGen::KEYS()
97 {
98         START_ARRAY_LINE();
99         int totalTrans = 0;
100         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
101                 /* Emit just low key and high key. */
102                 ARRAY_ITEM( KEY( st->lowKey ), ++totalTrans, false );
103                 ARRAY_ITEM( KEY( st->highKey ), ++totalTrans, false );
104                 if ( ++totalTrans % IALL == 0 )
105                         out << "\n\t";
106
107         }
108
109         /* Output one last number so we don't have to figure out when the last
110          * entry is and avoid writing a comma. */
111         ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
112         END_ARRAY_LINE();
113         return out;
114 }
115
116 std::ostream &RubyFlatCodeGen::INDICIES()
117 {
118         int totalTrans = 0;
119         START_ARRAY_LINE();
120         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
121                 if ( st->transList != 0 ) {
122                         /* Walk the singles. */
123                         unsigned long long span = keyOps->span( st->lowKey, st->highKey );
124                         for ( unsigned long long pos = 0; pos < span; pos++ ) {
125                                 ARRAY_ITEM( KEY( st->transList[pos]->id ), ++totalTrans, false );
126                         }
127                 }
128
129                 /* The state's default index goes next. */
130                 if ( st->defTrans != 0 )
131                         ARRAY_ITEM( KEY( st->defTrans->id ), ++totalTrans, false );
132         }
133
134         /* Output one last number so we don't have to figure out when the last
135          * entry is and avoid writing a comma. */
136         ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
137         END_ARRAY_LINE();
138         return out;
139 }
140
141 std::ostream &RubyFlatCodeGen::FLAT_INDEX_OFFSET()
142 {
143         START_ARRAY_LINE();
144         int totalStateNum = 0, curIndOffset = 0;
145         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
146                 /* Write the index offset. */
147                 ARRAY_ITEM( INT( curIndOffset ), ++totalStateNum, st.last() );
148                 /* Move the index offset ahead. */
149                 if ( st->transList != 0 )
150                         curIndOffset += keyOps->span( st->lowKey, st->highKey );
151
152                 if ( st->defTrans != 0 )
153                         curIndOffset += 1;
154         }
155
156         END_ARRAY_LINE();
157         return out;
158 }
159
160 std::ostream &RubyFlatCodeGen::KEY_SPANS()
161 {
162         START_ARRAY_LINE();
163         int totalStateNum = 0;
164         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
165                 /* Write singles length. */
166                 unsigned long long span = 0;
167                 if ( st->transList != 0 )
168                         span = keyOps->span( st->lowKey, st->highKey );
169                 ARRAY_ITEM( INT( span ), ++totalStateNum, st.last() );
170         }
171         END_ARRAY_LINE();
172         return out;
173 }
174
175 std::ostream &RubyFlatCodeGen::TO_STATE_ACTIONS()
176 {
177         START_ARRAY_LINE();
178         int totalStateNum = 0;
179         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
180                 /* Write any eof action. */
181                 ARRAY_ITEM( INT( TO_STATE_ACTION(st) ), ++totalStateNum, st.last() );
182         }
183         END_ARRAY_LINE();
184         return out;
185 }
186
187 std::ostream &RubyFlatCodeGen::FROM_STATE_ACTIONS()
188 {
189         START_ARRAY_LINE();
190         int totalStateNum = 0;
191         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
192                 /* Write any eof action. */
193                 ARRAY_ITEM( INT( FROM_STATE_ACTION(st) ), ++totalStateNum, st.last() );
194         }
195         END_ARRAY_LINE();
196         return out;
197 }
198
199 std::ostream &RubyFlatCodeGen::EOF_ACTIONS()
200 {
201         START_ARRAY_LINE();
202         int totalStateNum = 0;
203         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
204                 /* Write any eof action. */
205                 ARRAY_ITEM( INT( EOF_ACTION(st) ), ++totalStateNum, st.last() );
206         }
207         END_ARRAY_LINE();
208         return out;
209 }
210
211 std::ostream &RubyFlatCodeGen::EOF_TRANS()
212 {
213         START_ARRAY_LINE();
214         int totalStateNum = 0;
215         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
216                 /* Write any eof action. */
217                 long trans = 0;
218                 if ( st->eofTrans != 0 ) {
219                         assert( st->eofTrans->pos >= 0 );
220                         trans = st->eofTrans->pos+1;
221                 }
222
223                 /* Write any eof action. */
224                 ARRAY_ITEM( INT(trans), ++totalStateNum, st.last() );
225         }
226         END_ARRAY_LINE();
227         return out;
228 }
229
230 std::ostream &RubyFlatCodeGen::TRANS_TARGS()
231 {
232         /* Transitions must be written ordered by their id. */
233         RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
234         for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
235                 transPtrs[trans->id] = trans;
236
237         /* Keep a count of the num of items in the array written. */
238         START_ARRAY_LINE();
239
240         int totalStates = 0;
241         for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
242                 /* Save the position. Needed for eofTargs. */
243                 RedTransAp *trans = transPtrs[t];
244                 trans->pos = t;
245
246                 /* Write out the target state. */
247                 ARRAY_ITEM( INT( trans->targ->id ), ++totalStates, t >= redFsm->transSet.length()-1  );
248         }
249         END_ARRAY_LINE();
250         delete[] transPtrs;
251         return out;
252 }
253
254
255 std::ostream &RubyFlatCodeGen::TRANS_ACTIONS()
256 {
257         /* Transitions must be written ordered by their id. */
258         RedTransAp **transPtrs = new RedTransAp*[redFsm->transSet.length()];
259         for ( TransApSet::Iter trans = redFsm->transSet; trans.lte(); trans++ )
260                 transPtrs[trans->id] = trans;
261
262         /* Keep a count of the num of items in the array written. */
263         START_ARRAY_LINE();
264         int totalAct = 0;
265         for ( int t = 0; t < redFsm->transSet.length(); t++ ) {
266                 /* Write the function for the transition. */
267                 RedTransAp *trans = transPtrs[t];
268                 ARRAY_ITEM( INT( TRANS_ACTION( trans ) ), ++totalAct, t >= redFsm->transSet.length()-1 );
269         }
270         END_ARRAY_LINE();
271         delete[] transPtrs;
272         return out;
273 }
274
275
276 void RubyFlatCodeGen::LOCATE_TRANS()
277 {
278         out <<
279                 "       _keys = " << CS() << " << 1\n"
280                 "       _inds = " << IO() << "[" << CS() << "]\n"
281                 "       _slen = " << SP() << "[" << CS() << "]\n"
282                 "       _trans = if (   _slen > 0 && \n"
283                 "                       " << K() << "[_keys] <= " << GET_WIDE_KEY() << " && \n"
284                 "                       " << GET_WIDE_KEY() << " <= " << K() << "[_keys + 1] \n"
285                 "                   ) then\n"
286                 "                       " << I() << "[ _inds + " << GET_WIDE_KEY() << " - " << K() << "[_keys] ] \n"
287                 "                else \n"
288                 "                       " << I() << "[ _inds + _slen ]\n"
289                 "                end\n"
290                 "";
291         
292 }
293
294 std::ostream &RubyFlatCodeGen::COND_INDEX_OFFSET()
295 {
296         START_ARRAY_LINE();
297         int totalStateNum = 0, curIndOffset = 0;
298         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
299                 /* Write the index offset. */
300                 ARRAY_ITEM( INT( curIndOffset ), ++totalStateNum, st.last() );
301                 /* Move the index offset ahead. */
302                 if ( st->condList != 0 )
303                         curIndOffset += keyOps->span( st->condLowKey, st->condHighKey );
304         }
305         END_ARRAY_LINE();
306         return out;
307 }
308
309 void RubyFlatCodeGen::COND_TRANSLATE()
310 {
311         out << 
312                 "       _widec = " << GET_KEY() << "\n"
313                 "       _keys = " << CS() << " << 1\n"
314                 "       _conds = " << CO() << "[" << CS() << "]\n"
315                 "       _slen = " << CSP() << "[" << CS() << "]\n"
316                 "       _cond = if ( _slen > 0 && \n" 
317                 "                    " << CK() << "[_keys] <= " << GET_WIDE_KEY() << " &&\n" 
318                 "                    " << GET_WIDE_KEY() << " <= " << CK() << "[_keys + 1]\n"
319                 "                  ) then \n"
320                 "                       " << C() << "[ _conds + " << GET_WIDE_KEY() << " - " << CK() << "[_keys]" << " ]\n"
321                 "               else\n"
322                 "                      0\n"
323                 "               end\n";
324         out <<
325                 "       case _cond \n";
326         for ( CondSpaceList::Iter csi = condSpaceList; csi.lte(); csi++ ) {
327                 GenCondSpace *condSpace = csi;
328                 out << "        when " << condSpace->condSpaceId + 1 << " then\n";
329                 out << TABS(2) << "_widec = " << "(" <<
330                                 KEY(condSpace->baseKey) << " + (" << GET_KEY() << 
331                                 " - " << KEY(keyOps->minKey) << "))\n";
332
333                 for ( GenCondSet::Iter csi = condSpace->condSet; csi.lte(); csi++ ) {
334                         out << TABS(2) << "if ( ";
335                         CONDITION( out, *csi );
336                         Size condValOffset = ((1 << csi.pos()) * keyOps->alphSize());
337                         out << 
338                                 " ) then \n" <<
339                                 TABS(3) << "  _widec += " << condValOffset << "\n"
340                                 "end\n";
341                 }
342         }
343
344         out <<
345                 "       end # _cond switch \n";
346 }
347
348 std::ostream &RubyFlatCodeGen::CONDS()
349 {
350         int totalTrans = 0;
351         START_ARRAY_LINE();
352         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
353                 if ( st->condList != 0 ) {
354                         /* Walk the singles. */
355                         unsigned long long span = keyOps->span( st->condLowKey, st->condHighKey );
356                         for ( unsigned long long pos = 0; pos < span; pos++ ) {
357                                 if ( st->condList[pos] != 0 )
358                                         ARRAY_ITEM( INT( st->condList[pos]->condSpaceId + 1 ), ++totalTrans, false );
359                                 else
360                                         ARRAY_ITEM( INT( 0 ), ++totalTrans, false );
361                         }
362                 }
363         }
364
365         /* Output one last number so we don't have to figure out when the last
366          * entry is and avoid writing a comma. */
367         ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
368         END_ARRAY_LINE();
369         return out;
370 }
371
372 std::ostream &RubyFlatCodeGen::COND_KEYS()
373 {
374         START_ARRAY_LINE();
375         int totalTrans = 0;
376         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
377                 /* Emit just cond low key and cond high key. */
378                 ARRAY_ITEM( KEY( st->condLowKey ), ++totalTrans, false );
379                 ARRAY_ITEM( KEY( st->condHighKey ), ++totalTrans, false );
380         }
381
382         /* Output one last number so we don't have to figure out when the last
383          * entry is and avoid writing a comma. */
384         ARRAY_ITEM( INT( 0 ), ++totalTrans, true );
385         END_ARRAY_LINE();
386         return out;
387 }
388
389 std::ostream &RubyFlatCodeGen::COND_KEY_SPANS()
390 {
391         START_ARRAY_LINE();
392         int totalStateNum = 0;
393         for ( RedStateList::Iter st = redFsm->stateList; st.lte(); st++ ) {
394                 /* Write singles length. */
395                 unsigned long long span = 0;
396                 if ( st->condList != 0 )
397                         span = keyOps->span( st->condLowKey, st->condHighKey );
398                 ARRAY_ITEM( INT( span ), ++totalStateNum, false );
399         }
400         END_ARRAY_LINE();
401         return out;
402 }
403
404
405 void RubyFlatCodeGen::GOTO( ostream &out, int gotoDest, bool inFinish )
406 {
407         out << 
408                 "       begin\n"
409                 "               " << CS() << " = " << gotoDest << "\n"
410                 "               _trigger_goto = true\n"
411                 "               _goto_level = _again\n"
412                 "               break\n"
413                 "       end\n";
414 }
415
416 void RubyFlatCodeGen::CALL( ostream &out, int callDest, int targState, bool inFinish )
417 {
418         if ( prePushExpr != 0 ) {
419                 out << "begin\n";
420                 INLINE_LIST( out, prePushExpr, 0, false );
421         }
422
423         out <<
424                 "       begin\n"
425                 "               " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
426                 "               " << TOP() << "+= 1\n"
427                 "               " << CS() << " = " << callDest << "\n"
428                 "               _trigger_goto = true\n"
429                 "               _goto_level = _again\n"
430                 "               break\n"
431                 "       end\n";
432
433         if ( prePushExpr != 0 )
434                 out << "end\n";
435 }
436
437 void RubyFlatCodeGen::CALL_EXPR(ostream &out, GenInlineItem *ilItem, int targState, bool inFinish )
438 {
439         if ( prePushExpr != 0 ) {
440                 out << "begin\n";
441                 INLINE_LIST( out, prePushExpr, 0, false );
442         }
443
444         out <<
445                 "       begin\n"
446                 "               " << STACK() << "[" << TOP() << "] = " << CS() << "\n"
447                 "               " << TOP() << " += 1\n"
448                 "               " << CS() << " = (";
449         INLINE_LIST( out, ilItem->children, targState, inFinish );
450         out << ")\n";
451
452         out << 
453                 "               _trigger_goto = true\n"
454                 "               _goto_level = _again\n"
455                 "               break\n"
456                 "       end\n";
457
458         if ( prePushExpr != 0 )
459                 out << "end\n";
460 }
461
462 void RubyFlatCodeGen::RET( ostream &out, bool inFinish )
463 {
464         out <<
465                 "       begin\n"
466                 "               " << TOP() << " -= 1\n"
467                 "               " << CS() << " = " << STACK() << "[" << TOP() << "]\n";
468
469         if ( postPopExpr != 0 ) {
470                 out << "begin\n";
471                 INLINE_LIST( out, postPopExpr, 0, false );
472                 out << "end\n";
473         }
474
475         out <<
476                 "               _trigger_goto = true\n"
477                 "               _goto_level = _again\n"
478                 "               break\n"
479                 "       end\n";
480 }
481
482 void RubyFlatCodeGen::NEXT( ostream &ret, int nextDest, bool inFinish )
483 {
484         ret << CS() << " = " << nextDest << ";";
485 }
486
487 void RubyFlatCodeGen::GOTO_EXPR( ostream &out, GenInlineItem *ilItem, bool inFinish )
488 {
489         out << 
490                 "       begin\n"
491                 "               " << CS() << " = (";
492         INLINE_LIST( out, ilItem->children, 0, inFinish );
493         out << ")\n";
494         out <<
495                 "               _trigger_goto = true\n"
496                 "               _goto_level = _again\n"
497                 "               break\n"
498                 "       end\n";
499 }
500
501 void RubyFlatCodeGen::NEXT_EXPR( ostream &ret, GenInlineItem *ilItem, bool inFinish )
502 {
503         ret << CS() << " = (";
504         INLINE_LIST( ret, ilItem->children, 0, inFinish );
505         ret << ");";
506 }
507
508
509 void RubyFlatCodeGen::CURS( ostream &ret, bool inFinish )
510 {
511         ret << "(_ps)";
512 }
513
514 void RubyFlatCodeGen::TARGS( ostream &ret, bool inFinish, int targState )
515 {
516         ret << "(" << CS() << ")";
517 }
518
519 void RubyFlatCodeGen::BREAK( ostream &out, int targState )
520 {
521         out << 
522                 "       begin\n"
523                 "               " << P() << " += 1\n"
524                 "               _trigger_goto = true\n"
525                 "               _goto_level = _out\n"
526                 "               break\n"
527                 "       end\n";
528 }
529
530 int RubyFlatCodeGen::TO_STATE_ACTION( RedStateAp *state )
531 {
532         int act = 0;
533         if ( state->toStateAction != 0 )
534                 act = state->toStateAction->location+1;
535         return act;
536 }
537
538 int RubyFlatCodeGen::FROM_STATE_ACTION( RedStateAp *state )
539 {
540         int act = 0;
541         if ( state->fromStateAction != 0 )
542                 act = state->fromStateAction->location+1;
543         return act;
544 }
545
546 int RubyFlatCodeGen::EOF_ACTION( RedStateAp *state )
547 {
548         int act = 0;
549         if ( state->eofAction != 0 )
550                 act = state->eofAction->location+1;
551         return act;
552 }
553
554 int RubyFlatCodeGen::TRANS_ACTION( RedTransAp *trans )
555 {
556         /* If there are actions, emit them. Otherwise emit zero. */
557         int act = 0;
558         if ( trans->action != 0 )
559                 act = trans->action->location+1;
560         return act;
561 }
562
563 void RubyFlatCodeGen::writeData()
564 {
565         /* If there are any transtion functions then output the array. If there
566          * are none, don't bother emitting an empty array that won't be used. */
567         if ( redFsm->anyActions() ) {
568                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActArrItem), A() );
569                 ACTIONS_ARRAY();
570                 CLOSE_ARRAY() <<
571                 "\n";
572         }
573
574         if ( redFsm->anyConditions() ) {
575                 OPEN_ARRAY( WIDE_ALPH_TYPE(), CK() );
576                 COND_KEYS();
577                 CLOSE_ARRAY() <<
578                 "\n";
579
580                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondSpan), CSP() );
581                 COND_KEY_SPANS();
582                 CLOSE_ARRAY() <<
583                 "\n";
584
585                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCond), C() );
586                 CONDS();
587                 CLOSE_ARRAY() <<
588                 "\n";
589
590                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxCondIndexOffset), CO() );
591                 COND_INDEX_OFFSET();
592                 CLOSE_ARRAY() <<
593                 "\n";
594         }
595
596         OPEN_ARRAY( WIDE_ALPH_TYPE(), K() );
597         KEYS();
598         CLOSE_ARRAY() <<
599         "\n";
600
601         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxSpan), SP() );
602         KEY_SPANS();
603         CLOSE_ARRAY() <<
604         "\n";
605
606         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxFlatIndexOffset), IO() );
607         FLAT_INDEX_OFFSET();
608         CLOSE_ARRAY() <<
609         "\n";
610
611         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndex), I() );
612         INDICIES();
613         CLOSE_ARRAY() <<
614         "\n";
615
616         OPEN_ARRAY( ARRAY_TYPE(redFsm->maxState), TT() );
617         TRANS_TARGS();
618         CLOSE_ARRAY() <<
619         "\n";
620
621         if ( redFsm->anyActions() ) {
622                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TA() );
623                 TRANS_ACTIONS();
624                 CLOSE_ARRAY() <<
625                 "\n";
626         }
627
628         if ( redFsm->anyToStateActions() ) {
629                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), TSA() );
630                 TO_STATE_ACTIONS();
631                 CLOSE_ARRAY() <<
632                 "\n";
633         }
634
635         if ( redFsm->anyFromStateActions() ) {
636                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), FSA() );
637                 FROM_STATE_ACTIONS();
638                 CLOSE_ARRAY() <<
639                 "\n";
640         }
641
642         if ( redFsm->anyEofActions() ) {
643                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxActionLoc), EA() );
644                 EOF_ACTIONS();
645                 CLOSE_ARRAY() <<
646                 "\n";
647         }
648
649         if ( redFsm->anyEofTrans() ) {
650                 OPEN_ARRAY( ARRAY_TYPE(redFsm->maxIndexOffset+1), ET() );
651                 EOF_TRANS();
652                 CLOSE_ARRAY() <<
653                 "\n";
654         }
655         
656         STATE_IDS();
657 }
658
659 void RubyFlatCodeGen::writeExec()
660 {
661         out << 
662                 "begin # ragel flat\n"
663                 "       testEof = false\n"
664                 "       _slen, _trans, _keys, _inds";
665         if ( redFsm->anyRegCurStateRef() )
666                 out << ", _ps";
667         if ( redFsm->anyConditions() )
668                 out << ", _cond, _conds, _widec";
669         if ( redFsm->anyToStateActions() || redFsm->anyRegActions() 
670                         || redFsm->anyFromStateActions() )
671                 out << ", _acts, _nacts";
672         
673         out << " = nil\n";
674
675         out << 
676                 "       _goto_level = 0\n"
677                 "       _resume = 10\n"
678                 "       _eof_trans = 15\n"
679                 "       _again = 20\n"
680                 "       _test_eof = 30\n"
681                 "       _out = 40\n";
682
683         out << 
684                 "       while true\n"
685                 "       _trigger_goto = false\n"
686                 "       if _goto_level <= 0\n";
687         
688         if ( hasEnd ) {
689                 out << 
690                         "       if " << P() << " == " << PE() << "\n"
691                         "               _goto_level = _test_eof\n"
692                         "               next\n"
693                         "       end\n";
694         }
695
696         if ( redFsm->errState != 0 ) {
697                 out << 
698                         "       if " << CS() << " == " << redFsm->errState->id << "\n"
699                         "               _goto_level = _out\n"
700                         "               next\n"
701                         "       end\n";
702         }
703         
704         /* The resume label. */
705         out << 
706                 "       end\n"
707                 "       if _goto_level <= _resume\n";
708
709         if ( redFsm->anyFromStateActions() ) {
710                 out << 
711                         "       _acts = " << FSA() << "[" << CS() << "]\n"
712                         "       _nacts = " << A() << "[_acts]\n"
713                         "       _acts += 1\n"
714                         "       while _nacts > 0\n"
715                         "               _nacts -= 1\n"
716                         "               _acts += 1\n"
717                         "               case " << A() << "[_acts - 1]\n";
718                 FROM_STATE_ACTION_SWITCH();
719                 out <<
720                         "               end # from state action switch\n"
721                         "       end\n"
722                         "       if _trigger_goto\n"
723                         "               next\n"
724                         "       end\n";
725         }
726
727         if ( redFsm->anyConditions() )
728                 COND_TRANSLATE();
729         
730         LOCATE_TRANS();
731
732         if ( redFsm->anyEofTrans() ) {
733                 out << 
734                         "       end\n"
735                         "       if _goto_level <= _eof_trans\n";
736         }
737
738         if ( redFsm->anyRegCurStateRef() )
739                 out << "        _ps = " << CS() << "\n";
740
741         out << "        " << CS() << " = " << TT() << "[_trans]\n";
742
743         if ( redFsm->anyRegActions() ) {
744                 out << 
745                         "       if " << TA() << "[_trans] != 0\n"
746                         "               _acts = " << TA() << "[_trans]\n"
747                         "               _nacts = " << A() << "[_acts]\n"
748                         "               _acts += 1\n"
749                         "               while _nacts > 0\n"
750                         "                       _nacts -= 1\n"
751                         "                       _acts += 1\n"
752                         "                       case " << A() << "[_acts - 1]\n";
753                 ACTION_SWITCH();
754                 out <<
755                         "                       end # action switch\n"
756                         "               end\n"
757                         "       end\n"
758                         "       if _trigger_goto\n"
759                         "               next\n"
760                         "       end\n";
761         }
762         
763         /* The again label. */
764         out <<
765                 "       end\n"
766                 "       if _goto_level <= _again\n";
767
768         if ( redFsm->anyToStateActions() ) {
769                 out <<
770                         "       _acts = " << TSA() << "["  << CS() << "]\n"
771                         "       _nacts = " << A() << "[_acts]\n"
772                         "       _acts += 1\n"
773                         "       while _nacts > 0\n"
774                         "               _nacts -= 1\n"
775                         "               _acts += 1\n"
776                         "               case " << A() << "[_acts - 1]\n";
777                         TO_STATE_ACTION_SWITCH() <<
778                         "               end # to state action switch\n"
779                         "       end\n"
780                         "       if _trigger_goto\n"
781                         "               next\n"
782                         "       end\n";
783         }
784
785         if ( redFsm->errState != 0 ) {
786                 out << 
787                         "       if " << CS() << " == " << redFsm->errState->id << "\n"
788                         "               _goto_level = _out\n"
789                         "               next\n"
790                         "       end\n";
791         }
792
793         out << "        " << P() << " += 1\n";
794
795         if ( hasEnd ) {
796                 out << 
797                         "       if " << P() << " != " << PE() << "\n"
798                         "               _goto_level = _resume\n"
799                         "               next\n"
800                         "       end\n";
801         }
802         else {
803                 out <<
804                         "       _goto_level = _resume\n"
805                         "       next\n";
806         }
807
808         /* The test_eof label. */
809         out <<
810                 "       end\n"
811                 "       if _goto_level <= _test_eof\n";
812
813         if ( redFsm->anyEofTrans() || redFsm->anyEofActions() ) {
814                 out << 
815                         "       if " << P() << " == " << EOFV() << "\n";
816
817                 if ( redFsm->anyEofTrans() ) {
818                         out <<
819                                 "       if " << ET() << "[" << CS() << "] > 0\n"
820                                 "               _trans = " << ET() << "[" << CS() << "] - 1;\n"
821                                 "               _goto_level = _eof_trans\n"
822                                 "               next;\n"
823                                 "       end\n";
824                 }
825
826                 if ( redFsm->anyEofActions() ) {
827                         out <<
828                                 "       begin\n"
829                                 "       __acts = " << EA() << "[" << CS() << "]\n"
830                                 "       __nacts = " << A() << "[__acts]\n" << 
831                                 "       __acts += 1\n"
832                                 "       while ( __nacts > 0 ) \n"
833                                 "               __nacts -= 1\n"
834                                 "               __acts += 1\n"
835                                 "               case ( "<< A() << "[__acts-1] ) \n";
836                                 EOF_ACTION_SWITCH() <<
837                                 "               end\n"
838                                 "       end\n"
839                                 "       if _trigger_goto\n"
840                                 "               next\n"
841                                 "       end\n"
842                                 "       end\n";
843                 }
844
845                 out <<
846                         "       end\n";
847         }
848
849         out << 
850                 "       end\n"
851                 "       if _goto_level <= _out\n"
852                 "               break\n"
853                 "       end\n";
854
855         /* The loop for faking goto. */
856         out <<
857                 "       end\n";
858
859         /* Wrapping the execute block. */
860         out << 
861                 "       end\n";
862 }
863
864
865 /*
866  * Local Variables:
867  * mode: c++
868  * indent-tabs-mode: 1
869  * c-file-style: "bsd"
870  * End:
871  */