home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / graphtal / module.c < prev    next >
C/C++ Source or Header  |  1992-10-19  |  3KB  |  117 lines

  1. /*
  2.  * Module.C - methods for L-System module handling.
  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 "Module.h"
  19. #include "Error.h"
  20.  
  21. implementList(ExpressionList, ExpressionPtr);
  22. implementList(ValueList, ValuePtr);
  23. implementList(ModuleList, ModulePtr);
  24. implementList(ProdModuleList, ProdModulePtr);
  25.  
  26. //___________________________________________________________ Formals
  27.  
  28. const int MAX_PARAMETERS = 100;
  29. static Value Parameters[100];
  30.  
  31. Value* Formals(int i)
  32. {
  33.   if (i>=MAX_PARAMETERS)
  34.     Error(ERR_PANIC, "Formals: index out of range");
  35.   return &Parameters[i];
  36. }
  37.  
  38. //___________________________________________________________ ProdModule
  39.  
  40. ProdModule::ProdModule(const Name& n, ExpressionList* expressions)
  41. : name(n), parameters(expressions)
  42. {
  43.   extern unsigned int modHash(const char*, long);
  44.   if (expressions)
  45.     hashValue = modHash((const char*) n, expressions->count());
  46.   else
  47.     hashValue = modHash((const char*) n, 0L);
  48. }
  49.  
  50. ProdModule::~ProdModule()
  51. {
  52.   if (parameters) {
  53.     for (long i=0; i<parameters->count(); i++)
  54.       delete parameters->item(i);
  55.     delete parameters;
  56.   }
  57. }
  58.  
  59. ostream& operator<<(ostream& os, const ProdModule& m)
  60. {
  61.   os << m.name;
  62.   if (m.parameters) {
  63.     os << '(';
  64.     for (long i=0; i<m.parameters->count()-1; i++)
  65.       os << *m.parameters->item(i) << ", ";    
  66.     os << *m.parameters->item(i) << ')';
  67.   }
  68.   return os;
  69. }
  70.  
  71. //___________________________________________________________ Module
  72.  
  73. // Evaluate the parameter of the ProdModule and store the results
  74. //  as the constant arguments of the module
  75. Module::Module(ProdModule* m)
  76. : _name(m->name), _hashValue(m->hashValue)
  77. {
  78.   if (m->parameters) {
  79.     parameters = new ValueList(m->parameters->count());
  80.     for (register long i=0; i<m->parameters->count(); i++)
  81.       parameters->append(new Value(m->parameters->item(i)->evaluate()));
  82.   }
  83.   else
  84.     parameters = NULL;
  85. }
  86.  
  87. Module::~Module()
  88. {
  89.   if (parameters) {
  90.     for (long i=0; i<parameters->count(); i++)
  91.       delete parameters->item(i);
  92.     delete parameters;
  93.   }
  94. }
  95.  
  96. // Binding is done by assigning the actual parameters of the module
  97. //  to the Formals-array. This works because all the variables of the
  98. //  productions points to this array!
  99. void Module::bind()
  100. {
  101.   if (parameters)
  102.     for (register long i=0; i<parameters->count(); i++)
  103.       Parameters[i] = *parameters->item(i);
  104. }
  105.  
  106. ostream& operator<<(ostream& os, const Module& m)
  107. {
  108.   os << m._name;
  109.   if (m.parameters) {
  110.     os << '(';
  111.     for (long i=0; i<m.parameters->count()-1; i++)
  112.       os << *m.parameters->item(i) << ", ";
  113.     os << *m.parameters->item(i) << ')';
  114.   }
  115.   return os;
  116. }
  117.