(********************************************************************)
(*                                                                  *)
(*  act_act.s7i   Generate code for actions of the type action.     *)
(*  Copyright (C) 1990 - 1994, 2004 - 2015  Thomas Mertes           *)
(*                                                                  *)
(*  This file is part of the Seed7 compiler.                        *)
(*                                                                  *)
(*  This program 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 of  *)
(*  the License, or (at your option) any later version.             *)
(*                                                                  *)
(*  This program 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 this program; if not, write to the           *)
(*  Free Software Foundation, Inc., 51 Franklin Street,             *)
(*  Fifth Floor, Boston, MA  02110-1301, USA.                       *)
(*                                                                  *)
(********************************************************************)


const ACTION: ACE_CMP      is action "ACE_CMP";
const ACTION: ACE_CPY      is action "ACE_CPY";
const ACTION: ACE_EQ       is action "ACE_EQ";
const ACTION: ACE_GEN      is action "ACE_GEN";
const ACTION: ACE_HASHCODE is action "ACE_HASHCODE";
const ACTION: ACE_ICONV1   is action "ACE_ICONV1";
const ACTION: ACE_ICONV3   is action "ACE_ICONV3";
const ACTION: ACE_NE       is action "ACE_NE";
const ACTION: ACE_ORD      is action "ACE_ORD";
const ACTION: ACE_STR      is action "ACE_STR";
const ACTION: ACE_VALUE    is action "ACE_VALUE";


const proc: act_prototypes (inout file: c_prog) is func

  begin
    declareExtern(c_prog, "const_actEntryType aceGen (const const_striType);");
    declareExtern(c_prog, "intType            aceHashCode (const_actEntryType);");
    declareExtern(c_prog, "const_actEntryType aceIConv (intType);");
    declareExtern(c_prog, "intType            aceOrd (const_actEntryType);");
    declareExtern(c_prog, "striType           aceStr (const_actEntryType);");
    declareExtern(c_prog, "const_actEntryType aceValue (const const_objRefType);");
  end func;


const proc: process (ACE_CMP, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "ptrCmp(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ", ";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ACE_CPY, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  local
    var expr_type: c_param1 is expr_type.value;
    var expr_type: c_param3 is expr_type.value;
  begin
    process_expr(params[1], c_param1);
    c_param3.temp_num := c_param1.temp_num;
    process_expr(params[3], c_param3);
    if c_param3.temp_num <> 0 then
      c_expr.expr &:= "{\n";
      appendWithDiagnostic(c_param1.temp_decls, c_expr);
      appendWithDiagnostic(c_param3.temp_decls, c_expr);
      appendWithDiagnostic(c_param1.temp_assigns, c_expr);
      appendWithDiagnostic(c_param3.temp_assigns, c_expr);
    end if;
    setDiagnosticLine(c_expr);
    c_expr.expr &:= c_param1.expr;
    c_expr.expr &:= "=";
    if c_param3.result_expr <> "" then
      c_expr.expr &:= c_param3.result_expr;
    else
      c_expr.expr &:= c_param3.expr;
    end if;
    c_expr.expr &:= ";\n";
    if c_param3.temp_num <> 0 then
      appendWithDiagnostic(c_param1.temp_frees, c_expr);
      appendWithDiagnostic(c_param3.temp_frees, c_expr);
      c_expr.expr &:= "}\n";
    end if;
  end func;


const proc: process (ACE_EQ, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ") == (";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ACE_GEN, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    programUses.compilerLibrary := TRUE;
    c_expr.expr &:= "aceGen(";
    process_expr(params[2], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ACE_HASHCODE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    programUses.compilerLibrary := TRUE;
    c_expr.expr &:= "aceHashCode(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ACE_ICONV1, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    programUses.compDataLibrary := TRUE;
    c_expr.expr &:= "aceIConv(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ACE_ICONV3, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    programUses.compDataLibrary := TRUE;
    c_expr.expr &:= "aceIConv(";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ACE_NE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    c_expr.expr &:= "(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ") != (";
    process_expr(params[3], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ACE_ORD, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    programUses.compilerLibrary := TRUE;
    c_expr.expr &:= "aceOrd(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;


const proc: process (ACE_STR, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    programUses.compilerLibrary := TRUE;
    prepare_stri_result(c_expr);
    c_expr.result_expr := "aceStr(";
    getStdParamToResultExpr(params[1], c_expr);
    c_expr.result_expr &:= ")";
  end func;


const proc: process (ACE_VALUE, in reference: function,
    in ref_list: params, inout expr_type: c_expr) is func

  begin
    programUses.compDataLibrary := TRUE;
    c_expr.expr &:= "aceValue(";
    process_expr(params[1], c_expr);
    c_expr.expr &:= ")";
  end func;