replicode
compiler.h
Go to the documentation of this file.
1 // compiler.h
2 //
3 // Author: Eric Nivel
4 //
5 // BSD license:
6 // Copyright (c) 2010, Eric Nivel
7 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are met:
10 //
11 // - Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // - Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Eric Nivel nor the
17 // names of their contributors may be used to endorse or promote products
18 // derived from this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 // DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
24 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef compiler_h
32 #define compiler_h
33 
34 #include <fstream>
35 #include <sstream>
36 
37 #include "out_stream.h"
38 #include "segments.h"
39 
40 
41 using namespace r_code;
42 
43 namespace r_comp {
44 
45 class RepliStruct;
46 
48 private:
49  std::string error;
50  std::string m_errorFile;
52 
53  bool trace;
54 
55  Class current_class; // the sys-class currently parsed.
56  ImageObject *current_object; // the sys-object currently parsed.
57  uint64_t current_object_index; // ordinal of the current sys-object in the code segment.
58  int64_t current_view_index; // ordinal of a view in the sys-object's view set.
59 
62 
63  class State {
64  public:
65  State(): pattern_lvl(0),
66  no_arity_check(false) {}
67  State(Compiler *c): pattern_lvl(c->state.pattern_lvl),
68  no_arity_check(c->state.no_arity_check) {}
69  uint16_t pattern_lvl; // add one when parsing skel in (ptn skel guards), sub one when done.
70  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.
71  };
72 
74  State save_state(); // called before trying to read an expression.
75  void restore_state(State s); // called after failing to read an expression.
76 
77  void set_error(const std::string &s, RepliStruct *node);
78  void set_arity_error(RepliStruct *node, uint16_t expected, uint16_t got);
79 
81  std::unordered_map<std::string, Reference> local_references;
82 
83  // labels declared outside sys-objects. translate to reference pointers.
84  std::unordered_map<std::string, Reference> global_references;
85  bool addLocalReference(const std::string reference_name, const uint16_t index, const Class &p); // detect cast.
86  bool getGlobalReferenceIndex(const std::string reference_name, const ReturnType t, ImageObject *object, uint16_t &index, Class *&_class); // index points to the reference set.
87 // return false when not found.
88 
90  bool in_hlp;
91  std::vector<std::string> hlp_references;
92  uint32_t add_hlp_reference(std::string reference_name);
93  uint8_t get_hlp_reference(std::string reference_name);
94 
95 // Utility.
96  bool read_nil(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
97  bool read_nil_set(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
98  bool read_nil_nb(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
99  bool read_nil_us(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
100  bool read_forever_nb(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
101  bool read_nil_nid(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
102  bool read_nil_did(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
103  bool read_nil_fid(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
104  bool read_nil_bl(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
105  bool read_nil_st(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
106  bool read_variable(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write, const Class p);
107  bool read_reference(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write, const ReturnType t);
108  bool read_wildcard(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
109  bool read_tail_wildcard(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write);
110 
111  bool err; // set to true when parsing fails in the functions below.
112 
113 // All functions below return false (a) upon eof or, (b) when the class structure is not matched; in both cases, characters are pushed back.
114 
115 // Lexical units.
116  bool local_reference(RepliStruct *node, uint16_t &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).
117  bool global_reference(RepliStruct *node, uint16_t &index, const ReturnType t); // no conformance: return type==ANY.
118  bool hlp_reference(RepliStruct *node, uint16_t &index);
119  bool this_indirection(RepliStruct *node, std::vector<int16_t> &v, const ReturnType t); // ex: this.res.
120  bool local_indirection(RepliStruct *node, std::vector<int16_t> &v, const ReturnType t, uint32_t &cast_opcode); // ex: p.res where p is a label/variable declared within the object; cast_opcode=0x0FFF if no cast.
121  bool global_indirection(RepliStruct *node, std::vector<int16_t> &v, const ReturnType t); // ex: p.res where p is a label/variable declared outside the object.
122  bool object(RepliStruct *node, Class &p); // looks first in sys_objects, then in objects.
123  bool object(RepliStruct *node, const Class &p); // must conform to p.
124  bool sys_object(RepliStruct *node, Class &p); // looks only in sys_objects.
125  bool sys_object(RepliStruct *node, const Class &p); // must conform to p.
126  bool marker(RepliStruct *node, Class &p);
127  bool op(RepliStruct *node, Class &p, const ReturnType t); // operator; must conform to t. return true if type matches t or ANY.
128  bool op(RepliStruct *node, const Class &p); // must conform to p.
129  bool function(RepliStruct *node, Class &p); // device function.
130  bool expression_head(RepliStruct *node, Class &p, const ReturnType t); // starts from the first element; arity does not count the head; must conform to t.
131  bool expression_head(RepliStruct *node, const Class &p); // starts from the first element; arity does not count the head; must conform to p.
132  bool expression_tail(RepliStruct *node, const Class &p, uint16_t write_index, uint16_t &extent_index, bool write); // starts from the second element; must conform to p.
133 
134 // Structural units; check for heading labels.
135  bool expression(RepliStruct *node, const ReturnType t, uint16_t write_index, uint16_t &extent_index, bool write); // must conform to t.
136  bool expression(RepliStruct *node, const Class &p, uint16_t write_index, uint16_t &extent_index, bool write); // must conform to p.
137  bool set(RepliStruct *node, uint16_t write_index, uint16_t &extent_index, bool write); // no conformance, i.e. set of anything. [ ] is illegal; use |[] instead, or [nil].
138  bool set(RepliStruct *node, const Class &p, uint16_t write_index, uint16_t &extent_index, bool write); // must conform to p. for class defs like member-name:[member-list] or !class (name[] member-list).
139 
140  bool read(RepliStruct *node, const StructureMember &m, bool enforce, uint16_t write_index, uint16_t &extent_index, bool write);
141 
142  bool read_sys_object(RepliStruct *node, RepliStruct *view); // compiles one object; return false when there is an error.
143 public:
144  Compiler();
145 
146  bool compile(RepliStruct *rootNode, // stream must be open.
147  r_comp::Image *_image,
148  r_comp::Metadata *_metadata,
149  std::string &error,
150  bool trace); // set when compile() fails, e.g. returns false.
151 
152  std::string getError();
153 
154 // Read functions for defining structure members.
155 // 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.
156 // indented: flag indicating if an indent has been found, meaning that a matching indent will have to be enforced.
157 // enforce: set to true when the stream content has to conform with the type xxx in read_xxx.
158 // _class: specifies the elements that shall compose a structure (expression or set).
159 // 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).
160 // extent_index: the index where to write data belonging to a structure (the internal pointer is written at write_index).
161 // write: when false, no writing in code->data is performed (needed by set_element_count()).
162  bool read_any(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write); // calls all of the functions below.
163  bool read_number(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
164  bool read_timestamp(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write); // p always NULL
165  bool read_boolean(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
166  bool read_string(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
167  bool read_node(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
168  bool read_device(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
169  bool read_function(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
170  bool read_expression(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
171  bool read_set(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
172  bool read_class(RepliStruct *node, bool enforce, const Class *p, uint16_t write_index, uint16_t &extent_index, bool write);
173 
174 // Convenience to retrieve axiom names by index.
175  std::string getObjectName(const uint16_t index) const;
176 };
177 }
178 
179 
180 #endif
Definition: segments.h:119
ReturnType
Definition: structure_member.h:48
uint16_t pattern_lvl
Definition: compiler.h:69
Definition: compiler.h:63
std::unordered_map< std::string, Reference > local_references
labels and variables declared inside objects (cleared before parsing each sys-object): translate to v...
Definition: compiler.h:81
Definition: class.h:41
bool in_hlp
In high-level pattern.
Definition: compiler.h:90
ImageObject * current_object
Definition: compiler.h:56
#define dll_export
Definition: dll.h:44
Definition: atom.cpp:36
Definition: compiler.h:47
int m_errorLine
Definition: compiler.h:51
State(Compiler *c)
Definition: compiler.h:67
std::unordered_map< std::string, Reference > global_references
Definition: compiler.h:84
Definition: object.h:49
Definition: segments.h:59
bool err
Definition: compiler.h:111
bool trace
Definition: compiler.h:53
dll_export r_comp::Compiler Compiler
Definition: init.cpp:66
std::string m_errorFile
Definition: compiler.h:50
bool no_arity_check
Definition: compiler.h:70
r_comp::Metadata * _metadata
Definition: compiler.h:61
State()
Definition: compiler.h:65
Definition: structure_member.h:64
Class current_class
Definition: compiler.h:55
std::string error
Definition: compiler.h:49
uint64_t current_object_index
Definition: compiler.h:57
r_comp::Image * _image
Definition: compiler.h:60
Definition: image.h:41
Definition: replistruct.h:14
int64_t current_view_index
Definition: compiler.h:58
State state
Definition: compiler.h:73
std::vector< std::string > hlp_references
Definition: compiler.h:91