ExpressionLogical.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.ibm.com/developerworks/oss/CPLv1.0.htm */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Translator */
40 /* */
41 /* Primitive logical list evaluator */
42 /* */
43 /******************************************************************************/
44 #include <stdlib.h>
45 #include "RexxCore.h"
46 #include "StringClass.hpp"
47 #include "QueueClass.hpp"
48 #include "RexxActivation.hpp"
49 #include "RexxInstruction.hpp"
50 #include "ExpressionLogical.hpp"
51 #include "StackClass.hpp"
52 #include "RexxActivity.hpp"
53 #include "BuiltinFunctions.hpp"
54 #include "SourceFile.hpp"
55 
56 /**
57  * Constructor for a RexxExpressionLogical object.
58  *
59  * @param source The source parsing context (used for raising errors)
60  * @param count The number of expressions in the list.
61  * @param list The accumulated list of expressions.
62  */
64 {
65  expressionCount = count;
66 
67  // the parsed expressions are stored in a queue, so we process them in
68  // reverse order.
69  while (count > 0)
70  {
71  RexxObject *condition = list->pop();
72  if (condition == OREF_NULL)
73  {
75  }
76  OrefSet(this, this->expressions[--count], condition);
77  }
78 }
79 
80 /**
81  * The runtime, non-debug live marking routine.
82  */
83 void RexxExpressionLogical::live(size_t liveMark)
84 {
85  size_t i; /* loop counter */
86  size_t count; /* argument count */
87 
88  for (i = 0, count = this->expressionCount; i < count; i++)
89  {
90  memory_mark(this->expressions[i]);
91  }
92 }
93 
94 /**
95  * The generalized live marking routine used for non-performance
96  * critical marking operations.
97  */
99 {
100  size_t i; /* loop counter */
101  size_t count; /* argument count */
102 
103  for (i = 0, count = this->expressionCount; i < count; i++)
104  {
106  }
107 }
108 
109 /**
110  * The flattening routine, used for serializing object trees.
111  *
112  * @param envelope The envelope were's flattening into.
113  */
115 {
116  size_t i; /* loop counter */
117  size_t count; /* argument count */
118 
120 
121  for (i = 0, count = this->expressionCount; i < count; i++)
122  {
123  flatten_reference(newThis->expressions[i], envelope);
124  }
125 
127 }
128 
129 /**
130  * Evaluate a logical expresion list.
131  *
132  * @param context The execution context.
133  * @param stack The evaluation stack.
134  *
135  * @return The result of the operation, either .true or .false.
136  */
138 {
139  // loop through the expression list evaulating and then testing for the
140  // logical value
141  size_t count = expressionCount;
142  // there are no optional values in the list, so evaluate unconditionally.
143  for (size_t i = 0; i < count; i++)
144  {
145  // evaluate and trace
146  RexxObject *value = expressions[i]->evaluate(context, stack);
147  context->traceResult(value);
148 
149  // the comparison methods return either .true or .false, so we
150  // can to a quick test against those.
151  if (value != TheTrueObject) // most of the time, these will be true so test that first.
152  {
153  if (value == TheFalseObject)
154  {
155  return TheFalseObject;
156  }
157  // ok, the either returned a '0' or a '1' that was not the result of returning
158  // .true or .false, or we have a bad value.
160  {
161  return TheFalseObject; // we terminate on the first false condition
162  }
163  }
164  }
165  return TheTrueObject; // all is truth
166 }
167 
168 /**
169  * Create a new logical list object.
170  *
171  * @param size The size of the class object.
172  * @param count The count of logical expressions. Used to adjust the
173  * allocated size to the requirements.
174  *
175  * @return A new RexxExpressionLogical object.
176  */
177 void *RexxExpressionLogical::operator new(size_t size, size_t count)
178 {
179  /* Get new object */
180  return new_object(size + (count - 1) * sizeof(RexxObject *), T_LogicalTerm);
181 }
182 
@ T_LogicalTerm
#define OREF_NULL
Definition: RexxCore.h:60
#define OrefSet(o, r, v)
Definition: RexxCore.h:94
#define TheTrueObject
Definition: RexxCore.h:186
#define TheFalseObject
Definition: RexxCore.h:185
#define Error_Invalid_expression_logical_list
#define Error_Logical_value_logical_list
#define memory_mark(oref)
Definition: RexxMemory.hpp:445
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:431
#define flatten_reference(oref, envel)
Definition: RexxMemory.hpp:493
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:446
#define cleanUpFlatten
Definition: RexxMemory.hpp:479
#define setUpFlatten(type)
Definition: RexxMemory.hpp:473
void traceResult(RexxObject *v)
void flatten(RexxEnvelope *)
RexxObject * expressions[1]
void liveGeneral(int reason)
RexxExpressionLogical(RexxSource *, size_t, RexxQueue *)
RexxObject * evaluate(RexxActivation *, RexxExpressionStack *)
virtual RexxObject * evaluate(RexxActivation *, RexxExpressionStack *)
bool truthValue(int)
RexxObject * pop()
Definition: QueueClass.hpp:80
void syntaxError(int errorcode, RexxInstruction *i)
Definition: SourceFile.hpp:319