replicode
atom.h
Go to the documentation of this file.
1 // atom.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 r_code_atom_h
32 #define r_code_atom_h
33 
34 #include "CoreLibrary/dll.h"
35 
36 #include <cstdint>
37 #include <cstddef>
38 
39 namespace r_code {
40 
41 // Opcodes on 12 bits.
42 // Indices on 12 bits.
43 // Element count on 8 bits.
44 // To define bigger constructs (e.g. large matrices), define hooks to RAM (and derive classes from Object).
46 private: // trace utilities.
47  static uint8_t Members_to_go;
48  static uint8_t Timestamp_data;
49  static uint8_t String_data;
50  static uint8_t Char_count;
51  void write_indents() const;
52 public:
53  typedef enum {
54  NIL = 0x80,
55  BOOLEAN_ = 0x81,
56  WILDCARD = 0x82,
57  T_WILDCARD = 0x83,
58  I_PTR = 0x84, // internal pointer.
59  R_PTR = 0x85, // reference pointer.
60  VL_PTR = 0x86, // value pointer.
61  IPGM_PTR = 0x87, // r_exec internal: index of data of a tpl arg held by an ipgm.
62  IN_OBJ_PTR = 0x88, // r_exec internal: index of data held by an input object.
63  VALUE_PTR = 0x89, // r_exec internal: index of data held by the overlay's value array.
64  PROD_PTR = 0x8A, // r_exec internal: index of data held by the overlay's production array.
65  OUT_OBJ_PTR = 0x8B, // r_exec internal: index of data held by a newly produced object.
66  D_IN_OBJ_PTR = 0x8C, // r_exec internal: index of data held by an object referenced by an input object.
67  ASSIGN_PTR = 0x8D, // r_exec internal: index of a hlp variable and to be assigned index of an expression that produces the value.
68  THIS = 0x90, // this pointer.
69  VIEW = 0x91,
70  MKS = 0x92,
71  VWS = 0x93,
72  NODE = 0xA0,
73  DEVICE = 0xA1,
74  DEVICE_FUNCTION = 0xA2,
75  C_PTR = 0xC0, // chain pointer.
76  SET = 0xC1,
77  S_SET = 0xC2, // structured set.
78  OBJECT = 0xC3,
79  MARKER = 0xC4,
80  OPERATOR = 0xC5,
81  STRING = 0xC6,
82  TIMESTAMP = 0xC7,
83  GROUP = 0xC8,
84  INSTANTIATED_PROGRAM = 0xC9,
85  INSTANTIATED_CPP_PROGRAM = 0xCA,
86  INSTANTIATED_INPUT_LESS_PROGRAM = 0xCB,
87  INSTANTIATED_ANTI_PROGRAM = 0xCC,
88  COMPOSITE_STATE = 0xCD,
89  MODEL = 0xCE,
90  NULL_PROGRAM = 0xCF
91  } Type;
92 
93 // encoders
94  static Atom Float(float f); // IEEE 754 32 bits encoding; shifted by 1 to the right (loss of precison).
95  static Atom PlusInfinity();
96  static Atom MinusInfinity();
97  static Atom UndefinedFloat();
98  static Atom Nil();
99  static Atom Boolean(bool value);
100  static Atom UndefinedBoolean();
101  static Atom Wildcard(uint16_t opcode = 0x00);
102  static Atom TailWildcard();
103  static Atom IPointer(uint16_t index);
104  static Atom RPointer(uint16_t index);
105  static Atom VLPointer(uint16_t index, uint16_t cast_opcode = 0x0FFF);
106  static Atom IPGMPointer(uint16_t index);
107  static Atom InObjPointer(uint8_t inputIndex, uint16_t index); // inputIndex: index of the input view; index: index of data in the object's code.
108  static Atom DInObjPointer(uint8_t relativeIndex, uint16_t index); // relativeIndex: index of an in-obj-ptr in the program's (patched) code; index: index of data in the referenced object code.
109  static Atom OutObjPointer(uint16_t index);
110  static Atom ValuePointer(uint16_t index);
111  static Atom ProductionPointer(uint16_t index);
112  static Atom AssignmentPointer(uint8_t variable_index, uint16_t index);
113  static Atom This();
114  static Atom View();
115  static Atom Mks();
116  static Atom Vws();
117  static Atom Node(uint8_t nodeID);
118  static Atom UndefinedNode();
119  static Atom Device(uint8_t nodeID, uint8_t classID, uint8_t devID);
120  static Atom UndefinedDevice();
121  static Atom DeviceFunction(uint16_t opcode);
122  static Atom UndefinedDeviceFunction();
123  static Atom CPointer(uint8_t elementCount);
124  static Atom SSet(uint16_t opcode, uint8_t elementCount);
125  static Atom Set(uint8_t elementCount);
126  static Atom Object(uint16_t opcode, uint8_t arity);
127  static Atom Marker(uint16_t opcode, uint8_t arity);
128  static Atom Operator(uint16_t opcode, uint8_t arity);
129  static Atom String(uint8_t characterCount);
130  static Atom UndefinedString();
131  static Atom Timestamp();
132  static Atom UndefinedTimestamp();
133  static Atom InstantiatedProgram(uint16_t opcode, uint8_t arity);
134  static Atom Group(uint16_t opcode, uint8_t arity);
135  static Atom InstantiatedCPPProgram(uint16_t opcode, uint8_t arity);
136  static Atom InstantiatedAntiProgram(uint16_t opcode, uint8_t arity);
137  static Atom InstantiatedInputLessProgram(uint16_t opcode, uint8_t arity);
138  static Atom CompositeState(uint16_t opcode, uint8_t arity);
139  static Atom Model(uint16_t opcode, uint8_t arity);
140 
141  static Atom NullProgram(bool take_past_inputs);
142 
143  Atom(uint32_t a = UINT32_MAX);
144  ~Atom();
145 
146  Atom &operator =(const Atom& a);
147  bool operator ==(const Atom& a) const;
148  bool operator !=(const Atom& a) const;
149  bool operator !() const;
150  operator size_t () const;
151 
152  uint32_t atom;
153 
154 // decoders
155  bool isUndefined() const;
156  uint8_t getDescriptor() const;
157  bool isStructural() const;
158  bool isFloat() const;
159  bool readsAsNil() const; // returns true for all undefined values.
160  float asFloat() const;
161  bool asBoolean() const;
162  uint16_t asIndex() const; // applicable to internal, view, reference,
163 // and value pointers.
164  uint8_t asInputIndex() const; // applicable to IN_OBJ_PTR.
165  uint8_t asRelativeIndex() const; // applicable to D_IN_OBJ_PTR.
166  uint16_t asOpcode() const;
167  uint16_t asCastOpcode() const; // applicable to VL_PTR.
168  uint8_t getAtomCount() const; // arity of operators and
169 // objects/markers/structured sets,
170 // number of atoms in pointers chains,
171 // number of blocks of characters in
172 // strings.
173  uint8_t getNodeID() const; // applicable to nodes and devices.
174  uint8_t getClassID() const; // applicable to devices.
175  uint8_t getDeviceID() const; // applicable to devices.
176  uint8_t asAssignmentIndex() const;
177 
178  bool takesPastInputs() const; // applicable to NULL_PROGRAM.
179 
180  void trace() const;
181  static void Trace(Atom *base, uint16_t count);
182 };
183 }
184 
185 #endif
#define dll_export
Definition: dll.h:44
Definition: structure_member.h:51
Definition: atom.cpp:36
static uint8_t Members_to_go
Definition: atom.h:47
static uint8_t Char_count
Definition: atom.h:50
Definition: atom.h:45
Definition: object.h:100
static uint8_t String_data
Definition: atom.h:49
Definition: structure_member.h:52
uint32_t atom
Definition: atom.h:152
static uint8_t Timestamp_data
Definition: atom.h:48
Definition: structure_member.h:54