int rw_ruby_factor( Factor: indep::factor ) { switch Factor case [`first_token_char] { send Out "data\[ts\].ord" } case [tk_ident `[ expr `]] { send Out "[$Factor.tk_ident]\[ [rw_ruby_expr(Factor.expr)] \] } case [tk_ident `( expr `)] { send Out "[$Factor.tk_ident]( [rw_ruby_expr(Factor.expr)] ) } case [`< type `> `( expr `)] { send Out "( [rw_ruby_expr(Factor.expr)] ) } case [`( expr `)] { send Out "( [rw_ruby_expr(Factor.expr)] ) } case ['true'] { send Out 'true' } case ['false'] { send Out 'false' } case "'0'" { send Out '"0"[0].ord' } case "'a'" { send Out '"a"[0].ord' } case [`buffer] { send Out "buffer\[0..blen-1\].pack( \"c*\" )" } default { send Out [$Factor] } } void rw_ruby_type( Type: indep::type ) { if match Type [`int] { send Out "int" } elsif match Type [`bool] { send Out "bool" } elsif match Type [`char] { send Out "char" } elsif match Type [`ptr] { send Out "char *" } elsif match Type [`byte] { send Out "unsigned char" } } void rw_ruby_abs_expr( Expr: indep::abs_expr ) { if ( Expr.Op ) { send Out "[rw_ruby_abs_expr(Expr.E1)] [$Expr.Op] [rw_ruby_abs_expr( Expr.E2 )]" } else { rw_ruby_factor( Expr.factor ) } } void rw_ruby_expr( Expr: indep::expr ) { AbsExpr: indep::abs_expr = indep::abs_comparative( Expr.comparative ) rw_ruby_abs_expr( AbsExpr ) } void rw_ruby_opt_array( OptArr: indep::opt_arr ) { if OptArr.expr { send Out "\[[rw_ruby_expr( OptArr.expr )]\]" } } int rw_ruby_var_decl( VarDecl: indep::var_decl ) { OptArr: indep::opt_arr = VarDecl.opt_arr if OptArr.expr { send Out "[$VarDecl.tk_ident] = Array.new } else if match VarDecl.type [`bool] { send Out "[$VarDecl.tk_ident] = false } else { send Out "[$VarDecl.tk_ident] = 0 } } void rw_ruby_opt_sub( OptSub: indep::opt_sub ) { if ( OptSub.expr ) send Out "\[[rw_ruby_expr(OptSub.expr)]\]" } int rw_ruby_expr_stmt( ExprStmt: indep::expr_stmt ) { if match ExprStmt [tk_ident opt_sub `= expr `;] { send Out "[$ExprStmt.tk_ident rw_ruby_opt_sub(ExprStmt.opt_sub)] = [rw_ruby_expr(ExprStmt.expr)]; } else if match ExprStmt [expr `;] { send Out "[rw_ruby_expr(ExprStmt.expr)]; } } int rw_ruby_if_stmt( IfStmt: indep::if_stmt ) { send Out "if ( [rw_ruby_expr( IfStmt.expr )] ) " [rw_ruby_stmt_list( IfStmt._repeat_stmt )] if ( IfStmt.opt_else._repeat_stmt ) { send Out "else " [rw_ruby_stmt_list( IfStmt.opt_else._repeat_stmt )] } send Out "end } int rw_ruby_print_stmt( Stmt: indep::print_stmt ) { if match Stmt [`print_int expr `;] { send Out "print( [rw_ruby_expr(Stmt.expr)] ); } else if match Stmt [`print_buf `;] { send Out "print( buffer\[0..blen-1\].pack( \"c*\" ) ); } else if match Stmt [`print_str expr `;] { send Out "print( [rw_ruby_expr( Stmt.expr )] ); } else if match Stmt [`print_token `;] { send Out "_m = data\[ts..te-1\]; "print( _m ); } } int rw_ruby_ragel_stmt( Stmt: indep::ragel_stmt ) { send Out [$Stmt] } void rw_ruby_buf_stmt( BufStmt: indep::buf_stmt ) { switch BufStmt case [`buf_clear `( `) `;] { send Out " blen = 0; } case [`buf_append `( `) `;] { send Out " buffer\[blen\] = fc; " blen += 1; } } int rw_ruby_stmt( Stmt: indep::stmt ) { switch Stmt case [var_decl] rw_ruby_var_decl( Stmt.var_decl ) case [expr_stmt] rw_ruby_expr_stmt( Stmt.expr_stmt ) case [if_stmt] rw_ruby_if_stmt( Stmt.if_stmt ) case [print_stmt] rw_ruby_print_stmt( Stmt.print_stmt ) case [buf_stmt] rw_ruby_buf_stmt( Stmt.buf_stmt ) case [ragel_stmt] rw_ruby_ragel_stmt( Stmt.ragel_stmt ) } void rw_ruby_stmt_list( StmtList: indep::stmt* ) { for Stmt: indep::stmt in repeat( StmtList ) rw_ruby_stmt( Stmt ) } int rw_ruby_action_block( ActionBlock: indep::action_block ) { Out = new parser() if match ActionBlock [`{ stmt* `}] { send Out "{[rw_ruby_stmt_list( ActionBlock._repeat_stmt )]} } else if match ActionBlock [`{ expr `}] { send Out "{[rw_ruby_expr( ActionBlock.expr )]} } send Out [] eos } void rw_ruby( Output: stream ) { Section: indep::section = RagelTree.section # Lowercase the machine name for MachineName: ragel::machine_name in Section { for Word: ragel::word in MachineName { Word.data = tolower( Word.data ) } } MachineName = ragel::machine_name in Section # Rewrite the action blocks. for Action: ragel::action_block in Section { # Reparse as lang-independent code. parse IndepActionBlock: indep::action_block[$Action] if ( !IndepActionBlock ) { print( error, '\n', Action ) exit(1) } rw_ruby_action_block( IndepActionBlock ) # Reparse back to ragel action block. Action = parse ragel::action_block[$Out->tree] if ( !Action ) { print( error, '\n' ) exit(1) } } send Output "# "# @LANG: ruby "# @GENERATED: true if ProhibitGenflags { send Output "# @PROHIBIT_GENFLAGS:[ProhibitGenflags] } send Output "# " send Output " "[Section] " "%% write data; " "def run_machine( data ) " p = 0 " pe = data.length " eof = data.length " cs = 0; " _m = "" " _a = "" " nfa_len = 0 " nfa_count = 0 " nfa_bp_state = Array.new() " nfa_bp_p = Array.new() " buffer = Array.new " blen = 0 Init: indep::stmt* = RagelTree.Init for Stmt: indep::stmt in Init { if match Stmt [Decl: var_decl] { Out = new parser() rw_ruby_var_decl( Decl ) send Out [] eos send Output [Out->tree] } } for Stmt: indep::stmt in Init { if match Stmt [ExprStmt: expr_stmt] { Out = new parser() rw_ruby_expr_stmt( ExprStmt ) send Out [] eos send Output [Out->tree] } } send Output " %% write init; " %% write exec; " if cs >= [MachineName.mn_word]_first_final " puts \"ACCEPT\" " else " puts \"FAIL\" " end "end " send Output "inp = \[ NR: int = 0 for InputString: indep::input_string in RagelTree { send Output [^InputString ",\n"] NR = NR + 1 } send Output "\] " send Output "inplen = [NR] " send Output "inp.each { |str| run_machine(str) } " }