summaryrefslogtreecommitdiff
path: root/test/langtrans_c.sh
blob: 2523b0a6216732258555e207ec32e02fc2fb1b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/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`

# Make a temporary version of the test case using the C language translations.
sed -n '/\/\*/,/\*\//d;p' $file | txl -q stdin langtrans_c.txl > $file.pr

needs_eof=`sed '/@NEEDS_EOF/s/^.*$/yes/p;d' $file`
if [ "$needs_eof" != 'yes' ]; then
	needs_eof=`sed -n '/\/\*/,/\*\//d;p' $file | txl -q stdin checkeofact.txl`
fi

# Begin writing out the test case.
cat << EOF
/*
 * @LANG: c
 * @GENERATED: yes
EOF

grep '@ALLOW_GENFLAGS:' $file
grep '@ALLOW_MINFLAGS:' $file

cat << EOF
 */
#include <string.h>
#include <stdio.h>
EOF

# Write the data declarations
sed -n '/^%%$/q;p' $file.pr

# Write out the machine specification.
sed -n '/^%%{$/,/^}%%/p' $file.pr

# Write out the init and execute routines.
cat << EOF
int cs;
%% write data;
void init()
{
EOF

sed -n '0,/^%%$/d; /^%%{$/q; {s/^/\t/;p}' $file.pr

cat << EOF
	%% write init;
}

void exec( char *data, int len )
{
	char *p = data;
	char *pe = data + len;
EOF

[ "$needs_eof" = "yes" ] && echo "char *eof = pe;"

cat << EOF
	%% write exec;
}

void finish( )
{
	if ( cs >= ${machine}_first_final )
		printf( "ACCEPT\\n" );
	else
		printf( "FAIL\\n" );
}
EOF

# Write out the test data.
sed -n '0,/\/\* _____INPUT_____/d; /_____INPUT_____ \*\//q; p;' $file | awk '
BEGIN {
	print "char *inp[] = {"
}
{
	print "	" $0 ","
}
END {
	print "};"
	print ""
	print "int inplen = " NR ";"
}'

# Write out the main routine.
cat << EOF

int main( )
{
	int i;
	for ( i = 0; i < inplen; i++ ) {
		init();
		exec( inp[i], strlen(inp[i]) );
		finish();
	}
	return 0;
}
#ifdef _____OUTPUT_____
EOF

# Write out the expected output.
sed -n '0,/\/\* _____OUTPUT_____/d; /_____OUTPUT_____ \*\//q; p;' $file
echo "#endif"

# Don't need this language-specific file anymore.
rm $file.pr