AERA
All Classes Functions Pages
compiler.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 compiler_h
86 #define compiler_h
87 
88 #include <fstream>
89 #include <sstream>
90 
91 #include "out_stream.h"
92 #include "segments.h"
93 #include "../r_code/image.h"
94 
95 namespace r_comp {
96 
97 class dll_export Compiler {
98 private:
99 
100  std::istream *in_stream_;
101  std::string error_;
102 
103  bool trace_;
104 
105  Class current_class_; // the sys-class currently parsed.
106  r_code::ImageObject *current_object_; // the sys-object currently parsed.
107  uint32 current_object_index_; // ordinal of the current sys-object in the code segment.
108  int32 current_view_index_; // ordinal of a view in the sys-object's view set.
109 
110  r_comp::Image *image_;
111  r_comp::Metadata *metadata_;
112 
113  class State {
114  public:
115  State() : indents(0),
116  right_indents_ahead(0),
117  left_indents_ahead(0),
118  pattern_lvl(0),
119  no_arity_check(false) {}
120  State(Compiler *c) : indents(c->state_.indents),
121  right_indents_ahead(c->state_.right_indents_ahead),
122  left_indents_ahead(c->state_.left_indents_ahead),
123  pattern_lvl(c->state_.pattern_lvl),
124  no_arity_check(c->state_.no_arity_check),
125  stream_ptr(c->in_stream_->tellg()) {}
126  uint16 indents; // 1 indent = 3 char.
127  uint16 right_indents_ahead; // parsing right indents may unveil more 3-char groups than needed: that's some indents ahead. Avoids requiring a newline for each indent.
128  uint16 left_indents_ahead; // as above.
129  uint16 pattern_lvl; // add one when parsing skel in (ptn skel guards), sub one when done.
130  bool no_arity_check; // set to true when a tail wildcard is encountered while parsing an expression, set back to false when done parsing the expression.
131  std::streampos stream_ptr;
132  };
133 
134  State state_;
135  State save_state(); // called before trying to read an expression.
136  void restore_state(State s); // called after failing to read an expression.
137 
138  void set_error(const std::string &s);
139  void set_arity_error(uint16 expected, uint16 got);
140 
141  std::unordered_map<std::string, Reference> local_references_; // labels and variables declared inside objects (cleared before parsing each sys-object): translate to value pointers.
142  std::unordered_map<std::string, Reference> global_references_; // labels declared outside sys-objects. translate to reference pointers.
143  void addLocalReference(const std::string reference_name, const uint16 index, const Class &p); // detect cast.
144  bool getGlobalReferenceIndex(const std::string reference_name, const ReturnType t, r_code::ImageObject *object, uint16 &index, Class *&_class); // index points to the reference set.
145  // return false when not found.
146  bool in_hlp_;
147  bool allow_variables_and_wildcards_outside_pattern_skeleton_;
148  std::vector<std::string> hlp_references_;
149  uint8 add_hlp_reference(std::string reference_name);
150  uint8 get_hlp_reference(std::string reference_name);
151 
152  // Utility.
153  bool read_nil(uint16 write_index, uint16 &extent_index, bool write);
154  bool read_nil_set(uint16 write_index, uint16 &extent_index, bool write);
155  bool read_nil_nb(uint16 write_index, uint16 &extent_index, bool write);
156  bool read_nil_ts(uint16 write_index, uint16 &extent_index, bool write);
157  bool read_forever_nb(uint16 write_index, uint16 &extent_index, bool write);
158  bool read_nil_nid(uint16 write_index, uint16 &extent_index, bool write);
159  bool read_nil_did(uint16 write_index, uint16 &extent_index, bool write);
160  bool read_nil_fid(uint16 write_index, uint16 &extent_index, bool write);
161  bool read_nil_bl(uint16 write_index, uint16 &extent_index, bool write);
162  bool read_nil_st(uint16 write_index, uint16 &extent_index, bool write);
163  bool read_variable(uint16 write_index, uint16 &extent_index, bool write, const Class p);
164  bool read_reference(uint16 write_index, uint16 &extent_index, bool write, const ReturnType t);
165  bool read_wildcard(uint16 write_index, uint16 &extent_index, bool write);
166  bool read_tail_wildcard(uint16 write_index, uint16 &extent_index, bool write);
167 
168  bool err_; // set to true when parsing fails in the functions below.
169 
170  // All functions below return false (a) upon eof or, (b) when the class structure is not matched; in both cases, characters are pushed back.
171  // Sub-lexical units
172  bool comment(); // pull comments out of the stream.
173  bool separator(bool pushback); // blank space or indent.
174  bool right_indent(bool pushback); // newline + 3 blank spaces wrt indents_.top().
175  bool left_indent(bool pushback); // newline - 3 blank spaces wrt indents_.top().
176  bool indent(bool pushback); // newline + same number of 3 blank spaces as given by indents_.top().
177  bool expression_begin(bool &indented); // ( or right_indent.
178  bool expression_end(bool indented); // ) or left_indent.
179  bool set_begin(bool &indented); // [ or []+right_indent.
180  bool set_end(bool indented); // ] or left_indent.
181  bool symbol_expr(std::string &s); // finds any symbol s; detects trailing blanks, newline and ).
182  bool symbol_expr_set(std::string &s); // finds any symbol s; detects trailing blanks, newline, ) and ].
183  bool match_symbol_separator(const char *symbol, bool pushback); // matches a symbol followed by a separator/left/right indent; separator/left/right indent is pushed back.
184  bool match_symbol(const char *symbol, bool pushback); // matches a symbol regardless of what follows.
185  bool member(std::string &s); // finds a string possibly followed by ., blanks, newline, ) and ].
186 
187 // Some 3rd party libraries do define these macros.
188 #undef nil
189 #undef forever
190 
191  // Lexical units.
192  bool nil();
193  bool nil_nb();
194  bool nil_ts();
195  bool forever();
196  bool nil_nid();
197  bool nil_did();
198  bool nil_fid();
199  bool nil_bl();
200  bool nil_st();
201  bool label(std::string &l);
202  bool variable(std::string &v);
203  bool this_();
204  bool local_reference(uint16 &index, const ReturnType t); // must conform to t; indicates if the ref is to ba valuated in the value array (in_pattern set to true).
205  bool global_reference(uint16 &index, const ReturnType t); // no conformance: return type==ANY.
206  bool hlp_reference(uint16 &index);
207  bool this_indirection(std::vector<int16> &v, const ReturnType t); // ex: this.res.
208  bool local_indirection(std::vector<int16> &v, const ReturnType t, uint16 &cast_opcode); // ex: p.res where p is a label/variable declared within the object; cast_opcode=0x0FFF if no cast.
209  bool global_indirection(std::vector<int16> &v, const ReturnType t); // ex: p.res where p is a label/variable declared outside the object.
210  bool wildcard();
211  bool tail_wildcard();
217  bool duration(int64 &result);
225  bool timestamp_s_ms_us(int64 &ts);
226  bool str(std::string &s);
227  bool number(float32 &n);
228  bool hex(uint32 &h);
229  bool boolean(bool &b);
230  bool object(Class &p); // looks first in sys_objects, then in objects.
231  bool object(const Class &p); // must conform to p.
232  bool sys_object(Class &p); // looks only in sys_objects.
233  bool sys_object(const Class &p); // must conform to p.
234  bool marker(Class &p);
235  bool op(Class &p, const ReturnType t); // operator; must conform to t.
236  bool op(const Class &p); // must conform to p.
237  bool function(Class &p); // device function.
238  bool expression_head(Class &p, const ReturnType t); // starts from the first element; arity does not count the head; must conform to t.
239  bool expression_head(const Class &p); // starts from the first element; arity does not count the head; must conform to p.
240  bool expression_tail(bool indented, const Class &p, uint16 write_index, uint16 &extent_index, bool write); // starts from the second element; must conform to p.
241 
242  // Structural units; check for heading labels.
243  bool expression(bool &indented, const ReturnType t, uint16 write_index, uint16 &extent_index, bool write); // must conform to t.
244  bool expression(bool &indented, const Class &p, uint16 write_index, uint16 &extent_index, bool write); // must conform to p.
245  bool set(bool &indented, uint16 write_index, uint16 &extent_index, bool write); // no conformance, i.e. set of anything.
246  bool set(bool &indented, const Class &p, uint16 write_index, uint16 &extent_index, bool write); // must conform to p.
247 
248  uint8 set_element_count(bool indented); // returns the number of elements in a set; parses the stream (write set to false) until it finds the end of the set and rewinds (write set back to true).
249  bool read(const StructureMember &m, bool &indented, bool enforce, uint16 write_index, uint16 &extent_index, bool write);
250 
251  OutStream *out_stream_;
252 
253  bool read_sys_object(); // compiles one object; return false when there is an error.
254 public:
264  Compiler(bool allow_variables_and_wildcards_outside_pattern_skeleton = false);
265  ~Compiler();
266 
267  bool compile(std::istream *stream, // stream must be open.
268  r_comp::Image *image,
269  r_comp::Metadata *metadata,
270  std::string &error,
271  bool trace); // set when compile() fails, e.g. returns false.
272 
273 // Read functions for defining structure members.
274 // Always try to read nil (typed), a variable, a wildcrad or a tail wildcard first; then try to read the lexical unit; then try to read an expression returning the appropriate type.
275 // indented: flag indicating if an indent has been found, meaning that a matching indent will have to be enforced.
276 // enforce: set to true when the stream content has to conform with the type xxx in read_xxx.
277 // _class: specifies the elements that shall compose a structure (expression or set).
278 // write_index: the index where the r-atom shall be written (atomic data), or where an internal pointer to a structure shall be written (structural data).
279 // extent_index: the index where to write data belonging to a structure (the internal pointer is written at write_index).
280 // write: when false, no writing in code->data is performed (needed by set_element_count()).
281  bool read_any(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write); // calls all of the functions below.
282  bool read_number(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
283  bool read_timestamp(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
284  bool read_duration(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
285  bool read_boolean(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
286  bool read_string(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
287  bool read_node(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
288  bool read_device(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
289  bool read_function(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
290  bool read_expression(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
291  bool read_set(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
292  bool read_class(bool &indented, bool enforce, const Class *p, uint16 write_index, uint16 &extent_index, bool write);
293 
294  // Convenience to retrieve axiom names by index.
295  std::string getObjectName(const uint16 index) const;
296 };
297 }
298 
299 
300 #endif
r_comp::Compiler
Definition: compiler.h:97
r_code::ImageObject
Definition: r_code/object.h:106
r_comp::Image
Definition: segments.h:177
r_comp::StructureMember
Definition: structure_member.h:115
r_comp::OutStream
Definition: out_stream.h:97
r_comp::Metadata
Definition: segments.h:109
r_comp::Class
Definition: class.h:96