Delete the temporary file.
[external/ragel.git] / test / langtrans_c.sh
1 #!/bin/bash
2 #
3
4 file=$1
5
6 [ -f $file ] || exit 1
7
8 # Get the machine name.
9 machine=`sed -n 's/^[\t ]*machine[\t ]*\([a-zA-Z_0-9]*\)[\t ]*;[\t ]*$/\1/p' $file`
10
11 # Make a temporary version of the test case using the C language translations.
12 sed -n '/\/\*/,/\*\//d;p' $file | txl -q stdin langtrans_c.txl > $file.pr
13
14 # Begin writing out the test case.
15 cat << EOF
16 /*
17  * @LANG: c
18  * @GENERATED: yes
19  */
20 #include <string.h>
21 #include <stdio.h>
22 EOF
23
24 # Write the data declarations
25 sed -n '/^%%$/q;p' $file.pr
26
27 # Write out the machine specification.
28 sed -n '/^%%{$/,/^}%%/p' $file.pr
29
30 # Write out the init and execute routines.
31 cat << EOF
32 int cs;
33 %% write data;
34 void init()
35 {
36 EOF
37
38 sed -n '0,/^%%$/d; /^%%{$/q; {s/^/\t/;p}' $file.pr
39
40 cat << EOF
41         %% write init;
42 }
43
44 void exec( char *data, int len )
45 {
46         char *p = data;
47         char *pe = data + len;
48         %% write exec;
49 }
50
51 void finish( )
52 {
53         %% write eof;
54         if ( cs >= ${machine}_first_final )
55                 printf( "ACCEPT\\n" );
56         else
57                 printf( "FAIL\\n" );
58 }
59 EOF
60
61 # Write out the test data.
62 sed -n '0,/\/\* _____INPUT_____/d; /_____INPUT_____ \*\//q; p;' $file | awk '
63 BEGIN {
64         print "char *inp[] = {"
65 }
66 {
67         print " " $0 ","
68 }
69 END {
70         print "};"
71         print ""
72         print "int inplen = " NR ";"
73 }'
74
75 # Write out the main routine.
76 cat << EOF
77
78 int main( )
79 {
80         int i;
81         for ( i = 0; i < inplen; i++ ) {
82                 init();
83                 exec( inp[i], strlen(inp[i]) );
84                 finish();
85         }
86         return 0;
87 }
88 #ifdef _____OUTPUT_____
89 EOF
90
91 # Write out the expected output.
92 sed -n '0,/\/\* _____OUTPUT_____/d; /_____OUTPUT_____ \*\//q; p;' $file
93 echo "#endif"
94
95 # Don't need this language-specific file anymore.
96 rm $file.pr