replicode
operator.h
Go to the documentation of this file.
1 // operator.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 operator_h
32 #define operator_h
33 
34 #include "../r_code/object.h"
35 
36 #include "_context.h"
37 
38 
39 namespace r_exec {
40 
41 // Wrapper class for evaluation contexts.
42 // Template operator functions is not an option since some operators are defined in usr_operators.dll.
44 private:
46 public:
47  Context(_Context *implementation): implementation(implementation) {}
48  virtual ~Context() {
49  delete implementation;
50  }
51 
53  return implementation;
54  }
55 
56  uint16_t getChildrenCount() const {
57  return implementation->getChildrenCount();
58  }
59  Context getChild(uint16_t index) const {
60  return Context(implementation->_getChild(index));
61  }
62 
63  Context operator *() const {
64  return Context(implementation->dereference());
65  }
66  Context &operator =(const Context &c) {
67 
68  delete implementation;
69  implementation = implementation->assign(c.get_implementation());
70  return *this;
71  }
72 
73  bool operator ==(const Context &c) const {
74  return implementation->equal(c.get_implementation());
75  }
76  bool operator !=(const Context &c) const {
77  return !implementation->equal(c.get_implementation());
78  }
79 
80  Atom &operator [](uint16_t i) const {
81  return implementation->get_atom(i);
82  }
83 
84  uint16_t setAtomicResult(Atom a) const {
85  return implementation->setAtomicResult(a);
86  }
87  uint16_t setTimestampResult(uint64_t t) const {
88  return implementation->setTimestampResult(t);
89  }
90  uint16_t setCompoundResultHead(Atom a) const {
91  return implementation->setCompoundResultHead(a);
92  }
93  uint16_t addCompoundResultPart(Atom a) const {
94  return implementation->addCompoundResultPart(a);
95  }
96 
97  void trace() const {
98  return implementation->trace();
99  }
100 };
101 
102 bool red(const Context &context, uint16_t &index); // executive-dependent.
103 
104 bool syn(const Context &context, uint16_t &index);
105 
106 class Operator {
107 private:
108  static r_code::vector<Operator> Operators; // indexed by opcodes.
109 
110  bool (*_operator)(const Context &, uint16_t &);
111  bool (*_overload)(const Context &, uint16_t &);
112 public:
113  static void Register(uint16_t opcode, bool (*op)(const Context &, uint16_t &)); // first, register std operators; next register user-defined operators (may be registered as overloads).
114  static Operator Get(uint16_t opcode) {
115  return Operators[opcode];
116  }
117  Operator(): _operator(NULL), _overload(NULL) {}
118  Operator(bool (*o)(const Context &, uint16_t &)): _operator(o), _overload(NULL) {}
120 
121  void setOverload(bool (*o)(const Context &, uint16_t &)) {
122  _overload = o;
123  }
124 
125  bool operator()(const Context &context, uint16_t &index) const {
126  if (_operator(context, index))
127  return true;
128  if (_overload)
129  return _overload(context, index);
130  return false;
131  }
132 
133  bool is_red() const {
134  return _operator == red;
135  }
136  bool is_syn() const {
137  return _operator == syn;
138  }
139 };
140 
141 // std operators ////////////////////////////////////////
142 
143 bool now(const Context &context, uint16_t &index);
144 
145 bool rnd(const Context &context, uint16_t &index);
146 
147 bool equ(const Context &context, uint16_t &index);
148 bool neq(const Context &context, uint16_t &index);
149 bool gtr(const Context &context, uint16_t &index);
150 bool lsr(const Context &context, uint16_t &index);
151 bool gte(const Context &context, uint16_t &index);
152 bool lse(const Context &context, uint16_t &index);
153 
154 bool add(const Context &context, uint16_t &index);
155 bool sub(const Context &context, uint16_t &index);
156 bool mul(const Context &context, uint16_t &index);
157 bool div(const Context &context, uint16_t &index);
158 
159 bool dis(const Context &context, uint16_t &index);
160 
161 bool ln(const Context &context, uint16_t &index);
162 bool exp(const Context &context, uint16_t &index);
163 bool log(const Context &context, uint16_t &index);
164 bool e10(const Context &context, uint16_t &index);
165 
166 bool ins(const Context &context, uint16_t &index); // executive-dependent.
167 
168 bool fvw(const Context &context, uint16_t &index); // executive-dependent.
169 }
170 
171 
172 #endif
Operator()
Definition: operator.h:117
virtual _Context * _getChild(uint16_t index) const =0
bool fvw(const Context &context, uint16_t &index)
Definition: operator.cpp:597
bool add(const Context &context, uint16_t &index)
Definition: operator.cpp:220
bool neq(const Context &context, uint16_t &index)
Definition: operator.cpp:95
uint16_t getChildrenCount() const
Definition: operator.h:56
bool now(const Context &context, uint16_t &index)
Definition: operator.cpp:55
uint16_t setTimestampResult(uint64_t t) const
Definition: _context.cpp:42
Definition: _context.h:45
bool gtr(const Context &context, uint16_t &index)
Definition: operator.cpp:104
#define dll_export
Definition: dll.h:44
bool(* _overload)(const Context &, uint16_t &)
Definition: operator.h:111
Context getChild(uint16_t index) const
Definition: operator.h:59
static Operator Get(uint16_t opcode)
Definition: operator.h:114
Definition: _context.cpp:34
bool exp(const Context &context, uint16_t &index)
Definition: operator.cpp:525
uint16_t setAtomicResult(Atom a) const
Definition: operator.h:84
uint16_t setCompoundResultHead(Atom a) const
Definition: _context.cpp:51
bool lsr(const Context &context, uint16_t &index)
Definition: operator.cpp:133
static r_code::vector< Operator > Operators
Definition: operator.h:108
uint16_t setCompoundResultHead(Atom a) const
Definition: operator.h:90
bool is_red() const
Definition: operator.h:133
virtual ~Context()
Definition: operator.h:48
Operator(bool(*o)(const Context &, uint16_t &))
Definition: operator.h:118
bool dis(const Context &context, uint16_t &index)
Definition: operator.cpp:477
bool syn(const Context &context, uint16_t &index)
Definition: operator.cpp:576
void setOverload(bool(*o)(const Context &, uint16_t &))
Definition: operator.h:121
bool(* _operator)(const Context &, uint16_t &)
Definition: operator.h:110
bool operator()(const Context &context, uint16_t &index) const
Definition: operator.h:125
virtual _Context * assign(const _Context *c)=0
Definition: operator.h:106
void trace() const
Definition: operator.h:97
virtual Atom & get_atom(uint16_t i) const =0
Definition: atom.h:45
~Operator()
Definition: operator.h:119
bool red(const Context &context, uint16_t &index)
Definition: operator.cpp:590
_Context * get_implementation() const
Definition: operator.h:52
uint16_t addCompoundResultPart(Atom a) const
Definition: _context.cpp:59
bool is_syn() const
Definition: operator.h:136
uint16_t setTimestampResult(uint64_t t) const
Definition: operator.h:87
bool lse(const Context &context, uint16_t &index)
Definition: operator.cpp:191
bool div(const Context &context, uint16_t &index)
Definition: operator.cpp:390
virtual uint16_t getChildrenCount() const =0
Definition: vector.h:40
bool rnd(const Context &context, uint16_t &index)
Definition: operator.cpp:63
bool e10(const Context &context, uint16_t &index)
Definition: operator.cpp:560
virtual _Context * dereference() const =0
uint16_t addCompoundResultPart(Atom a) const
Definition: operator.h:93
bool log(const Context &context, uint16_t &index)
Definition: operator.cpp:541
Context(_Context *implementation)
Definition: operator.h:47
static void Register(uint16_t opcode, bool(*op)(const Context &, uint16_t &))
Definition: operator.cpp:45
uint16_t setAtomicResult(Atom a) const
Definition: _context.cpp:36
bool ins(const Context &context, uint16_t &index)
Definition: operator.cpp:583
bool ln(const Context &context, uint16_t &index)
Definition: operator.cpp:506
_Context * implementation
Definition: operator.h:45
bool equ(const Context &context, uint16_t &index)
Definition: operator.cpp:83
bool gte(const Context &context, uint16_t &index)
Definition: operator.cpp:162
bool sub(const Context &context, uint16_t &index)
Definition: operator.cpp:273
Definition: operator.h:43
bool mul(const Context &context, uint16_t &index)
Definition: operator.cpp:319
void trace() const
Definition: _context.cpp:65
virtual bool equal(const _Context *c) const =0