home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / expritem.c < prev    next >
C/C++ Source or Header  |  1992-11-02  |  13KB  |  523 lines

  1. /*
  2.  * ExprItems.C - methods for all the ExprItem's.
  3.  *
  4.  * Copyright (C) 1992, Christoph Streit (streit@iam.unibe.ch)
  5.  * All rights reserved.
  6.  *
  7.  * This software may be freely copied, modified, and redistributed
  8.  * provided that this copyright notice is preserved on all copies.
  9.  *
  10.  * You may not distribute this software, in whole or in part, as part of
  11.  * any commercial product without the express consent of the authors.
  12.  *
  13.  * There is no warranty or other guarantee of fitness of this software
  14.  * for any purpose.  It is provided solely "as is".
  15.  *
  16.  */
  17.  
  18. #include <iostream.h>
  19. #include "ExprItems.h"
  20.  
  21. //___________________________________________________________ Variable
  22.  
  23. Variable::Variable(const Name& n, Value* val)
  24. : varname(n), theValue(val)
  25. {}
  26. void Variable::process(ValueStack* theStack)
  27. {
  28.   theStack->push(*theValue); 
  29. }
  30. Variable::~Variable(){}
  31. void Variable::print(ValueStack* theStack)
  32. {
  33.   theStack->push(Value((const char*)varname));
  34. }
  35. // there's no way to simplify a variable
  36. int Variable::params() { return -1; } 
  37. ExprItem* Variable::copy() { return new Variable(varname, theValue); }
  38.  
  39. //___________________________________________________________ Uminus
  40.  
  41. Uminus::Uminus(){}
  42. Uminus::~Uminus(){}
  43. void Uminus::process(ValueStack* theStack)
  44.   theStack->push(- theStack->pop()); 
  45. }
  46. void Uminus::print(ValueStack* theStack)
  47.   theStack->push(Value("-(" + (rcString)theStack->pop() + ")")); 
  48. }
  49. int Uminus::params() { return 1; }
  50. ExprItem* Uminus::copy() { return new Uminus; }
  51.  
  52. //___________________________________________________________ Not
  53.  
  54. Not::Not(){}
  55. Not::~Not(){}
  56. void Not::process(ValueStack* theStack)
  57. {
  58.   theStack->push(!theStack->pop());
  59. }
  60. void Not::print(ValueStack* theStack)
  61.   theStack->push(Value("!(" + (rcString)theStack->pop() + ")")); 
  62. }
  63. int Not::params() { return 1; }
  64. ExprItem* Not::copy() { return new Not; }
  65.  
  66. //___________________________________________________________ Or
  67.  
  68. Or::Or(){}
  69. Or::~Or(){}
  70. void Or::process(ValueStack* theStack)
  71. {
  72.   theStack->push(theStack->pop() || theStack->pop());
  73. }
  74. void Or::print(ValueStack* theStack)
  75. {
  76.   theStack->push(Value("(" + (rcString)theStack->pop() + " || " +
  77.                      (rcString)theStack->pop() + ")"));
  78. }
  79. int Or::params() { return 2; }
  80. ExprItem* Or::copy() { return new Or; }
  81.  
  82. //___________________________________________________________ And
  83.  
  84. And::And(){}
  85. And::~And(){}
  86. void And::process(ValueStack* theStack)
  87. {
  88.   theStack->push(theStack->pop() && theStack->pop());
  89. }
  90. void And::print(ValueStack* theStack)
  91. {
  92.   theStack->push(Value("(" + (rcString)theStack->pop() + " && " +
  93.                      (rcString)theStack->pop() + ")"));
  94. }
  95. int And::params() { return 2; }
  96. ExprItem* And::copy() { return new And; }
  97.  
  98. //___________________________________________________________ Neq
  99.  
  100. Neq::Neq(){}
  101. Neq::~Neq(){}
  102. void Neq::process(ValueStack* theStack)
  103. {
  104.   theStack->push(theStack->pop() != theStack->pop());
  105. }
  106. void Neq::print(ValueStack* theStack)
  107. {
  108.   theStack->push(Value("(" + (rcString)theStack->pop() + " != " +
  109.                      (rcString)theStack->pop() + ")"));
  110. }
  111. int Neq::params() { return 2; }
  112. ExprItem* Neq::copy() { return new Neq; }
  113.  
  114. //___________________________________________________________ Eq
  115.  
  116. Eq::Eq(){}
  117. Eq::~Eq(){}
  118. void Eq::process(ValueStack* theStack)
  119. {
  120.   theStack->push(theStack->pop() == theStack->pop());
  121. }
  122. void Eq::print(ValueStack* theStack)
  123. {
  124.   theStack->push(Value("(" + (rcString)theStack->pop() + " == " +
  125.                      (rcString)theStack->pop() + ")"));
  126. }
  127. int Eq::params() { return 2; }
  128. ExprItem* Eq::copy() { return new Eq; }
  129.  
  130. //___________________________________________________________ Lt
  131.  
  132. Lt::Lt(){}
  133. Lt::~Lt(){}
  134. void Lt::process(ValueStack* theStack)
  135. {
  136.   theStack->push(theStack->pop() < theStack->pop());
  137. }
  138. void Lt::print(ValueStack* theStack)
  139. {
  140.   theStack->push(Value("(" + (rcString)theStack->pop() + " < " +
  141.                      (rcString)theStack->pop() + ")"));
  142. }
  143. int Lt::params() { return 2; }
  144. ExprItem* Lt::copy() { return new Lt; }
  145.  
  146. //___________________________________________________________ Leq
  147.  
  148. Leq::Leq(){}
  149. Leq::~Leq(){}
  150. void Leq::process(ValueStack* theStack)
  151. {
  152.   theStack->push(theStack->pop() <= theStack->pop());
  153. }
  154. void Leq::print(ValueStack* theStack)
  155. {
  156.   theStack->push(Value("(" + (rcString)theStack->pop() + " <= " +
  157.                      (rcString)theStack->pop() + ")"));
  158. }
  159. int Leq::params() { return 2; }
  160. ExprItem* Leq::copy() { return new Leq; }
  161.  
  162. //___________________________________________________________ Gt
  163.  
  164. Gt::Gt(){}
  165. Gt::~Gt(){}
  166. void Gt::process(ValueStack* theStack)
  167. {
  168.   theStack->push(theStack->pop() > theStack->pop());
  169. }
  170. void Gt::print(ValueStack* theStack)
  171. {
  172.   theStack->push(Value("(" + (rcString)theStack->pop() + " > " +
  173.                      (rcString)theStack->pop() + ")"));
  174. }
  175. int Gt::params() { return 2; }
  176. ExprItem* Gt::copy() { return new Gt; }
  177.  
  178. //___________________________________________________________ Geq
  179.  
  180. Geq::Geq(){}
  181. Geq::~Geq(){}
  182. void Geq::process(ValueStack* theStack)
  183. {
  184.   theStack->push(theStack->pop() >= theStack->pop());
  185. }
  186. void Geq::print(ValueStack* theStack)
  187. {
  188.   theStack->push(Value("(" + (rcString)theStack->pop() + " >= " +
  189.                      (rcString)theStack->pop() + ")"));
  190. }
  191. int Geq::params() { return 2; }
  192. ExprItem* Geq::copy() { return new Geq; }
  193.  
  194. //___________________________________________________________ Add
  195.  
  196. Add::Add(){}
  197. Add::~Add(){}
  198. void Add::process(ValueStack* theStack)
  199. {
  200.   theStack->push(theStack->pop() + theStack->pop());
  201. }
  202. void Add::print(ValueStack* theStack)
  203. {
  204.   theStack->push(Value("(" + (rcString)theStack->pop() + " + " +
  205.                      (rcString)theStack->pop() + ")"));
  206. }
  207. int Add::params() { return 2; }
  208. ExprItem* Add::copy() { return new Add; }
  209.  
  210. //___________________________________________________________ Sub
  211.  
  212. Sub::Sub(){}
  213. Sub::~Sub(){}
  214. void Sub::process(ValueStack* theStack)
  215. {
  216.   theStack->push(theStack->pop() - theStack->pop());
  217. }
  218. void Sub::print(ValueStack* theStack)
  219. {
  220.   theStack->push(Value("(" + (rcString)theStack->pop() + " - " +
  221.                      (rcString)theStack->pop() + ")"));
  222. }
  223. int Sub::params() { return 2; }
  224. ExprItem* Sub::copy() { return new Sub; }
  225.  
  226. //___________________________________________________________ Mul
  227.  
  228. Mul::Mul(){}
  229. Mul::~Mul(){}
  230. void Mul::process(ValueStack* theStack)
  231. {
  232.   theStack->push(theStack->pop() * theStack->pop());
  233. }
  234. void Mul::print(ValueStack* theStack)
  235. {
  236.   theStack->push(Value("(" + (rcString)theStack->pop() + " * " +
  237.                      (rcString)theStack->pop() + ")"));
  238. }
  239. int Mul::params() { return 2; }
  240. ExprItem* Mul::copy() { return new Mul; }
  241.  
  242. //___________________________________________________________ Div
  243.  
  244. Div::Div(){}
  245. Div::~Div(){}
  246. void Div::process(ValueStack* theStack)
  247. {
  248.   theStack->push(theStack->pop() / theStack->pop());
  249. }
  250. void Div::print(ValueStack* theStack)
  251. {
  252.   theStack->push(Value("(" + (rcString)theStack->pop() + " / " +
  253.                      (rcString)theStack->pop() + ")"));
  254. }
  255. int Div::params() { return 2; }
  256. ExprItem* Div::copy() { return new Div; }
  257.  
  258. //___________________________________________________________ Mod
  259.  
  260. Mod::Mod(){}
  261. Mod::~Mod(){}
  262. void Mod::process(ValueStack* theStack)
  263. {
  264.   theStack->push(theStack->pop() % theStack->pop());
  265. }
  266. void Mod::print(ValueStack* theStack)
  267. {
  268.   theStack->push(Value("(" + (rcString)theStack->pop() + " % " +
  269.                      (rcString)theStack->pop() + ")"));
  270. }
  271. int Mod::params() { return 2; }
  272. ExprItem* Mod::copy() { return new Mod; }
  273.  
  274. //___________________________________________________________ Sin
  275.  
  276. Sin::Sin(){}
  277. Sin::~Sin(){}
  278. void Sin::process(ValueStack* theStack)
  279.   theStack->push( sin(dtor(real(theStack->pop()))) ); 
  280. }
  281. void Sin::print(ValueStack* theStack)
  282.   theStack->push(Value("sin " + (rcString)theStack->pop()));
  283. }
  284. int Sin::params() { return 1; }
  285. ExprItem* Sin::copy() { return new Sin; }
  286.  
  287. //___________________________________________________________  Cos
  288.  
  289. Cos::Cos(){}
  290. Cos::~Cos(){}
  291. void Cos::process(ValueStack* theStack)
  292.   theStack->push(cos(dtor(real(theStack->pop()))) ); 
  293. }
  294. void Cos::print(ValueStack* theStack)
  295.   theStack->push(Value("cos " + (rcString)theStack->pop()));
  296. }
  297. int Cos::params() { return 1; }
  298. ExprItem* Cos::copy() { return new Cos; }
  299.  
  300. //___________________________________________________________ Tan
  301.  
  302. Tan::Tan(){}
  303. Tan::~Tan(){}
  304. void Tan::process(ValueStack* theStack)
  305.   theStack->push( tan(dtor(real(theStack->pop()))) ); 
  306. }
  307. void Tan::print(ValueStack* theStack)
  308.   theStack->push(Value("tan " + (rcString)theStack->pop()));
  309. }
  310. int Tan::params() { return 1; }
  311. ExprItem* Tan::copy() { return new Tan; }
  312.  
  313. //___________________________________________________________ Asin
  314.  
  315. Asin::Asin(){}
  316. Asin::~Asin(){}
  317. void Asin::process(ValueStack* theStack)
  318.   theStack->push( rtod(asin(real(theStack->pop()))) ); 
  319. }
  320. void Asin::print(ValueStack* theStack)
  321.   theStack->push(Value("asin " + (rcString)theStack->pop()));
  322. }
  323. int Asin::params() { return 1; }
  324. ExprItem* Asin::copy() { return new Asin; }
  325.  
  326. //___________________________________________________________ Acos
  327.  
  328. Acos::Acos(){}
  329. Acos::~Acos(){}
  330. void Acos::process(ValueStack* theStack)
  331.   theStack->push( rtod(acos(real(theStack->pop()))) ); 
  332. }
  333. void Acos::print(ValueStack* theStack)
  334.   theStack->push(Value("acos " + (rcString)theStack->pop()));
  335. }
  336. int Acos::params() { return 1; }
  337. ExprItem* Acos::copy() { return new Acos; }
  338.  
  339. //___________________________________________________________ Atan
  340.  
  341. Atan::Atan(){}
  342. Atan::~Atan(){}
  343. void Atan::process(ValueStack* theStack)
  344.   theStack->push( rtod(atan(real(theStack->pop()))) ); 
  345. }
  346. void Atan::print(ValueStack* theStack)
  347.   theStack->push(Value("atan " + (rcString)theStack->pop()));
  348. }
  349. int Atan::params() { return 1; }
  350. ExprItem* Atan::copy() { return new Atan; }
  351.  
  352. //___________________________________________________________ Abs
  353.  
  354. Abs::Abs(){}
  355. Abs::~Abs(){}
  356. void Abs::process(ValueStack* theStack)
  357.   theStack->push(fabs(real(theStack->pop())) ); 
  358. }
  359. void Abs::print(ValueStack* theStack)
  360.   theStack->push(Value("abs " + (rcString)theStack->pop()));
  361. }
  362. int Abs::params() { return 1; }
  363. ExprItem* Abs::copy() { return new Abs; }
  364.  
  365. //___________________________________________________________ Sqrt
  366.  
  367. Sqrt::Sqrt(){}
  368. Sqrt::~Sqrt(){}
  369. void Sqrt::process(ValueStack* theStack)
  370.   theStack->push(sqrt(real(theStack->pop())) ); 
  371. }
  372. void Sqrt::print(ValueStack* theStack)
  373.   theStack->push(Value("sqrt " + (rcString)theStack->pop()));
  374. }
  375. int Sqrt::params() { return 1; }
  376. ExprItem* Sqrt::copy() { return new Sqrt; }
  377.  
  378. //___________________________________________________________ Pow
  379.  
  380. Pow::Pow(){}
  381. Pow::~Pow(){}
  382. void Pow::process(ValueStack* theStack)
  383.   theStack->push( pow(real(theStack->pop()), real(theStack->pop())) );
  384. }
  385. void Pow::print(ValueStack* theStack)
  386. {
  387.   theStack->push(Value("(" + (rcString)theStack->pop() + " ** " +
  388.                      (rcString)theStack->pop() + ")"));
  389. }
  390. int Pow::params() { return 2; }
  391. ExprItem* Pow::copy() { return new Pow; }
  392.  
  393. //___________________________________________________________ Exp
  394.  
  395. Exp::Exp(){}
  396. Exp::~Exp(){}
  397. void Exp::process(ValueStack* theStack)
  398.   theStack->push( exp(real(theStack->pop())) ); 
  399. }
  400. void Exp::print(ValueStack* theStack)
  401.   theStack->push(Value("exp " + (rcString)theStack->pop()));
  402. }
  403. int Exp::params() { return 1; }
  404. ExprItem* Exp::copy() { return new Exp; }
  405.  
  406. //___________________________________________________________ Log
  407.  
  408. Log::Log(){}
  409. Log::~Log(){}
  410. void Log::process(ValueStack* theStack)
  411.   theStack->push( log(real(theStack->pop())) ); 
  412. }
  413. void Log::print(ValueStack* theStack)
  414.   theStack->push(Value("log " + (rcString)theStack->pop()));
  415. }
  416. int Log::params() { return 1; }
  417. ExprItem* Log::copy() { return new Log; }
  418.  
  419. //___________________________________________________________ Log10
  420.  
  421. Log10::Log10(){}
  422. Log10::~Log10(){}
  423. void Log10::process(ValueStack* theStack)
  424.   theStack->push( log10(real(theStack->pop())) ); 
  425. }
  426. void Log10::print(ValueStack* theStack)
  427.   theStack->push(Value("log10 " + (rcString)theStack->pop()));
  428. }
  429. int Log10::params() { return 1; }
  430. ExprItem* Log10::copy() { return new Log10; }
  431.  
  432. //___________________________________________________________ Rand
  433.  
  434. Rand::Rand(){}
  435. Rand::~Rand(){}
  436. void Rand::process(ValueStack* theStack)
  437.   theStack->push(real(drand48())); 
  438. }
  439. void Rand::print(ValueStack* theStack)
  440.   theStack->push(Value("Rand() "));
  441. }
  442. int Rand::params() { return -1; }
  443. ExprItem* Rand::copy() { return new Rand; }
  444.  
  445. /*___________________________________________________________ Gauss
  446.  *
  447.  * Gaussian random number generator.
  448.  */
  449.  
  450. Gauss::Gauss()
  451. {
  452.   Nrand = 4;
  453.   GaussAdd = sqrt(3*Nrand);
  454.   GaussFac = 2*GaussAdd/Nrand;
  455. }
  456. Gauss::~Gauss(){}
  457. void Gauss::process(ValueStack* theStack)
  458.   real sum = 0;
  459.   for (int i=0; i<Nrand; i++)
  460.     sum += drand48();
  461.   theStack->push(real(GaussFac*sum-GaussAdd)); 
  462. }
  463. void Gauss::print(ValueStack* theStack)
  464.   theStack->push(Value("Gauss() "));
  465. }
  466. int Gauss::params() { return -1; }
  467. ExprItem* Gauss::copy() { return new Gauss; }
  468.  
  469. //___________________________________________________________ If
  470.  
  471. If::If(){}
  472. If::~If(){}
  473. void If::process(ValueStack* theStack)
  474.   if (theStack->pop()) {
  475.     Value result = theStack->pop();
  476.     theStack->pop();
  477.     theStack->push(result);
  478.   }
  479.   else {
  480.     theStack->pop();
  481.     // theStack->push(theStack->pop());
  482.   }
  483. }
  484. void If::print(ValueStack* theStack)
  485.   theStack->push(Value("If(" + (rcString)theStack->pop() + ", " 
  486.                     + (rcString)theStack->pop() + ", " 
  487.                     + (rcString)theStack->pop() + ")")); 
  488. }
  489. int If::params() { return 3; }
  490. ExprItem* If::copy() { return new If; }
  491.