#!/bin/sh

# first run C tests as simpvir bytecode

echo vm >compilation-mode.txt

cat src/c/abs.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >result

cat src/c/bitwise.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

cat src/c/case.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

cat src/c/fib.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

cat src/c/for.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

cat src/c/forward.c | \
  ../bin/shmall >x
#filter out forward function definitions
OS=`uname -s`
if [ $OS = OpenBSD -o $OS = FreeBSD ]; then
  REVERSE="tail -r"
else
  REVERSE=tac
fi
cat x | $REVERSE | sed -e '/^__forward/{N;N;N;d;}' | $REVERSE >xx
mv xx x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

cat src/c/memcpy32.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

cat src/c/repeat.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

cat src/c/sieve.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

cat src/c/while.c | \
  ../bin/shmall >x
cat ../bin/schimper.txt compilation-mode.txt x | \
  ../bin/schant-int >xx
cat ../bin/simpvir-assembler.txt xx | \
  ../bin/schant-int >x
cat x | ../bin/simpvir >>result

diff -w -q result reference/reference-c.txt
if [ $? = 0 ]; then
  echo "simpvir shmall C tests pass"
else
  echo "simpvir shmall C tests fail"
  diff -w result reference/reference-c.txt # report failures
fi

rm x xx

# now run C tests as native executable

cd ..

cp tests/src/c/abs.c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >result

cp tests/src/c/bitwise.c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

cp tests/src/c/"case".c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

cp tests/src/c/fib.c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

cp tests/src/c/"for".c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

cp tests/src/c/forward.c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

cp tests/src/c/memcpy32.c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

cp tests/src/c/repeat.c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

cp tests/src/c/sieve.c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

cp tests/src/c/"while".c y.c
./compile-with-shmall-to-native.sh y object 100000
./y >>result

diff -w -q result tests/reference/reference-c.txt
if [ $? = 0 ]; then
  echo "native shmall C tests pass"
else
  echo "native shmall C tests fail"
fi

rm y.c y result

cd tests

# finally run C tests as C to scheme tests

cat src/c/abs.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >result

cat src/c/bitwise.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

cat src/c/case.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

cat src/c/fib.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

cat src/c/for.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

cat src/c/forward.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

cat src/c/memcpy32.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

cat src/c/repeat.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

cat src/c/sieve.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

cat src/c/while.c | ../bin/shmall >x.scm
cat ../bin/schant-compiler.txt ../src/scheme/schimper-rt.scm x.scm | \
  ../bin/schant-int >x
cat x | ../bin/schant-int >>result

diff -w -q result reference/reference-c.txt
if [ $? = 0 ]; then
  echo "C to scheme shmall C tests pass"
else
  echo "C to scheme shmall C tests fail"
fi

#rm x x.scm result
