AERA
hlp_context.cpp
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 #include "hlp_context.h"
86 #include "operator.h"
87 #include "opcodes.h"
88 
89 
90 namespace r_exec {
91 
92 HLPContext::HLPContext() : _Context(NULL, 0, NULL, UNDEFINED) {
93 }
94 
95 HLPContext::HLPContext(Atom *code, uint16 index, HLPOverlay *const overlay, Data data) : _Context(code, index, overlay, data) {
96 }
97 
98 bool HLPContext::operator ==(const HLPContext &c) const {
99 
100  HLPContext lhs = dereference();
101  HLPContext rhs = c.dereference();
102 
103  if (lhs[0] != rhs[0]) // both contexts point to an atom which is not a pointer.
104  return false;
105 
106  if (lhs[0].isStructural()) { // both are structural.
107 
108  uint16 atom_count = lhs.get_children_count();
109  for (uint16 i = 1; i <= atom_count; ++i)
110  if (lhs.get_child_deref(i) != rhs.get_child_deref(i))
111  return false;
112  return true;
113  }
114  return true;
115 }
116 
117 bool HLPContext::operator !=(const HLPContext &c) const {
118 
119  return !(*this == c);
120 }
121 
122 HLPContext HLPContext::dereference() const {
123 
124  switch ((*this)[0].getDescriptor()) {
125  case Atom::I_PTR:
126  return HLPContext(code_, (*this)[0].asIndex(), (HLPOverlay *)overlay_, data_).dereference();
127  case Atom::VL_PTR: {
128  Atom *value_code = ((HLPOverlay *)overlay_)->get_value_code((*this)[0].asIndex());
129  if (value_code)
130  return HLPContext(value_code, 0, (HLPOverlay *)overlay_, BINDING_MAP).dereference();
131  else // unbound variable.
132  return HLPContext(); // data=undefined: evaluation will return false.
133  }case Atom::VALUE_PTR:
134  return HLPContext(&overlay_->values_[0], (*this)[0].asIndex(), (HLPOverlay *)overlay_, VALUE_ARRAY).dereference();
135  default:
136  return *this;
137  }
138 }
139 
140 bool HLPContext::evaluate_no_dereference() const {
141 
142  switch (data_) {
143  case VALUE_ARRAY:
144  case BINDING_MAP:
145  return true;
146  case UNDEFINED:
147  return false;
148  }
149 
150  switch (code_[index_].getDescriptor()) {
151  case Atom::ASSIGN_PTR: {
152 
153  HLPContext c(code_, code_[index_].asIndex(), (HLPOverlay *)overlay_);
154  if (c.evaluate_no_dereference()) {
155 
156  ((HLPOverlay *)overlay_)->bindings_->bind_variable(code_, code_[index_].asAssignmentIndex(), code_[index_].asIndex(), &overlay_->values_[0]);
157  return true;
158  } else if (((HLPOverlay*)overlay_)->bindings_->scan_variable(code_[index_].asAssignmentIndex())) {
159  // The assignment expression could not be evaluated, but the assignment variable is already bound.
160  return true;
161  } else
162  return false;
163  }case Atom::OPERATOR: {
164 
165  uint16 atom_count = get_children_count();
166  for (uint16 i = 1; i <= atom_count; ++i) {
167 
168  if (!get_child_deref(i).evaluate_no_dereference())
169  return false;
170  }
171 
172  Operator op = Operator::Get((*this)[0].asOpcode());
173  HLPContext *c = new HLPContext(*this);
174  Context _c(c);
175  return op(_c);
176  }case Atom::OBJECT:
177  case Atom::MARKER:
178  case Atom::INSTANTIATED_PROGRAM:
179  case Atom::INSTANTIATED_CPP_PROGRAM:
180  case Atom::INSTANTIATED_INPUT_LESS_PROGRAM:
181  case Atom::INSTANTIATED_ANTI_PROGRAM:
182  case Atom::COMPOSITE_STATE:
183  case Atom::MODEL:
184  case Atom::GROUP:
185  case Atom::SET:
186  case Atom::S_SET: {
187 
188  uint16 atom_count = get_children_count();
189  for (uint16 i = 1; i <= atom_count; ++i) {
190 
191  if (!get_child_deref(i).evaluate_no_dereference())
192  return false;
193  }
194  return true;
195  }default:
196  return true;
197  }
198 }
199 
200 uint16 HLPContext::get_object_code_size() const {
201 
202  switch (data_) {
203  case STEM:
204  return ((HLPOverlay *)overlay_)->get_unpacked_object()->code_size();
205  case BINDING_MAP:
206  return ((HLPOverlay *)overlay_)->get_value_code_size((*this)[0].asIndex());
207  case VALUE_ARRAY:
208  return overlay_->values_.size();
209  default:
210  return 0;
211  }
212 }
213 }
r_exec::HLPContext::get_child_deref
HLPContext get_child_deref(uint16 index) const
Definition: hlp_context.h:124