#! /bin/sh
# This file is part of GNU epsilon, a functional language implementation

# Copyright (C) 2002, 2003 Luca Saiu

# GNU epsilon is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 2, or (at your
# option) any later version.

# GNU epsilon is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with epsilon; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.


./make_instructions_h
./make_all_scheme_instructions

# Write the first constant part:
echo ";;; This file is automatically generated. Do not edit." > eam.scm
echo >> eam.scm
cat eam_scm.head >> eam.scm

cd c_instructions
for CATEGORY in *; do
  if test $CATEGORY != "CVS"; then
    echo >> ../eam.scm
    echo ";;; Instructions of category $CATEGORY:" >> ../eam.scm
    cd $CATEGORY
    for I in *; do
      if test $I != "CVS" && test $I != "nln" ; then
	# Output the instruction definition:
	echo "(defmacro $I (instruction-pointer " >> ../../eam.scm

	# The names and numbers of parameters depend on $CATEGORY:
	if test $CATEGORY = nullary; then
	  echo ")" >> ../../eam.scm
	elif test $CATEGORY = integer; then
	  echo "integer-parameter-1)" >> ../../eam.scm
	elif test $CATEGORY = integer_integer; then
	  echo "integer-parameter-1 integer-parameter-2)" \
	      >> ../../eam.scm
	elif test $CATEGORY = integer_integer_integer; then
	  echo "integer-parameter-1 integer-parameter-2 integer-parameter-3)"\
	      >> ../../eam.scm
	elif test $CATEGORY = float; then
	  echo "float-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = string; then
	  echo "string-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = string_string; then
	  echo "string-parameter-1 string-parameter-2)" \
	      >> ../../eam.scm
	elif test $CATEGORY = register; then
	  echo "register-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = register_register; then
	  echo "register-parameter-1 register-parameter-2)" \
	      >> ../../eam.scm
	elif test $CATEGORY = register_register_integer; then
	  echo "register-parameter-1 register-parameter-2 integer-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = register_integer; then
	  echo "register-parameter-1 integer-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = register_integer_integer; then
	  echo "register-parameter-1 integer-parameter-1 integer-parameter-2)" \
	      >> ../../eam.scm
	elif test $CATEGORY = register_integer_register; then
	  echo "register-parameter-1 integer-parameter-1 register-parameter-2)" \
	      >> ../../eam.scm
	elif test $CATEGORY = register_register_register; then
	  echo "register-parameter-1 register-parameter-2 register-parameter-3)" \
	      >> ../../eam.scm
	elif test $CATEGORY = label; then
	  echo "label-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = exception; then
	  echo "exception-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = exception_label; then
	  echo "exception-parameter-1 label-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = global; then
	  echo "global-parameter-1)" \
	      >> ../../eam.scm
	elif test $CATEGORY = integer_label; then
	  echo "integer-parameter-1 label-parameter-1)" \
	      >> ../../eam.scm
	else
	  echo make_eam_scm:unknown category $CATEGORY > /dev/stderr
	  exit -1
	fi

	# Output the instruction definition, or exit if it does not exist:
	if test ! -r ../../all_scheme_instructions/$I; then
	  echo "make_eam_scm: $I is implemented in C but not in Scheme" > /dev/stderr
	  echo "    ; no Scheme definition"   >> ../../eam.scm
	  echo "    (display \"$I is not implemented\")" >> ../../eam.scm
	  echo "    (newline)" >> ../../eam.scm
	  echo "    (1+ instruction-pointer)" >> ../../eam.scm
	  #exit -1
	else
	  cat ../../all_scheme_instructions/$I >> ../../eam.scm
	fi

	# Close definition:
	echo ")" >> ../../eam.scm
      fi
    done # For each instruction $I in $CATEGORY
    cd ..
  fi
done # For each $CATEGORY
cd ..

# Write the trailing part:
cat eam_scm.tail >> eam.scm
