AERA
preprocessor.h
1 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
2 //_/_/
3 //_/_/ AERA
4 //_/_/ Autocatalytic Endogenous Reflective Architecture
5 //_/_/
6 //_/_/ Copyright (c) 2018-2025 Jeff Thompson
7 //_/_/ Copyright (c) 2018-2025 Kristinn R. Thorisson
8 //_/_/ Copyright (c) 2018-2025 Icelandic Institute for Intelligent Machines
9 //_/_/ http://www.iiim.is
10 //_/_/
11 //_/_/ Copyright (c) 2010-2012 Eric Nivel
12 //_/_/ Center for Analysis and Design of Intelligent Agents
13 //_/_/ Reykjavik University, Menntavegur 1, 102 Reykjavik, Iceland
14 //_/_/ http://cadia.ru.is
15 //_/_/
16 //_/_/ Part of this software was developed by Eric Nivel
17 //_/_/ in the HUMANOBS EU research project, which included
18 //_/_/ the following parties:
19 //_/_/
20 //_/_/ Autonomous Systems Laboratory
21 //_/_/ Technical University of Madrid, Spain
22 //_/_/ http://www.aslab.org/
23 //_/_/
24 //_/_/ Communicative Machines
25 //_/_/ Edinburgh, United Kingdom
26 //_/_/ http://www.cmlabs.com/
27 //_/_/
28 //_/_/ Istituto Dalle Molle di Studi sull'Intelligenza Artificiale
29 //_/_/ University of Lugano and SUPSI, Switzerland
30 //_/_/ http://www.idsia.ch/
31 //_/_/
32 //_/_/ Institute of Cognitive Sciences and Technologies
33 //_/_/ Consiglio Nazionale delle Ricerche, Italy
34 //_/_/ http://www.istc.cnr.it/
35 //_/_/
36 //_/_/ Dipartimento di Ingegneria Informatica
37 //_/_/ University of Palermo, Italy
38 //_/_/ http://diid.unipa.it/roboticslab/
39 //_/_/
40 //_/_/
41 //_/_/ --- HUMANOBS Open-Source BSD License, with CADIA Clause v 1.0 ---
42 //_/_/
43 //_/_/ Redistribution and use in source and binary forms, with or without
44 //_/_/ modification, is permitted provided that the following conditions
45 //_/_/ are met:
46 //_/_/ - Redistributions of source code must retain the above copyright
47 //_/_/ and collaboration notice, this list of conditions and the
48 //_/_/ following disclaimer.
49 //_/_/ - Redistributions in binary form must reproduce the above copyright
50 //_/_/ notice, this list of conditions and the following disclaimer
51 //_/_/ in the documentation and/or other materials provided with
52 //_/_/ the distribution.
53 //_/_/
54 //_/_/ - Neither the name of its copyright holders nor the names of its
55 //_/_/ contributors may be used to endorse or promote products
56 //_/_/ derived from this software without specific prior
57 //_/_/ written permission.
58 //_/_/
59 //_/_/ - CADIA Clause: The license granted in and to the software
60 //_/_/ under this agreement is a limited-use license.
61 //_/_/ The software may not be used in furtherance of:
62 //_/_/ (i) intentionally causing bodily injury or severe emotional
63 //_/_/ distress to any person;
64 //_/_/ (ii) invading the personal privacy or violating the human
65 //_/_/ rights of any person; or
66 //_/_/ (iii) committing or preparing for any act of war.
67 //_/_/
68 //_/_/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
69 //_/_/ CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
70 //_/_/ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
71 //_/_/ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
72 //_/_/ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
73 //_/_/ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
74 //_/_/ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
75 //_/_/ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
76 //_/_/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77 //_/_/ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78 //_/_/ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 //_/_/ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
80 //_/_/ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
81 //_/_/ OF SUCH DAMAGE.
82 //_/_/
83 //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
84 
85 #ifndef preprocessor_h
86 #define preprocessor_h
87 
88 #include "segments.h"
89 #include <istream>
90 #include <sstream>
91 #include <fstream>
92 #include <list>
93 
94 namespace r_comp {
95 
96 class RepliMacro;
97 class RepliCondition;
98 class RepliStruct {
99 public:
100  static std::unordered_map<std::string, RepliMacro *> RepliMacros_;
101  static std::unordered_map<std::string, int32> Counters_;
102  static std::list<RepliCondition *> Conditions_;
103  static uint32 GlobalLine_;
104  static std::vector<std::string> LoadedFilePaths_;
105 
106  enum Type { Root, Structure, Set, Atom, Directive, Condition, Development };
107  Type type_;
108  std::string cmd_;
109  std::string tail_;
110  std::string label_;
111  std::string error_;
112  uint32 line_;
113  std::list<RepliStruct *> args_;
114  RepliStruct *parent_;
115  std::string filePath_;
116 
117  RepliStruct(RepliStruct::Type type);
118  ~RepliStruct();
119 
120  void reset(); // remove rags that are objects.
121 
122  uint32 getIndent(std::istream *stream);
123  int32 parse(std::istream *stream, const std::string& file_path, uint32 &cur_indent, uint32 &prev_indent, int32 param_expect = 0);
124  bool parseDirective(std::istream *stream, const std::string& file_path, uint32 &cur_indent, uint32 &prev_indent);
125  int32 process();
126 
127  RepliStruct *findAtom(const std::string &name);
128 
136  RepliStruct *loadReplicodeFile(const std::string &filename);
137 
146  static bool isFileLoaded(const std::string& file_path);
147 
148  RepliStruct *clone() const;
149  std::string print() const;
150  std::string printError() const;
151 
152  friend std::ostream& operator<<(std::ostream &os, const RepliStruct &structure);
153  friend std::ostream& operator<<(std::ostream &os, RepliStruct *structure);
154 };
155 
156 class RepliMacro {
157 public:
158  std::string name_;
159  RepliStruct *src_;
160  RepliStruct *dest_;
161  std::string error_;
162 
163  RepliMacro(const std::string &name, RepliStruct *src, RepliStruct *dest);
164  ~RepliMacro();
165 
166  uint32 argCount();
167  RepliStruct *expandMacro(RepliStruct *old_struct);
168 };
169 
171 public:
172  std::string name_;
173  bool reversed_;
174 
175  RepliCondition(const std::string &name, bool reversed);
176  ~RepliCondition();
177  void reverse();
178  bool isActive(std::unordered_map<std::string, RepliMacro*> &repli_macros, std::unordered_map<std::string, int32> &counters);
179 };
180 
181 class dll_export Preprocessor {
182 private:
183  typedef enum {
184  T_CLASS = 0,
185  T_SYS_CLASS = 1,
186  T_SET = 2
187  }ClassType;
188  Metadata *metadata_;
189  uint16 class_opcode_; // shared with sys_classes_
190  std::unordered_map<std::string, RepliStruct *> template_classes_;
191  void instantiateClass(RepliStruct *tpl_class, std::list<RepliStruct *> &tpl_args, std::string &instantiated_class_name);
192  bool isSet(std::string class_name);
193  bool isTemplateClass(RepliStruct *s);
194  void getMember(std::vector<StructureMember> &members, RepliStruct *m, std::list<RepliStruct *> &tpl_args, bool instantiate);
195  void getMembers(RepliStruct *s, std::vector<StructureMember> &members, std::list<RepliStruct *> &tpl_args, bool instantiate);
196  ReturnType getReturnType(RepliStruct *s);
197  void initialize(Metadata *metadata); // init definition_segment
198 public:
199  RepliStruct *root_;
200 
201  Preprocessor();
202  ~Preprocessor();
203  bool process(std::istream *stream, // if an ifstream, stream must be open.
204  const std::string& filePath, // the file path of the ifstream.
205  std::ostringstream *outstream, // output stream=input stream where macros are expanded.
206  std::string &error, // set when function fails, e.g. returns false.
207  Metadata *metadata = NULL); // process will fill class_image, or use the exiting one if NULL.
208 };
209 }
210 
211 
212 #endif
r_comp::Preprocessor
Definition: preprocessor.h:181
r_comp::RepliMacro
Definition: preprocessor.h:156
r_comp::RepliStruct::loadReplicodeFile
RepliStruct * loadReplicodeFile(const std::string &filename)
Definition: preprocessor.cpp:783
r_comp::RepliStruct::isFileLoaded
static bool isFileLoaded(const std::string &file_path)
Definition: preprocessor.cpp:811
r_comp::RepliCondition
Definition: preprocessor.h:170
r_comp::Metadata
Definition: segments.h:109
r_comp::RepliStruct
Definition: preprocessor.h:98