#!/bin/sh

# test the simpvir vm at the bytecode level

VM=../bin/simpvir

# opcodes
PUSH=7
ADD=9
DIV=12
ABS=14
SHL=16
SHR=17
BITOR=18
BITAND=19
BITNOT=20
LT=25
NOT=30
AND=31
OR=32
READ_CHAR=35
EOF_OBJECT=36
DISPLAY=37
NEWLINE=38
HALT=39

ARG1=8
ARG2=7

echo >x.x

echo "add" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $ADD $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "quotient" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $DIV $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "lt" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $LT $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "bitwise-and" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $BITAND $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "bitwise-or" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $BITOR $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "bitwise-not" >>x.x
echo "6 $PUSH $ARG1 $BITNOT $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "abs" >>x.x
echo "6 $PUSH $ARG1 $ABS $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "shl" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $SHL $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "shr" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $SHR $DISPLAY $NEWLINE $HALT" | $VM >>x.x

ARG1=1
ARG2=0

echo "and" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $AND $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "or" >>x.x
echo "8 $PUSH $ARG2 $PUSH $ARG1 $OR $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "not" >>x.x
echo "6 $PUSH $ARG1 $NOT $DISPLAY $NEWLINE $HALT" | $VM >>x.x

echo "read_char, eof_object" >>x.x
echo "25 $READ_CHAR $EOF_OBJECT $DISPLAY $NEWLINE $DISPLAY $NEWLINE $READ_CHAR $EOF_OBJECT $DISPLAY $NEWLINE $DISPLAY $NEWLINE $READ_CHAR $EOF_OBJECT $DISPLAY $NEWLINE $DISPLAY $NEWLINE $READ_CHAR $EOF_OBJECT $DISPLAY $NEWLINE $DISPLAY $NEWLINE $HALT 0" | \
  $VM >>x.x

diff -b reference/reference-simpvir.txt x.x
if [ $? = 0 ]; then
  echo $executable simpvir tests pass
else
  echo $executable simpvir tests failed
fi

rm -f x.x
