int rw_d_factor( Factor: indep::factor ) { if match Factor [`first_token_char] { send Out "ts\[0\]" } else if match Factor [tk_ident `[ expr `]] { send Out "[$Factor.tk_ident]\[ [rw_d_expr(Factor.expr)] \] } else if match Factor [tk_ident `( expr `)] { send Out "[$Factor.tk_ident]( [rw_d_expr(Factor.expr)] ) } elsif match Factor [`< type `> `( expr `)] { send Out "cast( [rw_d_type(Factor.type)] ) ( [rw_d_expr(Factor.expr)] ) } elsif match Factor [`( expr `)] { send Out "( [rw_d_expr(Factor.expr)] ) } elsif match Factor ['true'] { send Out '1' } elsif match Factor ['false'] { send Out '0' } elsif match Factor [`buffer] { send Out "cast(string)buffer\[0..blen\]" } else { send Out [$Factor] } } void rw_d_type( Type: indep::type ) { if match Type [`int] { send Out "int" } elsif match Type [`bool] { send Out "int" } elsif match Type [`char] { send Out "char" } elsif match Type [`ptr] { send Out "const(char) *" } elsif match Type [`byte] { send Out "unsigned char" } } void rw_d_abs_expr( Expr: indep::abs_expr ) { if ( Expr.Op ) { send Out "[rw_d_abs_expr(Expr.E1)] [$Expr.Op] [rw_d_abs_expr( Expr.E2 )]" } else { rw_d_factor( Expr.factor ) } } void rw_d_expr( Expr: indep::expr ) { AbsExpr: indep::abs_expr = indep::abs_comparative( Expr.comparative ) rw_d_abs_expr( AbsExpr ) } void rw_d_opt_array( OptArr: indep::opt_arr ) { if OptArr.expr { send Out "\[[rw_d_expr( OptArr.expr )]\]" } } int rw_d_var_decl( VarDecl: indep::var_decl ) { send Out "[rw_d_type( VarDecl.type )][rw_d_opt_array(VarDecl.opt_arr)] [$VarDecl.tk_ident]; } void rw_d_opt_sub( OptSub: indep::opt_sub ) { if ( OptSub.expr ) send Out "\[[rw_d_expr(OptSub.expr)]\]" } int rw_d_expr_stmt( ExprStmt: indep::expr_stmt ) { if match ExprStmt [tk_ident opt_sub `= expr `;] { send Out "[$ExprStmt.tk_ident rw_d_opt_sub(ExprStmt.opt_sub)] = [rw_d_expr(ExprStmt.expr)]; } else if match ExprStmt [expr `;] { send Out "[rw_d_expr(ExprStmt.expr)]; } } int rw_d_if_stmt( IfStmt: indep::if_stmt ) { send Out "if ( [rw_d_expr( IfStmt.expr )] ) "{ " [rw_d_stmt_list( IfStmt._repeat_stmt )] "} if ( IfStmt.opt_else._repeat_stmt ) { send Out "else { " [rw_d_stmt_list( IfStmt.opt_else._repeat_stmt )] "} } } int rw_d_print_stmt( Stmt: indep::print_stmt ) { if match Stmt [`print_int expr `;] { send Out "write( [rw_d_expr(Stmt.expr)] ); } else if match Stmt [`print_buf `;] { send Out "printf( \"%s\", &buffer ); } else if match Stmt [`print_str expr `;] { send Out "write( [rw_d_expr(Stmt.expr)] );" } else if match Stmt [`print_token `;] { send Out "write( ts\[0..(te - ts)\] );" } else if match Stmt [`print_off `;] { send Out "write( (p - &data\[0\]) );" } } void rw_d_buf_stmt( BufStmt: indep::buf_stmt ) { switch BufStmt case [`buf_clear `( `) `;] { send Out " blen = 0; } case [`buf_append `( `) `;] { send Out " buffer\[blen++\] = *p; " buffer\[blen\] = 0; } } int rw_d_ragel_stmt( Stmt: indep::ragel_stmt ) { send Out [$Stmt] } int rw_d_stmt( Stmt: indep::stmt ) { if match Stmt [var_decl] rw_d_var_decl( Stmt.var_decl ) else if match Stmt [expr_stmt] rw_d_expr_stmt( Stmt.expr_stmt ) else if match Stmt [if_stmt] rw_d_if_stmt( Stmt.if_stmt ) else if match Stmt [print_stmt] rw_d_print_stmt( Stmt.print_stmt ) else if match Stmt [buf_stmt] rw_d_buf_stmt( Stmt.buf_stmt ) else if match Stmt [ragel_stmt] rw_d_ragel_stmt( Stmt.ragel_stmt ) } void rw_d_stmt_list( StmtList: indep::stmt* ) { for Stmt: indep::stmt in repeat( StmtList ) rw_d_stmt( Stmt ) } int rw_d_action_block( ActionBlock: indep::action_block ) { Out = new parser() if match ActionBlock [`{ stmt* `}] { send Out "{[rw_d_stmt_list( ActionBlock._repeat_stmt )]} } else if match ActionBlock [`{ expr `}] { send Out "{[rw_d_expr( ActionBlock.expr )]} } send Out [] eos } void rw_d( Output: stream ) { send Output "/* " * @LANG: d if ProhibitGenflags { send Output " * @PROHIBIT_GENFLAGS:[ProhibitGenflags] } send Output " * @GENERATED: true " */ " "import std.stdio; "import core.stdc.stdio; "import std.string; " "class [MachineName.mn_word] "{ Init: indep::stmt* = RagelTree.Init for Stmt: indep::stmt in Init { if match Stmt [Decl: var_decl] { Out = new parser() rw_d_var_decl( Decl ) send Out [] eos send Output [Out->tree] } } # Colm bug. Section: indep::section = RagelTree.section for Action: ragel::action_block in Section { # Reparse as lang-independent code. parse IndepActionBlock: indep::action_block[$Action] rw_d_action_block( IndepActionBlock ) # Reparse back to ragel action block. Action = parse ragel::action_block[$Out->tree] } send Output " "[Section] " "%% write data; "int cs; "int blen; "char\[1024\] buffer; "int nfa_len, nfa_count; "struct nfa_s { int state; const(char)* p; }; "nfa_s\[20\] nfa_bp; " "void init() "{ for Stmt: indep::stmt in Init { if match Stmt [ExprStmt: expr_stmt] { Out = new parser() rw_d_expr_stmt( ExprStmt ) send Out [] eos send Output [Out->tree] } } send Output " %% write init; "} send Output "void exec( const(char)\[\] data ) "{ " const(char) *p = data.ptr; " const(char) *pe = data.ptr + data.length; if NeedsEof { send Output " const(char) *eof = pe; } send Output " char\[\] _s; " " %% write exec; "} " "void finish( ) "{ " if ( cs >= [MachineName.mn_word]_first_final ) " writefln( \"ACCEPT\" ); " else " writefln( \"FAIL\" ); "} send Output ~static const char[][] inp = [ NR: int = 0 for InputString: indep::input_string in RagelTree { send Output [^InputString ",\n"] NR = NR + 1 } send Output "\]; " "int inplen = [NR]; " "} send Output "int main() "{ " [MachineName.mn_word] m = new [MachineName.mn_word](); " int i; " for ( i = 0; i < m.inplen; i++ ) { " m.init(); " m.exec( m.inp\[i\] ); " m.finish(); " } " return 0; "} " }