2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 // $XConsortium: evaluate.cc /main/3 1996/06/11 17:10:18 cde-hal $
25 #include "Expression.h"
27 #include "FeatureValue.h"
28 #include "ResolverStack.h"
29 #include "StyleSheet.h"
30 #include "StyleSheetExceptions.h"
32 #include "VariableTable.h"
34 extern const Element *gCurrentElement ;
35 extern const FeatureSet *gCurrentLocalSet;
36 extern const FeatureSet *gParentCompleteSet;
38 // unused except by HardCopy
39 Renderer *gRenderer = 0 ;
42 styleerror(char *errorstr)
44 cerr << "Parse Error: " << errorstr << endl;
46 main(int argc, char **argv)
57 /* -------- String -------- */
58 fs.add(new Feature(gSymTab->intern("string"),
59 new FeatureValueString("this is a string")));
61 /* -------- Symbol -------- */
62 fs.add(new Feature(gSymTab->intern("symbol"),
63 new FeatureValueSymbol(gSymTab->intern("Symbol"))));
65 /* -------- Integer -------- */
67 fs.add(new Feature(gSymTab->intern("int"),
68 new FeatureValueInt(17)));
70 /* -------- Real -------- */
72 fs.add(new Feature(gSymTab->intern("real"),
73 new FeatureValueReal(42.2)));
76 /* -------- Integer addition -------- */
77 exp = new FeatureValueExpression
78 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::PLUS,
79 new ConstantNode(new FeatureValueInt(10)),
80 new ConstantNode(new FeatureValueInt(10)))));
84 FeatureValueExpression ep(*(FeatureValueExpression*)exp);
87 fs.add(new Feature(gSymTab->intern("int + int"), exp));
89 /* -------- Integer subtraction -------- */
90 exp = new FeatureValueExpression
91 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::MINUS,
92 new ConstantNode(new FeatureValueInt(3)),
93 new ConstantNode(new FeatureValueInt(127)))));
95 fs.add(new Feature(gSymTab->intern("int - int"), exp));
97 /* -------- Integer division -------- */
98 exp = new FeatureValueExpression
99 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::DIVIDE,
100 new ConstantNode(new FeatureValueInt(10)),
101 new ConstantNode(new FeatureValueInt(2)))));
103 fs.add(new Feature(gSymTab->intern("int / int"), exp));
105 /* -------- Real division -------- */
106 exp = new FeatureValueExpression
107 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::DIVIDE,
108 new ConstantNode(new FeatureValueReal(5)),
109 new ConstantNode(new FeatureValueReal(2)))));
111 fs.add(new Feature(gSymTab->intern("real / real"), exp));
113 /* -------- Real multiplication -------- */
114 exp = new FeatureValueExpression
115 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::TIMES,
116 new ConstantNode(new FeatureValueReal(5.1)),
117 new ConstantNode(new FeatureValueReal(8.7)))));
119 fs.add(new Feature(gSymTab->intern("real * real"), exp));
121 /* -------- int + real -------- */
122 exp = new FeatureValueExpression
123 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::PLUS,
124 new ConstantNode(new FeatureValueInt(5)),
125 new ConstantNode(new FeatureValueReal(8.7)))));
127 fs.add(new Feature(gSymTab->intern("int + real"), exp));
130 /* -------- real + int -------- */
131 exp = new FeatureValueExpression
132 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::PLUS,
133 new ConstantNode(new FeatureValueReal(8.7)),
134 new ConstantNode(new FeatureValueInt(5)))));
136 fs.add(new Feature(gSymTab->intern("real + int"), exp));
139 /* -------- now do some variable stuff -------- */
141 // empty top of stack item
142 // gTopOfStack = new ResolverStackElement(0,new FeatureSet(),new FeatureSet());
144 FeatureSet *localSet = new FeatureSet ;
145 gCurrentElement = 0 ;
146 gCurrentLocalSet = localSet ;
147 gParentCompleteSet = new FeatureSet ;
149 // add an element for variable lookup
150 localSet->add(new Feature(gSymTab->intern("size"),
151 new FeatureValueInt(10)));
153 // make some variable entries
154 // insert a duplicate to check memory leaks
156 gVariableTable->enter(gSymTab->intern("DEFAULT_FONT_FAMILY"),
157 new Expression(new ConstantNode(new FeatureValueString("courier"))));
158 gVariableTable->enter(gSymTab->intern("DEFAULT_FONT_FAMILY"),
159 new Expression(new ConstantNode(new FeatureValueString("helvetica"))));
162 fs.add(new Feature(gSymTab->intern("exp"),
163 new FeatureValueExpression
164 (new Expression (new VariableNode(gSymTab->intern("DEFAULT_FONT_FAMILY"))))));
166 CompositeVariableNode *cvn = new CompositeVariableNode;
167 cvn->appendItem(gSymTab->intern("size"));
168 fs.add(new Feature(gSymTab->intern("Xsize"),
169 new FeatureValueExpression(new Expression (cvn))));
171 /* -------- dimensions -------- */
172 fs.add(new Feature(gSymTab->intern("dim"),
173 new FeatureValueDimension(new FeatureValueInt(10),
174 FeatureValueDimension::INCH)));
176 exp = new FeatureValueExpression
178 (new BinaryOperatorNode(BinaryOperatorNode::PLUS,
179 new ConstantNode(new FeatureValueInt(5)),
181 (new FeatureValueDimension
182 (new FeatureValueInt(25),
183 FeatureValueDimension::INCH)))));
185 fs.add(new Feature(gSymTab->intern("int + dim"), exp));
187 exp = new FeatureValueExpression
189 (new BinaryOperatorNode(BinaryOperatorNode::PLUS,
191 (new FeatureValueDimension
192 (new FeatureValueInt(25),
193 FeatureValueDimension::INCH)),
194 new ConstantNode(new FeatureValueInt(5)))));
196 fs.add(new Feature(gSymTab->intern("dim + int"), exp));
199 exp = new FeatureValueExpression
201 (new BinaryOperatorNode(BinaryOperatorNode::PLUS,
203 (new FeatureValueDimension
204 (new FeatureValueInt(25),
205 FeatureValueDimension::POINT)),
207 (new FeatureValueDimension
208 (new FeatureValueInt(5),
209 FeatureValueDimension::INCH)))));
212 fs.add(new Feature(gSymTab->intern("dim + dim"), exp));
214 /* -------- real * dim -------- */
215 exp = new FeatureValueExpression
217 (new BinaryOperatorNode(BinaryOperatorNode::TIMES,
219 (new FeatureValueReal(2.2)),
221 (new FeatureValueDimension
222 (new FeatureValueInt(11),
223 FeatureValueDimension::POINT)))));
226 fs.add(new Feature(gSymTab->intern("real * dim"), exp));
229 /* -------- dim * real -------- */
230 exp = new FeatureValueExpression
232 (new BinaryOperatorNode(BinaryOperatorNode::TIMES,
234 (new FeatureValueDimension
235 (new FeatureValueInt(11),
236 FeatureValueDimension::POINT)),
238 (new FeatureValueReal(2.1)))));
241 fs.add(new Feature(gSymTab->intern("dim * real"), exp));
244 /* -------- dim / real -------- */
245 exp = new FeatureValueExpression
247 (new BinaryOperatorNode(BinaryOperatorNode::DIVIDE,
249 (new FeatureValueDimension
250 (new FeatureValueInt(11),
251 FeatureValueDimension::POINT)),
253 (new FeatureValueReal(2.0)))));
256 fs.add(new Feature(gSymTab->intern("dim / real"), exp));
259 /* -------- real / dim -------- */
260 exp = new FeatureValueExpression
262 (new BinaryOperatorNode(BinaryOperatorNode::DIVIDE,
264 (new FeatureValueReal(11)),
266 (new FeatureValueDimension
267 (new FeatureValueInt(2),
268 FeatureValueDimension::POINT)))));
271 fs.add(new Feature(gSymTab->intern("real / dim"), exp));
274 /* -------- dim * int -------- */
275 exp = new FeatureValueExpression
277 (new BinaryOperatorNode(BinaryOperatorNode::TIMES,
279 (new FeatureValueDimension
280 (new FeatureValueInt(11),
281 FeatureValueDimension::POINT)),
283 (new FeatureValueInt(2)))));
286 fs.add(new Feature(gSymTab->intern("dim * int"), exp));
289 /* -------- int * dim -------- */
290 exp = new FeatureValueExpression
292 (new BinaryOperatorNode(BinaryOperatorNode::TIMES,
294 (new FeatureValueInt(2)),
296 (new FeatureValueDimension
297 (new FeatureValueInt(11),
298 FeatureValueDimension::POINT)))));
301 fs.add(new Feature(gSymTab->intern("int * dim"), exp));
304 /* -------- dim / int -------- */
305 exp = new FeatureValueExpression
307 (new BinaryOperatorNode(BinaryOperatorNode::DIVIDE,
309 (new FeatureValueDimension
310 (new FeatureValueInt(11),
311 FeatureValueDimension::POINT)),
313 (new FeatureValueInt(2)))));
316 fs.add(new Feature(gSymTab->intern("dim / int"), exp));
319 /* -------- int / dim -------- */
320 exp = new FeatureValueExpression
322 (new BinaryOperatorNode(BinaryOperatorNode::DIVIDE,
324 (new FeatureValueInt(22)),
326 (new FeatureValueDimension
327 (new FeatureValueInt(11),
328 FeatureValueDimension::POINT)))));
331 fs.add(new Feature(gSymTab->intern("int / dim"), exp));
335 /* -------- int - dim -------- */
336 exp = new FeatureValueExpression
338 (new BinaryOperatorNode(BinaryOperatorNode::MINUS,
340 (new FeatureValueInt(2)),
342 (new FeatureValueDimension
343 (new FeatureValueInt(11),
344 FeatureValueDimension::POINT)))));
347 fs.add(new Feature(gSymTab->intern("int - dim"), exp));
350 /* -------- dim - int -------- */
351 exp = new FeatureValueExpression
353 (new BinaryOperatorNode(BinaryOperatorNode::MINUS,
355 (new FeatureValueDimension
356 (new FeatureValueInt(11),
357 FeatureValueDimension::POINT)),
359 (new FeatureValueInt(2)))));
362 fs.add(new Feature(gSymTab->intern("dim - int"), exp));
365 /* -------- String + String -------- */
366 exp = new FeatureValueExpression
367 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::PLUS,
368 new ConstantNode(new FeatureValueString("foo")),
369 new ConstantNode(new FeatureValueString("bar")))));
371 fs.add(new Feature(gSymTab->intern("String + String"), exp));
375 /* -------- String + String + String-------- */
376 FeatureValueExpression *exp1 =
377 new FeatureValueExpression
378 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::PLUS,
379 new ConstantNode(new FeatureValueString("foo")),
380 new ConstantNode(new FeatureValueString("bar")))));
383 exp = new FeatureValueExpression
384 (new Expression(new BinaryOperatorNode(BinaryOperatorNode::PLUS,
385 new ConstantNode(exp1),
386 new ConstantNode(new FeatureValueString("baz")))));
388 fs.add(new Feature(gSymTab->intern("String + String + String"), exp));
392 /* -------- print out our set -------- */
396 FeatureSet *evaluated = fs.evaluate();
398 cout << *evaluated << endl;
403 cout << *gVariableTable << endl;
405 // write this guy out that we created at the top
408 delete gCurrentLocalSet ;
409 delete gParentCompleteSet ;
411 FeatureValueDimension *dtest =
412 new FeatureValueDimension
413 (new FeatureValueDimension(new FeatureValueInt(72),
414 FeatureValue::POINT),
417 cout << *dtest << endl;
418 cout << (float)*dtest << endl;