1# Copyright (C) 2006-2009, Parrot Foundation. 2 3=head1 TITLE 4 5tgc.pir - The TGE rules compiler 6 7=head1 SYNOPSIS 8 9 > ./parrot compilers/tge/tgc.pir [OPTIONS] FILE 10 11=head1 DESCRIPTION 12 13This program takes a tree grammar, specified in B<FILE>, and compiles it 14into the PIR code needed to execute that grammar. This PIR code is then 15suitable for inclusion or compilation into other larger programs. 16 17=head1 OPTIONS 18 19=over 4 20 21=item --output OUTFILE 22 23Send the output to OUTFILE. By default, output is directed to STDOUT. 24 25=back 26 27=cut 28 29 30.sub "main" :main 31 .param pmc args 32 .local string prog 33 .local string infile, outfile 34 35 load_bytecode "TGE.pbc" 36 load_bytecode "Getopt/Obj.pbc" 37 38 # Grab program name for error reporting 39 prog = shift args 40 41 # Sanity check parameters 42 $I0 = args 43 unless $I0 >= 1 goto ERR_TOO_FEW_ARGS 44 45 # Grab the final argument 46 infile = pop args 47 48 49 # Process command line options 50 .local pmc getopts 51 getopts = new ["Getopt";"Obj"] 52 getopts."notOptStop"(1) 53 push getopts, "output|o=s" 54 push getopts, "help|h" 55 .local pmc opts 56 opts = getopts."get_options"( args ) 57 58 .local string help 59 help = opts['help'] 60 if help goto USAGE 61 62 .local pmc outfh 63 64 .local int ck_output 65 ck_output = exists opts['output'] 66 if ck_output goto OUTPUT_FILE 67 68 OUTPUT_STDOUT: 69 $P0 = getinterp 70 outfh = $P0.'stdout_handle'() 71 goto OUTPUT_DONE 72 73 OUTPUT_FILE: 74 outfile = opts['output'] 75 outfh = new ['FileHandle'] 76 outfh.'open'(outfile, 'w') 77 unless outfh goto ERR_NO_OUTFILE 78 79 OUTPUT_DONE: 80 81 # Read grammar file and compile here 82 .local pmc infh 83 infh = new ['FileHandle'] 84 infh.'open'(infile, 'r') 85 unless infh goto ERR_NO_INFILE 86 87 .local string source 88 source = infh.'read'(65535) 89 infh.'close'() 90 91 .local pmc grammar 92 grammar = new ['TGE';'Compiler'] 93 94 .local string compiled_source 95 compiled_source = grammar.'precompile'(source, infile) 96 print outfh, compiled_source 97 unless ck_output goto END 98 99 # Close the output file and check result 100 $I0 = outfh.'close'() 101 unless $I0 goto END 102 die 'Error: close output failed' 103 104 goto END 105 106 USAGE: 107 $P0 = getinterp 108 $P1 = $P0.'stderr_handle'() 109 $P1.'print'("Usage: ") 110 $P1.'print'(prog) 111 $P1.'print'(" [OPTIONS] FILE\n") 112 $P1.'print'(<<"OPTIONS") 113 Options: 114 --output=OUTFILE -- redirect output to OUTFILE 115 --help -- print this message 116OPTIONS 117 exit 1 118 119 ERR_TOO_FEW_ARGS: 120 $P0 = getinterp 121 $P1 = $P0.'stderr_handle'() 122 $P1.'print'("Error: too few arguments\n\n") 123 goto USAGE 124 125 ERR_NO_INFILE: 126 $P0 = getinterp 127 $P1 = $P0.'stderr_handle'() 128 $P1.'print'("Error: file not found: ") 129 $P1.'print'(infile) 130 $P1.'print'("\n\n") 131 goto USAGE 132 133 ERR_NO_OUTFILE: 134 $P0 = getinterp 135 $P1 = $P0.'stderr_handle'() 136 $P1.'print'("Error: file not found: ") 137 $P1.'print'(outfile) 138 $P1.'print'("\n\n") 139 goto USAGE 140 141 END: 142.end 143 144# Local Variables: 145# mode: pir 146# fill-column: 100 147# End: 148# vim: expandtab shiftwidth=4 ft=pir: 149