1 /********************************************************************/
2 /*                                                                  */
3 /*  s7   Seed7 interpreter                                          */
4 /*  Copyright (C) 1990 - 2008  Thomas Mertes                        */
5 /*                                                                  */
6 /*  This program is free software; you can redistribute it and/or   */
7 /*  modify it under the terms of the GNU General Public License as  */
8 /*  published by the Free Software Foundation; either version 2 of  */
9 /*  the License, or (at your option) any later version.             */
10 /*                                                                  */
11 /*  This program is distributed in the hope that it will be useful, */
12 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of  */
13 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   */
14 /*  GNU General Public License for more details.                    */
15 /*                                                                  */
16 /*  You should have received a copy of the GNU General Public       */
17 /*  License along with this program; if not, write to the           */
18 /*  Free Software Foundation, Inc., 51 Franklin Street,             */
19 /*  Fifth Floor, Boston, MA  02110-1301, USA.                       */
20 /*                                                                  */
21 /*  Module: Seed7 compiler library                                  */
22 /*  File: seed7/src/act_comp.c                                      */
23 /*  Changes: 1991, 1992, 1993, 1994, 2008  Thomas Mertes            */
24 /*  Content: Primitive actions for the action type.                 */
25 /*                                                                  */
26 /********************************************************************/
27 
28 #define LOG_FUNCTIONS 0
29 #define VERBOSE_EXCEPTIONS 0
30 
31 #include "version.h"
32 
33 #include "stdlib.h"
34 #include "stdio.h"
35 #include "string.h"
36 
37 #include "common.h"
38 #include "data.h"
39 #include "heaputl.h"
40 #include "striutl.h"
41 #include "actutl.h"
42 #include "rtl_err.h"
43 
44 #undef EXTERN
45 #define EXTERN
46 #include "act_comp.h"
47 
48 
49 
50 /**
51  *  Convert a string to an action.
52  *  @param actionName Name of the action to be converted.
53  *  @return an action which corresponds to the given string.
54  *  @exception RANGE_ERROR No such action exists.
55  */
actGen(striType actionName)56 actType actGen (striType actionName)
57 
58   {
59     actType anAction;
60 
61   /* actGen */
62     logFunction(printf("actGen(\"%s\")\n",
63                        striAsUnquotedCStri(actionName)););
64     anAction = findAction(actionName);
65     if (unlikely(anAction == NULL)) {
66       logError(printf("act_gen(\"%s\"): No such action exists.\n",
67                       striAsUnquotedCStri(actionName)););
68       raise_error(RANGE_ERROR);
69     } /* if */
70     logFunction(printf("actGen --> " FMT_U_MEM "\n",
71                        (memSizeType) anAction););
72     return anAction;
73   } /* actGen */
74 
75 
76 
77 /**
78  *  Convert an integer number to an action.
79  *  @param ordinal Number to be converted.
80  *  @return an action which corresponds to the given integer.
81  *  @exception RANGE_ERROR Number not in allowed range.
82  */
actIConv(intType ordinal)83 actType actIConv (intType ordinal)
84 
85   {
86     actType anAction;
87 
88   /* actIConv */
89     logFunction(printf("actIConv(" FMT_D ")\n", ordinal););
90     if (unlikely(ordinal < 0 || (uintType) ordinal >= actTable.size)) {
91       logError(printf("actIConv(" FMT_D "): "
92                       "Number not in allowed range of 0 .. %lu.\n",
93                       ordinal, (unsigned long) (actTable.size - 1)););
94       raise_error(RANGE_ERROR);
95       anAction = NULL;
96     } else {
97       anAction = actTable.table[ordinal].action;
98     } /* if */
99     logFunction(printf("actIConv --> " FMT_U_MEM "\n",
100                        (memSizeType) anAction););
101     return anAction;
102   } /* actIConv */
103 
104 
105 
106 /**
107  *  Get the ordinal number of an action.
108  *  The action ACT_ILLEGAL has the ordinal number 0.
109  *  @param anAction Action for which the ordinal number is determined.
110  *  @return the ordinal number of the action.
111  */
actOrd(actType anAction)112 intType actOrd (actType anAction)
113 
114   {
115     intType ordinal;
116 
117   /* actOrd */
118     logFunction(printf("actOrd(" FMT_U_MEM ")\n",
119                        (memSizeType) anAction););
120     ordinal = getActEntry(anAction) - actTable.table;
121     logFunction(printf("actOrd --> " FMT_D "\n", ordinal););
122     return ordinal;
123   } /* actOrd */
124 
125 
126 
127 /**
128  *  Convert an action to a string.
129  *  If the action is not found in the table of legal actions
130  *  the string "ACT_ILLEGAL" is returned.
131  *  @param anAction Action which is converted to a string..
132  *  @return the string result of the conversion.
133  *  @exception MEMORY_ERROR Not enough memory to represent the result.
134  */
actStr(actType anAction)135 striType actStr (actType anAction)
136 
137   {
138     striType actionName;
139 
140   /* actStr */
141     logFunction(printf("actStr(" FMT_U_MEM ")\n",
142                        (memSizeType) anAction););
143     actionName = cstri_to_stri(getActEntry(anAction)->name);
144     if (unlikely(actionName == NULL)) {
145       raise_error(MEMORY_ERROR);
146     } /* if */
147     logFunction(printf("actStr --> \"%s\"\n",
148                        striAsUnquotedCStri(actionName)););
149     return actionName;
150   } /* actStr */
151