Aseba  1.5.5
tree.h
1 /*
2  Aseba - an event-based framework for distributed robot control
3  Copyright (C) 2007--2016:
4  Stephane Magnenat <stephane at magnenat dot net>
5  (http://stephane.magnenat.net)
6  and other contributors, see authors.txt for details
7 
8  This program is free software: you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation, version 3 of the License.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __TREE_H
22 #define __TREE_H
23 
24 #include "compiler.h"
25 #include "../common/consts.h"
26 #include "../common/utils/FormatableString.h"
27 #include <vector>
28 #include <string>
29 #include <ostream>
30 #include <climits>
31 #include <cassert>
32 
33 #include <iostream>
34 
35 namespace Aseba
36 {
39 
42 
45 
47  struct Node
48  {
51  {
52  TYPE_UNIT = 0,
53  TYPE_BOOL,
54  TYPE_INT
55  };
56 
58  Node(const SourcePos& sourcePos) : sourcePos(sourcePos) { }
59  virtual ~Node();
61  virtual Node* shallowCopy() = 0;
63  virtual Node* deepCopy();
64 
66  virtual void checkVectorSize() const;
68  virtual Node* expandAbstractNodes(std::wostream* dump);
70  virtual Node* expandVectorialNodes(std::wostream* dump, Compiler* compiler=0, unsigned int index = 0);
72  virtual ReturnType typeCheck(Compiler* compiler);
74  virtual Node* optimize(std::wostream* dump) = 0;
76  virtual unsigned getStackDepth() const;
78  virtual void emit(PreLinkBytecode& bytecodes) const = 0;
79 
81  virtual std::wstring toWString() const = 0;
83  virtual std::wstring toNodeName() const = 0;
85  virtual void dump(std::wostream& dest, unsigned& indent) const;
86 
88  std::wstring typeName(const Node::ReturnType& type) const;
90  void expectType(const Node::ReturnType& expected, const Node::ReturnType& type) const;
91 
92  enum MemoryErrorCode
93  {
94  E_NOVAL = UINT_MAX
95  };
96  virtual unsigned getVectorAddr() const;
97  virtual unsigned getVectorSize() const;
98 
100  typedef std::vector<Node *> NodesVector;
101  NodesVector children;
103  };
104 
106  struct BlockNode : Node
107  {
109  BlockNode(const SourcePos& sourcePos) : Node(sourcePos) { }
110  virtual BlockNode* shallowCopy() { return new BlockNode(*this); }
111 
112  virtual Node* optimize(std::wostream* dump);
113  virtual void emit(PreLinkBytecode& bytecodes) const;
114  virtual std::wstring toWString() const { return L"Block"; }
115  virtual std::wstring toNodeName() const { return L"block"; }
116  };
117 
120  {
122  ProgramNode(const SourcePos& sourcePos) : BlockNode(sourcePos) { }
123  virtual ProgramNode* shallowCopy() { return new ProgramNode(*this); }
124 
125  virtual Node* expandVectorialNodes(std::wostream* dump, Compiler* compiler=0, unsigned int index = 0);
126  virtual void emit(PreLinkBytecode& bytecodes) const;
127  virtual std::wstring toWString() const { return L"ProgramBlock"; }
128  virtual std::wstring toNodeName() const { return L"program block"; }
129  };
130 
135  {
137  AssignmentNode(const SourcePos& sourcePos) : Node(sourcePos) { }
138  AssignmentNode(const SourcePos& sourcePos, Node* left, Node* right);
139  virtual AssignmentNode* shallowCopy() { return new AssignmentNode(*this); }
140 
141  virtual void checkVectorSize() const;
142  virtual Node* expandVectorialNodes(std::wostream* dump, Compiler* compiler=0, unsigned int index = 0);
143  virtual ReturnType typeCheck(Compiler* compiler);
144  virtual Node* optimize(std::wostream* dump);
145  virtual void emit(PreLinkBytecode& bytecodes) const;
146  virtual std::wstring toWString() const { return L"Assign"; }
147  virtual std::wstring toNodeName() const { return L"assignment"; }
148  };
149 
154  struct IfWhenNode : Node
155  {
157  unsigned endLine;
158 
160  IfWhenNode(const SourcePos& sourcePos) : Node(sourcePos) { }
161  virtual IfWhenNode* shallowCopy() { return new IfWhenNode(*this); }
162 
163  virtual void checkVectorSize() const;
164  virtual ReturnType typeCheck(Compiler* compiler);
165  virtual Node* optimize(std::wostream* dump);
166  virtual void emit(PreLinkBytecode& bytecodes) const;
167  virtual std::wstring toWString() const;
168  virtual std::wstring toNodeName() const { return L"if/when"; }
169  };
170 
177  {
180  unsigned endLine;
181 
183  FoldedIfWhenNode(const SourcePos& sourcePos) : Node(sourcePos) { }
184  virtual FoldedIfWhenNode* shallowCopy() { return new FoldedIfWhenNode(*this); }
185 
186  virtual void checkVectorSize() const;
187  virtual Node* optimize(std::wostream* dump);
188  virtual unsigned getStackDepth() const;
189  virtual void emit(PreLinkBytecode& bytecodes) const;
190  virtual std::wstring toWString() const;
191  virtual std::wstring toNodeName() const { return L"folded if/when"; }
192  };
193 
197  struct WhileNode : Node
198  {
200  WhileNode(const SourcePos& sourcePos) : Node(sourcePos) { }
201  virtual WhileNode* shallowCopy() { return new WhileNode(*this); }
202 
203  virtual void checkVectorSize() const;
204  virtual ReturnType typeCheck(Compiler* compiler);
205  virtual Node* optimize(std::wostream* dump);
206  virtual void emit(PreLinkBytecode& bytecodes) const;
207  virtual std::wstring toWString() const;
208  virtual std::wstring toNodeName() const { return L"while"; }
209  };
210 
216  {
218 
220  FoldedWhileNode(const SourcePos& sourcePos) : Node(sourcePos) { }
221  virtual FoldedWhileNode* shallowCopy() { return new FoldedWhileNode(*this); }
222 
223  virtual void checkVectorSize() const;
224  virtual Node* optimize(std::wostream* dump);
225  virtual unsigned getStackDepth() const;
226  virtual void emit(PreLinkBytecode& bytecodes) const;
227  virtual std::wstring toWString() const;
228  virtual std::wstring toNodeName() const { return L"folded while"; }
229  };
230 
234  {
235  unsigned eventId;
236 
237  EventDeclNode(const SourcePos& sourcePos, unsigned eventId = 0);
238  virtual EventDeclNode* shallowCopy() { return new EventDeclNode(*this); }
239 
240  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_UNIT; }
241  virtual Node* optimize(std::wostream* dump);
242  virtual void emit(PreLinkBytecode& bytecodes) const;
243  virtual std::wstring toWString() const;
244  virtual std::wstring toNodeName() const { return L"event declaration"; }
245  };
246 
249  struct EmitNode : Node
250  {
251  unsigned eventId;
252  unsigned arrayAddr;
253  unsigned arraySize;
254 
256  EmitNode(const SourcePos& sourcePos) : Node(sourcePos), eventId(0), arrayAddr(0), arraySize(0) { }
257  virtual EmitNode* shallowCopy() { return new EmitNode(*this); }
258 
259  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_UNIT; }
260  virtual Node* optimize(std::wostream* dump);
261  virtual void emit(PreLinkBytecode& bytecodes) const;
262  virtual std::wstring toWString() const;
263  virtual std::wstring toNodeName() const { return L"emit"; }
264  };
265 
268  struct SubDeclNode : Node
269  {
270  unsigned subroutineId;
271 
272  SubDeclNode(const SourcePos& sourcePos, unsigned subroutineId);
273  virtual SubDeclNode* shallowCopy() { return new SubDeclNode(*this); }
274 
275  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_UNIT; }
276  virtual Node* optimize(std::wostream* dump);
277  virtual void emit(PreLinkBytecode& bytecodes) const;
278  virtual std::wstring toWString() const;
279  virtual std::wstring toNodeName() const { return L"subroutine declaration"; }
280  };
281 
284  struct CallSubNode : Node
285  {
286  std::wstring subroutineName;
287  unsigned subroutineId;
288 
289  CallSubNode(const SourcePos& sourcePos, const std::wstring& subroutineName);
290  virtual CallSubNode* shallowCopy() { return new CallSubNode(*this); }
291 
292  virtual ReturnType typeCheck(Compiler* compiler);
293  virtual Node* optimize(std::wostream* dump);
294  virtual void emit(PreLinkBytecode& bytecodes) const;
295  virtual std::wstring toWString() const;
296  virtual std::wstring toNodeName() const { return L"subroutine call"; }
297  };
298 
303  {
305 
306  BinaryArithmeticNode(const SourcePos& sourcePos) : Node(sourcePos) { }
308  virtual BinaryArithmeticNode* shallowCopy() { return new BinaryArithmeticNode(*this); }
309 
310  void deMorganNotRemoval();
311 
312  virtual ReturnType typeCheck(Compiler* compiler);
313  virtual Node* optimize(std::wostream* dump);
314  virtual unsigned getStackDepth() const;
315  virtual void emit(PreLinkBytecode& bytecodes) const;
316  virtual std::wstring toWString() const;
317  virtual std::wstring toNodeName() const { return L"binary function"; }
318 
319  static BinaryArithmeticNode *fromComparison(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
320  static BinaryArithmeticNode *fromShiftExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
321  static BinaryArithmeticNode *fromAddExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
322  static BinaryArithmeticNode *fromMultExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
323  static BinaryArithmeticNode *fromBinaryExpression(const SourcePos& sourcePos, Compiler::Token::Type op, Node *left, Node *right);
324  };
325 
329  {
331 
333  UnaryArithmeticNode(const SourcePos& sourcePos) : Node(sourcePos) { }
335  virtual UnaryArithmeticNode* shallowCopy() { return new UnaryArithmeticNode(*this); }
336 
337  virtual ReturnType typeCheck(Compiler* compiler);
338  virtual Node* optimize(std::wostream* dump);
339  virtual void emit(PreLinkBytecode& bytecodes) const;
340  virtual std::wstring toWString() const;
341  virtual std::wstring toNodeName() const { return L"unary function"; }
342  };
343 
349  {
350  int value;
351 
353  ImmediateNode(const SourcePos& sourcePos, int value) : Node(sourcePos), value(value) { }
354  virtual ImmediateNode* shallowCopy() { return new ImmediateNode(*this); }
355 
356  virtual Node* expandVectorialNodes(std::wostream* dump, Compiler* compiler=0, unsigned int index = 0);
357  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_INT; }
358  virtual Node* optimize(std::wostream* dump);
359  virtual unsigned getStackDepth() const;
360  virtual void emit(PreLinkBytecode& bytecodes) const;
361  virtual std::wstring toWString() const;
362  virtual std::wstring toNodeName() const { return L"constant"; }
363 
364  virtual unsigned getVectorSize() const { return 1; }
365  };
366 
369  struct StoreNode : Node
370  {
371  unsigned varAddr;
372 
374  StoreNode(const SourcePos& sourcePos, unsigned varAddr) : Node(sourcePos), varAddr(varAddr) { }
375  virtual StoreNode* shallowCopy() { return new StoreNode(*this); }
376 
377  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_UNIT; }
378  virtual Node* optimize(std::wostream* dump);
379  virtual void emit(PreLinkBytecode& bytecodes) const;
380  virtual std::wstring toWString() const;
381  virtual std::wstring toNodeName() const { return L"variable access (write)"; }
382 
383  virtual unsigned getVectorAddr() const { return varAddr; }
384  virtual unsigned getVectorSize() const { return 1; }
385  };
386 
389  struct LoadNode : Node
390  {
391  unsigned varAddr;
392 
394  LoadNode(const SourcePos& sourcePos, unsigned varAddr) : Node(sourcePos), varAddr(varAddr) { }
395  virtual LoadNode* shallowCopy() { return new LoadNode(*this); }
396 
397  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_INT; }
398  virtual Node* optimize(std::wostream* dump);
399  virtual unsigned getStackDepth() const;
400  virtual void emit(PreLinkBytecode& bytecodes) const;
401  virtual std::wstring toWString() const;
402  virtual std::wstring toNodeName() const { return L"variable access (read)"; }
403 
404  virtual unsigned getVectorAddr() const { return varAddr; }
405  virtual unsigned getVectorSize() const { return 1; }
406  };
407 
411  {
412  unsigned arrayAddr;
413  unsigned arraySize;
414  std::wstring arrayName;
415 
416  ArrayWriteNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::wstring &arrayName);
417  virtual ArrayWriteNode* shallowCopy() { return new ArrayWriteNode(*this); }
418 
419  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_UNIT; }
420  virtual Node* optimize(std::wostream* dump);
421  virtual void emit(PreLinkBytecode& bytecodes) const;
422  virtual std::wstring toWString() const;
423  virtual std::wstring toNodeName() const { return L"array access (write)"; }
424 
425  virtual unsigned getVectorAddr() const { return arrayAddr; }
426  virtual unsigned getVectorSize() const { return 1; }
427  };
428 
432  {
433  unsigned arrayAddr;
434  unsigned arraySize;
435  std::wstring arrayName;
436 
437  ArrayReadNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::wstring &arrayName);
438  virtual ArrayReadNode* shallowCopy() { return new ArrayReadNode(*this); }
439 
440  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_INT; }
441  virtual Node* optimize(std::wostream* dump);
442  virtual void emit(PreLinkBytecode& bytecodes) const;
443  virtual std::wstring toWString() const;
444  virtual std::wstring toNodeName() const { return L"array access (read)"; }
445 
446  virtual unsigned getVectorAddr() const { return arrayAddr; }
447  virtual unsigned getVectorSize() const { return 1; }
448  };
449 
453  {
454  unsigned tempAddr;
455  unsigned arrayAddr;
456  unsigned arraySize;
457  std::wstring arrayName;
458 
459  LoadNativeArgNode(MemoryVectorNode* memoryNode, unsigned tempAddr);
460  virtual LoadNativeArgNode* shallowCopy() { return new LoadNativeArgNode(*this); }
461 
462  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_INT; }
463  virtual Node* optimize(std::wostream* dump);
464  virtual unsigned getStackDepth() const;
465  virtual void emit(PreLinkBytecode& bytecodes) const;
466  virtual std::wstring toWString() const;
467  virtual std::wstring toNodeName() const { return L"load native arg"; }
468  };
469 
472  struct CallNode : Node
473  {
474  unsigned funcId;
475  std::vector<unsigned> templateArgs;
476 
477  CallNode(const SourcePos& sourcePos, unsigned funcId);
478  virtual CallNode* shallowCopy() { return new CallNode(*this); }
479 
480  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_UNIT; }
481  virtual Node* optimize(std::wostream* dump);
482  virtual unsigned getStackDepth() const;
483  virtual void emit(PreLinkBytecode& bytecodes) const;
484  virtual std::wstring toWString() const;
485  virtual std::wstring toNodeName() const { return L"native function call"; }
486  };
487 
490  struct ReturnNode : Node
491  {
492  ReturnNode(const SourcePos& sourcePos) : Node(sourcePos) {}
493  virtual ReturnNode* shallowCopy() { return new ReturnNode(*this); }
494 
495  virtual ReturnType typeCheck(Compiler* compiler) { return TYPE_UNIT; }
496  virtual Node* optimize(std::wostream* dump) { return this; }
497  virtual unsigned getStackDepth() const { return 0; }
498  virtual void emit(PreLinkBytecode& bytecodes) const;
499  virtual std::wstring toWString() const { return L"Return"; }
500  virtual std::wstring toNodeName() const { return L"return"; }
501  };
502 
503  /*** Nodes for abstract operations (e.g. vectors) ***/
504 
509  {
511  AbstractTreeNode(const SourcePos& sourcePos) : Node(sourcePos) {}
512 
513  virtual Node* expandAbstractNodes(std::wostream* dump) = 0;
514 
515  // Following operations should not be performed on abstraction nodes
516  virtual ReturnType typeCheck(Compiler* compiler) { abort(); }
517  virtual Node* optimize(std::wostream* dump) { abort(); }
518  virtual unsigned getStackDepth() const { abort(); }
519  virtual void emit(PreLinkBytecode& bytecodes) const { abort(); }
520  };
521 
525  {
528  TupleVectorNode(const SourcePos& sourcePos, int value) : AbstractTreeNode(sourcePos) { children.push_back(new ImmediateNode(sourcePos, value)); }
529  virtual TupleVectorNode* shallowCopy() { return new TupleVectorNode(*this); }
530 
531  virtual Node* expandAbstractNodes(std::wostream* dump);
532  virtual Node* expandVectorialNodes(std::wostream* dump, Compiler* compiler=0, unsigned int index = 0);
533  virtual std::wstring toWString() const;
534  virtual std::wstring toNodeName() const { return L"array of constants"; }
535  virtual void dump(std::wostream& dest, unsigned& indent) const;
536 
537  virtual unsigned getVectorSize() const;
538 
539  virtual bool isImmediateVector() const;
540  virtual int getImmediateValue(unsigned index) const;
541  virtual void addImmediateValue(int value) { children.push_back(new ImmediateNode(sourcePos, value)); }
542  };
543 
553  {
554  unsigned arrayAddr;
555  unsigned arraySize;
556  std::wstring arrayName;
557  bool write;
558 
559  MemoryVectorNode(const SourcePos& sourcePos, unsigned arrayAddr, unsigned arraySize, const std::wstring &arrayName);
560  virtual MemoryVectorNode* shallowCopy() { return new MemoryVectorNode(*this); }
561 
562  virtual Node* expandAbstractNodes(std::wostream* dump);
563  virtual Node* expandVectorialNodes(std::wostream* dump, Compiler* compiler=0, unsigned int index = 0);
564  virtual std::wstring toWString() const;
565  virtual std::wstring toNodeName() const { return L"vector access"; }
566 
567  virtual unsigned getVectorAddr() const;
568  virtual unsigned getVectorSize() const;
569  bool isAddressStatic() const;
570 
571  virtual void setWrite(bool write) { this->write = write; }
572  };
573 
579  {
581 
585 
586  virtual void checkVectorSize() const;
587  virtual Node* expandAbstractNodes(std::wostream* dump);
588  virtual Node* expandVectorialNodes(std::wostream* dump, Compiler* compiler=0, unsigned int index = 0) { abort(); } // should not happen
589  virtual std::wstring toWString() const;
590  virtual std::wstring toNodeName() const { return L"arithmetic assignment"; }
591 
592  static ArithmeticAssignmentNode* fromArithmeticAssignmentToken(const SourcePos& sourcePos, Compiler::Token::Type token, Node *left, Node *right);
593 
594  protected:
595  const static AsebaBinaryOperator operatorMap[];
596  static AsebaBinaryOperator getBinaryOperator(Compiler::Token::Type token);
597  };
598 
603  {
605 
607  UnaryArithmeticAssignmentNode(const SourcePos& sourcePos, Compiler::Token::Type token, Node *memory);
609 
610  virtual Node* expandAbstractNodes(std::wostream* dump);
611  virtual Node* expandVectorialNodes(std::wostream* dump, Compiler* compiler=0, unsigned int index = 0) { abort(); } // should not happen
612  virtual std::wstring toWString() const;
613  virtual std::wstring toNodeName() const { return L"unary arithmetic assignment"; }
614  };
615 
618 } // namespace Aseba
619 
620 #endif
virtual EmitNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:257
virtual CallNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:478
unsigned eventId
id of event to emit
Definition: tree.h:251
Node for unary arithmetic children[0] is the expression to negate.
Definition: tree.h:328
void expectType(const Node::ReturnType &expected, const Node::ReturnType &type) const
Check for a specific type, throw an exception otherwise.
Definition: tree-typecheck.cpp:41
unsigned arraySize
size of the array, might be used to assert compile-time access checks
Definition: tree.h:555
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:259
virtual unsigned getVectorAddr() const
return the address of the left-most operand, or E_NOVAL if none
Definition: tree.h:425
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:296
Node for L"if" and L"when" with operator folded inside.
Definition: tree.h:176
An abstract node of syntax tree.
Definition: tree.h:47
Node for writing to an array.
Definition: tree.h:410
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:317
int value
value to push on stack
Definition: tree.h:350
AsebaBinaryOperator op
operator
Definition: tree.h:580
Node for reading from an array.
Definition: tree.h:431
std::wstring binaryOperatorToString(AsebaBinaryOperator op)
Return the string corresponding to the binary operator.
Definition: tree-dump.cpp:30
std::wstring arrayName
name of the array (for debug)
Definition: tree.h:457
unsigned endLine
line of end keyword
Definition: tree.h:180
virtual UnaryArithmeticNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:335
virtual Node * expandVectorialNodes(std::wostream *dump, Compiler *compiler=0, unsigned int index=0)
Third pass to expand vectorial operations into mutliple scalar ones.
Definition: tree.h:588
AsebaBinaryOperator op
operator
Definition: tree.h:304
virtual MemoryVectorNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:560
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:168
AsebaUnaryOperator
List of unary operators.
Definition: consts.h:105
virtual EventDeclNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:238
unsigned subroutineId
the associated subroutine
Definition: tree.h:270
unsigned varAddr
address of variable to load from
Definition: tree.h:391
virtual Node * optimize(std::wostream *dump)=0
Optimize this node, return the optimized node.
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:147
AsebaBinaryOperator op
operator
Definition: tree.h:217
std::wstring unaryOperatorToString(AsebaUnaryOperator op)
Return the string corresponding to the unary operator.
Definition: tree-dump.cpp:60
virtual Node * expandVectorialNodes(std::wostream *dump, Compiler *compiler=0, unsigned int index=0)
Third pass to expand vectorial operations into mutliple scalar ones.
Definition: tree-expand.cpp:197
Node for L"while" with operator folded inside.
Definition: tree.h:215
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree-typecheck.cpp:47
virtual unsigned getVectorSize() const
return the children&#39;s size, check for equal size, or E_NOVAL if no child
Definition: tree.h:405
unsigned arrayAddr
address of the first element of the array
Definition: tree.h:433
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:480
virtual unsigned getVectorSize() const
return the children&#39;s size, check for equal size, or E_NOVAL if no child
Definition: tree.h:426
virtual void dump(std::wostream &dest, unsigned &indent) const
Dump this node and the rest of the tree.
Definition: tree-dump.cpp:73
virtual void emit(PreLinkBytecode &bytecodes) const =0
Generate bytecode.
virtual void checkVectorSize() const
Check the consistency in vectors&#39; size.
Definition: tree-expand.cpp:338
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:495
virtual ~Node()
Destructor, delete all children.
Definition: tree-build.cpp:45
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:381
Node for L"while".
Definition: tree.h:197
virtual std::wstring toWString() const =0
Return a string representation of this node.
FoldedWhileNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:220
IfWhenNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:160
EmitNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:256
unsigned arrayAddr
address of the first element of the array to send
Definition: tree.h:252
std::wstring arrayName
name of the array (for debug)
Definition: tree.h:414
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:516
unsigned arrayAddr
address of the first element of the array
Definition: tree.h:554
virtual std::wstring toWString() const
Return a string representation of this node.
Definition: tree.h:127
UnaryArithmeticNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:333
virtual SubDeclNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:273
virtual unsigned getStackDepth() const
Return the stack depth requirement for this node and its children.
Definition: tree.h:518
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:362
Definition: analysis.cpp:26
virtual Node * optimize(std::wostream *dump)
Optimize this node, return the optimized node.
Definition: tree.h:517
virtual ArrayWriteNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:417
virtual unsigned getStackDepth() const
Return the stack depth requirement for this node and its children.
Definition: tree-emit.cpp:94
WhileNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:200
Node for L"callsub" no children.
Definition: tree.h:284
Node for calling a native function may have children for pushing constants somewhere.
Definition: tree.h:472
Node for accessing a memory as a vector, in read or write operations If write == true, will expand to StoreNode or ArrayWriteNode If write == false, will expand to LoadNode or ArrayReadNode children[0] is an optional index If children[0] is a TupleVectorNode of one elements (int), it will be foo[x] If children[0] is a TupleVectorNode of two elements (int), it will be foo[x:y] If children[0] is another type of node, it will be foo[whatever] If children[0] doesn&#39;t exist, access to the full array is considered.
Definition: tree.h:552
virtual ArrayReadNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:438
virtual unsigned getVectorAddr() const
return the address of the left-most operand, or E_NOVAL if none
Definition: tree.h:383
virtual ArithmeticAssignmentNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:584
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:419
virtual Node * optimize(std::wostream *dump)
Optimize this node, return the optimized node.
Definition: tree.h:496
virtual std::wstring toWString() const
Return a string representation of this node.
Definition: tree.h:146
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:467
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:191
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:485
virtual UnaryArithmeticAssignmentNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:608
std::wstring typeName(const Node::ReturnType &type) const
Return the name of a type.
Definition: tree-typecheck.cpp:30
Node for L"program", i.e. a block node with some special behaviour later on.
Definition: tree.h:119
virtual IfWhenNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:161
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:263
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:128
std::wstring arrayName
name of the array (for debug)
Definition: tree.h:435
Aseba Event Scripting Language compiler.
Definition: compiler.h:258
unsigned eventId
the event id associated with this context
Definition: tree.h:235
virtual ImmediateNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:354
Node for storing a variable from stack.
Definition: tree.h:369
virtual void emit(PreLinkBytecode &bytecodes) const
Generate bytecode.
Definition: tree.h:519
unsigned arraySize
size of the array to send.
Definition: tree.h:253
std::vector< Node * > NodesVector
Vector for children of a node.
Definition: tree.h:100
Node for returning from an event or subroutine has no children, just a jump of 0 offset that will be ...
Definition: tree.h:490
virtual LoadNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:395
virtual Node * expandVectorialNodes(std::wostream *dump, Compiler *compiler=0, unsigned int index=0)
Third pass to expand vectorial operations into mutliple scalar ones.
Definition: tree.h:611
bool edgeSensitive
if true, true block is triggered only if previous comparison was false ("when" block).
Definition: tree.h:179
SourcePos sourcePos
position is source
Definition: tree.h:102
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:377
Node for L"onevent" no children.
Definition: tree.h:233
virtual unsigned getVectorAddr() const
return the address of the left-most operand, or E_NOVAL if none
Definition: tree.h:446
AsebaUnaryOperator op
operator
Definition: tree.h:330
virtual unsigned getVectorSize() const
return the children&#39;s size, check for equal size, or E_NOVAL if no child
Definition: tree.h:447
NodesVector children
children of this node
Definition: tree.h:101
unsigned funcId
identifier of the function to be called
Definition: tree.h:474
unsigned arrayAddr
address of the first element of the array
Definition: tree.h:455
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:228
Node for assignation.
Definition: tree.h:134
AbstractTreeNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:511
Node for binary arithmetic.
Definition: tree.h:302
bool write
expand to a node for storing or loading data?
Definition: tree.h:557
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:357
Node for L"emit".
Definition: tree.h:249
Node for pushing immediate value on stack.
Definition: tree.h:348
virtual std::wstring toWString() const
Return a string representation of this node.
Definition: tree.h:114
virtual ReturnNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:493
std::wstring arrayName
name of the array (for debug)
Definition: tree.h:556
Node(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:58
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:341
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:397
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:115
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:500
virtual StoreNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:375
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:590
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:423
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:534
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:444
Node for L"sub" no children.
Definition: tree.h:268
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:208
AssignmentNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:137
bool edgeSensitive
if true, true block is triggered only if previous comparison was false ("when" block).
Definition: tree.h:156
AsebaBinaryOperator op
operator
Definition: tree.h:178
virtual AssignmentNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:139
AsebaBinaryOperator
List of binary operators.
Definition: consts.h:75
StoreNode(const SourcePos &sourcePos, unsigned varAddr)
Constructor.
Definition: tree.h:374
virtual std::wstring toNodeName() const =0
Return a string representation of the name of this node.
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:440
unsigned arraySize
size of the array, might be used to assert compile-time access checks
Definition: tree.h:434
Node for assembling values into an array children[x] is the x-th Node to be assembled.
Definition: tree.h:524
unsigned arraySize
size of the array, might be used to assert compile-time access checks
Definition: tree.h:456
Node for L"block", i.e. a vector of statements.
Definition: tree.h:106
Node for L"if" and L"when".
Definition: tree.h:154
std::wstring subroutineName
the subroutine to call
Definition: tree.h:286
ProgramNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:122
std::vector< unsigned > templateArgs
sizes of templated arguments
Definition: tree.h:475
virtual BinaryArithmeticNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:308
virtual FoldedIfWhenNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:184
Node for operations like "vector (op)= something" Will expand to "vector = vector (op) something" chi...
Definition: tree.h:578
virtual CallSubNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:290
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:613
virtual unsigned getVectorSize() const
return the children&#39;s size, check for equal size, or E_NOVAL if no child
Definition: tree-expand.cpp:448
virtual ProgramNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:123
virtual unsigned getVectorAddr() const
return the address of the left-most operand, or E_NOVAL if none
Definition: tree-expand.cpp:438
virtual Node * shallowCopy()=0
Return a shallow copy of the object (children point to the same objects)
Node for loading a variable on stack.
Definition: tree.h:389
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:462
virtual FoldedWhileNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:221
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:279
LoadNode(const SourcePos &sourcePos, unsigned varAddr)
Constructor.
Definition: tree.h:394
virtual Node * expandAbstractNodes(std::wostream *dump)
Second pass to expand "abstract" nodes into more concrete ones.
Definition: tree-expand.cpp:59
virtual unsigned getVectorSize() const
return the children&#39;s size, check for equal size, or E_NOVAL if no child
Definition: tree.h:384
unsigned varAddr
address of variable to store to
Definition: tree.h:371
BlockNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:109
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:402
unsigned arraySize
size of the array, might be used to assert compile-time access checks
Definition: tree.h:413
Node for loading the address of the argument of a native function that is not known at compile time...
Definition: tree.h:452
TupleVectorNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:527
virtual Node * deepCopy()
Return a deep copy of the object (children are also copied)
Definition: tree-build.cpp:55
virtual unsigned getVectorAddr() const
return the address of the left-most operand, or E_NOVAL if none
Definition: tree.h:404
virtual unsigned getVectorSize() const
return the children&#39;s size, check for equal size, or E_NOVAL if no child
Definition: tree.h:364
Virtual class for abstraction nodes abort() if you try to typeCheck(), optimize() or emit() to force ...
Definition: tree.h:508
ReturnType
A type a node can return.
Definition: tree.h:50
Bytecode use for compilation previous to linking.
Definition: compiler.h:483
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:275
Node for operations like "vector(op)", may be ++ or – Will expand to "vector (op)= [1...
Definition: tree.h:602
FoldedIfWhenNode(const SourcePos &sourcePos)
Constructor.
Definition: tree.h:183
virtual TupleVectorNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:529
virtual unsigned getStackDepth() const
Return the stack depth requirement for this node and its children.
Definition: tree.h:497
virtual WhileNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:201
unsigned arrayAddr
address of the first element of the array
Definition: tree.h:412
virtual std::wstring toWString() const
Return a string representation of this node.
Definition: tree.h:499
virtual BlockNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:110
Position in a source file or string. First is line, second is column.
Definition: compiler.h:156
virtual LoadNativeArgNode * shallowCopy()
Return a shallow copy of the object (children point to the same objects)
Definition: tree.h:460
virtual ReturnType typeCheck(Compiler *compiler)
Typecheck this node, throw an exception if there is any type violation.
Definition: tree.h:240
unsigned endLine
line of end keyword
Definition: tree.h:157
AsebaBinaryOperator arithmeticOp
operator
Definition: tree.h:604
unsigned tempAddr
address of temporary (end of memory) for stack value duplication
Definition: tree.h:454
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:244
virtual std::wstring toNodeName() const
Return a string representation of the name of this node.
Definition: tree.h:565
ImmediateNode(const SourcePos &sourcePos, int value)
Constructor.
Definition: tree.h:353