--- /dev/null
+#!/bin/bash
+#
+
+file=$1
+
+[ -f $file ] || exit 1
+
+# Get the machine name.
+machine=`sed -n 's/^[\t ]*machine[\t ]*\([a-zA-Z_0-9]*\)[\t ]*;[\t ]*$/\1/p' \
+ $file | tr '[A-Z]' '[a-z]'`
+
+# Make a temporary version of the test case using the Ruby language translations.
+sed -n '/\/\*/,/\*\//d;p' $file | txl -q stdin langtrans_ruby.txl > $file.pr
+
+# Begin writing out the test case.
+cat << EOF
+#
+# @LANG: ruby
+# @ALLOW_GENFLAGS: -T0
+# @GENERATED: yes
+#
+
+EOF
+
+# Write out the machine specification.
+sed -n '/^%%{$/,/^}%%/{s/^/\t/;p}' $file.pr
+
+# Write out the init and execute routines.
+cat << EOF
+
+ %% write data;
+
+ def run_machine( data )
+ p = 0
+ pe = data.length
+ cs = 0;
+EOF
+
+# Write the data declarations
+sed -n '/^%%$/q;{s/^/\t/;p}' $file.pr
+
+# Write the data initializations
+sed -n '0,/^%%$/d; /^%%{$/q; {s/^/\t\t/;p}' $file.pr
+
+cat << EOF
+
+ %% write init;
+ %% write exec;
+ %% write eof;
+ if cs >= ${machine}_first_final
+ puts "ACCEPT"
+ else
+ puts "FAIL"
+ end
+ end
+
+EOF
+
+# Write out the test data.
+sed -n '0,/\/\* _____INPUT_____/d; /_____INPUT_____ \*\//q; p;' $file | awk '
+BEGIN {
+ print " inp = ["
+}
+{
+ print " " $0 ","
+}
+END {
+ print " ]"
+ print ""
+ print " inplen = " NR ";"
+}'
+
+
+# Write out the main routine.
+cat << EOF
+
+ inp.each { |str|
+ run_machine(str)
+ }
+
+EOF
+
+# Write out the expected output.
+echo "=begin _____OUTPUT_____"
+
+sed -n '/\/\* _____OUTPUT_____/,/_____OUTPUT_____ \*\//{/_____OUTPUT_____/d;p;};' $file
+
+echo "=end _____OUTPUT_____"
+
+# Don't need this language-specific file anymore.
+#rm $file.pr
--- /dev/null
+include "testcase.txl"
+
+keys
+ 'boolean 'new
+end keys
+
+
+define ruby_statements
+ [repeat ruby_lang_stmt]
+end define
+
+define ruby_lang_stmt
+ [al_ragel_stmt]
+ | [ruby_expr_stmt]
+ | [ruby_if_stmt]
+ | [EX] 'do [IN] [NL] [ruby_statements] [EX] 'end [IN] [NL]
+end define
+
+define ruby_type_decl
+ [al_type_decl]
+ | 'boolean
+end define
+
+define ruby_expr_stmt
+ [ruby_expr] '; [NL]
+end define
+
+define ruby_expr
+ [ruby_term] [repeat ruby_expr_extend]
+end define
+
+define ruby_expr_extend
+ [al_expr_op] [ruby_term]
+end define
+
+define ruby_term
+ [al_term]
+ | [stringlit] [union]
+ | [id] [repeat ruby_dot_id]
+ | [SPOFF] [id] [repeat ruby_dot_id] '( [SPON] [ruby_args] ')
+ | 'new [ruby_type_decl] [union]
+end define
+
+define ruby_dot_id
+ '. [id]
+end define
+
+define ruby_args
+ [list ruby_expr]
+end define
+
+define ruby_sign
+ '- | '+
+end define
+
+define ruby_if_stmt
+ 'if [ruby_expr] [NL] [IN]
+ [ruby_statements] [EX]
+ [opt ruby_else]
+ 'end [NL]
+end define
+
+define ruby_else
+ 'else [NL] [IN]
+ [ruby_statements] [EX]
+end define
+
+define ruby_lang
+ [ruby_statements]
+ '%% [NL]
+ [ruby_statements]
+ [ragel_def]
+end define
+
+define program
+ [lang_indep]
+ | [ruby_lang]
+end define
+
+redefine al_host_block
+ '{ [NL] [IN] [al_statements] [EX] '} [NL]
+ | '{ [NL] [IN] [ruby_statements] [EX] '} [NL]
+end define
+
+function clearUnion Type [ruby_type_decl] Id [id]
+ replace [opt union]
+ Union [union]
+ import ArrayInits [ruby_statements]
+ Stmts [repeat ruby_lang_stmt]
+ export ArrayInits
+ Id '= 'new Type Union '; Stmts
+ by
+ '[]
+end function
+
+function initDecl1 Id [id] DeclType [al_type_decl]
+ deconstruct DeclType
+ 'bool
+ replace [repeat ruby_lang_stmt]
+ by
+ Id '= 'false ';
+end function
+
+function initDecl2 Id [id] DeclType [al_type_decl]
+ deconstruct DeclType
+ 'char
+ replace [repeat ruby_lang_stmt]
+ by
+ Id '= ''c' ';
+end function
+
+function initDecl3 Id [id] DeclType [al_type_decl]
+ deconstruct DeclType
+ 'int
+ replace [repeat ruby_lang_stmt]
+ by
+ Id '= '0 ';
+end function
+
+function initDecl4 Id [id] DeclType [al_type_decl]
+ deconstruct DeclType
+ 'ptr
+ replace [repeat ruby_lang_stmt]
+ by
+ Id '= '-1 ';
+end function
+
+function alStmtToRuby1 AlStmt [action_lang_stmt]
+ deconstruct AlStmt
+ VarDecl [al_variable_decl]
+ deconstruct VarDecl
+ Type [al_type_decl] Id [id] OptUnion [opt union] ';
+ construct RubyType [ruby_type_decl]
+ Type
+ replace [repeat ruby_lang_stmt]
+ by
+ _ [initDecl1 Id Type] [initDecl2 Id Type]
+ [initDecl3 Id Type] [initDecl4 Id Type]
+end function
+
+function alExprExtendToRuby AlExprExtend [repeat al_expr_extend]
+ deconstruct AlExprExtend
+ Op [al_expr_op] Term [al_term] Rest [repeat al_expr_extend]
+ construct RubyRest [repeat ruby_expr_extend]
+ _ [alExprExtendToRuby Rest]
+ replace [repeat ruby_expr_extend]
+ by
+ Op Term RubyRest
+end function
+
+% Note: this doesn't go into the ( al_expr ) form of al_term.
+function alExprToRuby AlExpr [al_expr]
+ deconstruct AlExpr
+ ALTerm [al_term] AlExprExtend [repeat al_expr_extend]
+ construct RubyExprExtend [repeat ruby_expr_extend]
+ _ [alExprExtendToRuby AlExprExtend]
+ construct Result [opt ruby_expr]
+ ALTerm RubyExprExtend
+ replace [opt ruby_expr]
+ by
+ Result
+end function
+
+function alStmtToRuby2 AlStmt [action_lang_stmt]
+ deconstruct AlStmt
+ AlExpr [al_expr] ';
+ construct OptRubyExpr [opt ruby_expr]
+ _ [alExprToRuby AlExpr]
+ deconstruct OptRubyExpr
+ RubyExpr [ruby_expr]
+ replace [repeat ruby_lang_stmt]
+ by
+ RubyExpr ';
+end function
+
+function liftBlock
+ replace [repeat ruby_lang_stmt]
+ 'do Block [repeat ruby_lang_stmt] 'end
+ by
+ Block
+end function
+
+function alOptElseRuby AlOptElse [opt al_else]
+ deconstruct AlOptElse
+ 'else
+ AlSubStmt [action_lang_stmt]
+ construct AlSubStmts [repeat action_lang_stmt]
+ AlSubStmt
+ construct RubySubStmts [repeat ruby_lang_stmt]
+ _ [alToRuby AlSubStmts]
+ deconstruct RubySubStmts
+ RubySubStmt [ruby_lang_stmt]
+ replace [opt ruby_else]
+ by
+ 'else
+ RubySubStmts [liftBlock]
+end function
+
+function alStmtToRuby3 AlStmt [action_lang_stmt]
+ deconstruct AlStmt
+ 'if '( AlExpr [al_expr] ')
+ AlSubStmt [action_lang_stmt]
+ AlOptElse [opt al_else]
+ construct OptRubyExpr [opt ruby_expr]
+ _ [alExprToRuby AlExpr]
+ deconstruct OptRubyExpr
+ RubyExpr [ruby_expr]
+ construct AlSubStmts [repeat action_lang_stmt]
+ AlSubStmt
+ construct RubySubStmts [repeat ruby_lang_stmt]
+ _ [alToRuby AlSubStmts]
+ construct OptRubyElse [opt ruby_else]
+ _ [alOptElseRuby AlOptElse]
+ replace [repeat ruby_lang_stmt]
+ by
+ 'if RubyExpr
+ RubySubStmts [liftBlock]
+ OptRubyElse
+ 'end
+end function
+
+function alStmtToRuby4a AlStmt [action_lang_stmt]
+ deconstruct AlStmt
+ 'printi Id [id] ';
+ replace [repeat ruby_lang_stmt]
+ by
+ 'print '( Id ') ';
+end function
+
+function alStmtToRuby4b AlStmt [action_lang_stmt]
+ deconstruct AlStmt
+ 'prints String [stringlit] ';
+ replace [repeat ruby_lang_stmt]
+ by
+ 'print '( String ') ';
+end function
+
+function alStmtToRuby5 AlStmt [action_lang_stmt]
+ deconstruct AlStmt
+ '{ AlSubStmts [repeat action_lang_stmt] '}
+ construct RubySubStmts [repeat ruby_lang_stmt]
+ _ [alToRuby AlSubStmts]
+ replace [repeat ruby_lang_stmt]
+ by
+ 'do RubySubStmts 'end
+end function
+
+function alStmtToRuby6 AlStmt [action_lang_stmt]
+ deconstruct AlStmt
+ RagelStmt [al_ragel_stmt]
+ replace [repeat ruby_lang_stmt]
+ by
+ RagelStmt
+end function
+
+rule fixCharLit
+ replace $ [al_term]
+ CharLit [charlit]
+ construct BaseId [id]
+ 'id
+ construct Id [id]
+ BaseId [unquote CharLit]
+ construct EmptyString [stringlit]
+ '""
+ construct Repl [stringlit]
+ EmptyString [quote Id]
+ by
+ Repl '[0]
+end rule
+
+
+function alToRuby AlStmts [repeat action_lang_stmt]
+ deconstruct AlStmts
+ FirstStmt [action_lang_stmt] Rest [repeat action_lang_stmt]
+ construct RubyFirst [repeat ruby_lang_stmt]
+ _
+ [alStmtToRuby1 FirstStmt]
+ [alStmtToRuby2 FirstStmt]
+ [alStmtToRuby3 FirstStmt]
+ [alStmtToRuby4a FirstStmt]
+ [alStmtToRuby4b FirstStmt]
+ [alStmtToRuby5 FirstStmt]
+ [alStmtToRuby6 FirstStmt]
+ [fixCharLit]
+ construct RubyRest [repeat ruby_lang_stmt]
+ _ [alToRuby Rest]
+ replace [repeat ruby_lang_stmt]
+ by
+ RubyFirst [. RubyRest]
+end function
+
+rule actionTransRuby
+ replace [al_host_block]
+ '{ AlStmts [repeat action_lang_stmt] '}
+ construct RubyStmts [repeat ruby_lang_stmt]
+ _ [alToRuby AlStmts]
+ by
+ '{ RubyStmts '}
+end rule
+
+rule lowercaseMachine
+ replace $ [machine_stmt]
+ 'machine Id [id] ';
+ by
+ 'machine Id [tolower] ';
+end rule
+
+function langTransRuby
+ replace [program]
+ Definitions [repeat action_lang_stmt]
+ '%%
+ Initializations [repeat action_lang_stmt]
+ RagelDef [ragel_def]
+ construct RubyDefinitions [repeat ruby_lang_stmt]
+ _ [alToRuby Definitions]
+ construct RubyInitializations [repeat ruby_lang_stmt]
+ _ [alToRuby Initializations]
+ construct NewRagelDef [ragel_def]
+ RagelDef [actionTransRuby] [lowercaseMachine]
+ import ArrayInits [ruby_statements]
+ ArrayInitStmts [repeat ruby_lang_stmt]
+ by
+ RubyDefinitions
+ '%%
+ ArrayInitStmts [. RubyInitializations]
+ NewRagelDef
+end function
+
+function main
+ replace [program]
+ P [program]
+ export ArrayInits [ruby_statements]
+ _
+ by
+ P [langTransRuby]
+end function