21 #ifndef __ASEBA_COMPILER_H 22 #define __ASEBA_COMPILER_H 32 #include "errors_code.h" 33 #include "../common/types.h" 34 #include "../common/utils/FormatableString.h" 47 struct BinaryArithmeticNode;
48 struct AssignmentNode;
49 struct TupleVectorNode;
53 typedef std::map<std::wstring, std::pair<unsigned, unsigned> >
VariablesMap;
90 NativeFunction(
const std::wstring &
name,
const std::wstring &description) :
name(name), description(description) {}
109 TargetDescription() : protocolVersion(0), bytecodeSize(0), variablesSize(0), stackSize(0) { }
119 BytecodeElement(
unsigned short bytecode) : bytecode(bytecode), line(0) { }
120 BytecodeElement(
unsigned short bytecode,
unsigned short line) : bytecode(bytecode), line(line) { }
121 operator unsigned short ()
const {
return bytecode; }
122 unsigned getWordSize()
const;
124 unsigned short bytecode;
140 std::deque<BytecodeElement>::push_back(be);
144 void changeStopToRetSub();
145 unsigned short getTypeOfLast()
const;
149 EventAddressesToIdsMap getEventAddressesToIds()
const;
163 SourcePos(
unsigned character,
unsigned row,
unsigned column) : character(character - 1), row(row), column(column - 1) { valid =
true; }
164 SourcePos() : character(0), row(0), column(0), valid(
false) { }
165 std::wstring toWString()
const;
175 typedef const std::wstring (*ErrorCallback)(ErrorCode error);
177 static const std::wstring defaultCallback(ErrorCode error);
185 Error(
const SourcePos& pos,
const std::wstring& message) : pos(pos), message(message) { }
188 Error() { message = L
"not defined"; }
191 std::wstring toWString()
const;
198 TranslatableError &arg(
int value,
int fieldWidth = 0,
int base = 10,
wchar_t fillChar =
' ');
199 TranslatableError &arg(
long int value,
int fieldWidth = 0,
int base = 10,
wchar_t fillChar =
' ');
200 TranslatableError &arg(
unsigned value,
int fieldWidth = 0,
int base = 10,
wchar_t fillChar =
' ');
201 TranslatableError &arg(
float value,
int fieldWidth = 0,
int precision = 6,
wchar_t fillChar =
' ');
208 WFormatableString message;
233 bool contains(
const std::wstring& s,
size_t* position = 0)
const;
251 void clear() { events.clear(); constants.clear(); }
266 TOKEN_END_OF_STREAM = 0,
269 TOKEN_STR_hidden_emit,
288 TOKEN_STRING_LITERAL,
304 TOKEN_OP_BIGGER_EQUAL,
306 TOKEN_OP_SMALLER_EQUAL,
314 TOKEN_OP_SHIFT_RIGHT,
325 TOKEN_OP_SHIFT_LEFT_EQUAL,
326 TOKEN_OP_SHIFT_RIGHT_EQUAL,
327 TOKEN_OP_BIT_OR_EQUAL,
328 TOKEN_OP_BIT_XOR_EQUAL,
329 TOKEN_OP_BIT_AND_EQUAL,
339 Token() : type(TOKEN_END_OF_STREAM), iValue(0) {}
341 const std::wstring typeName()
const;
342 std::wstring toWString()
const;
343 operator Type ()
const {
return type; }
353 SubroutineDescriptor(
const std::wstring& name,
unsigned address,
unsigned line) :
name(name), address(address), line(line) {}
372 const TargetDescription *getTargetDescription()
const {
return targetDescription;}
374 const SubroutineTable *getSubroutineTable()
const {
return &subroutineTable; }
376 bool compile(std::wistream& source,
BytecodeVector& bytecode,
unsigned& allocatedVariablesCount,
Error &errorDescription, std::wostream* dump = 0);
378 static std::wstring translate(ErrorCode error) {
return TranslatableError::translateCB(error); }
379 static bool isKeyword(
const std::wstring& word);
382 void internalCompilerError()
const;
383 void expect(
const Token::Type& type)
const;
384 unsigned expectUInt12Literal()
const;
385 unsigned expectUInt16Literal()
const;
386 unsigned expectPositiveInt16Literal()
const;
387 int expectAbsoluteInt16Literal(
bool negative)
const;
388 unsigned expectPositiveConstant()
const;
389 int expectConstant()
const;
390 unsigned expectPositiveInt16LiteralOrConstant()
const;
391 int expectInt16Literal();
392 int expectInt16LiteralOrConstant();
393 unsigned expectGlobalEventId()
const;
394 unsigned expectAnyEventId()
const;
395 std::wstring eventName(
unsigned eventId)
const;
396 template <
int length>
397 bool isOneOf(
const Token::Type types[length])
const;
398 template <
int length>
399 void expectOneOf(
const Token::Type types[length])
const;
401 void freeTemporaryMemory();
402 unsigned allocateTemporaryMemory(
const SourcePos varPos,
const unsigned size);
405 VariablesMap::const_iterator findVariable(
const std::wstring&
name,
const SourcePos& pos)
const;
406 FunctionsMap::const_iterator findFunction(
const std::wstring&
name,
const SourcePos& pos)
const;
407 ConstantsMap::const_iterator findConstant(
const std::wstring&
name,
const SourcePos& pos)
const;
408 EventsMap::const_iterator findGlobalEvent(
const std::wstring&
name,
const SourcePos& pos)
const;
409 EventsMap::const_iterator findAnyEvent(
const std::wstring&
name,
const SourcePos& pos)
const;
410 SubroutineReverseTable::const_iterator findSubroutine(
const std::wstring&
name,
const SourcePos& pos)
const;
411 bool constantExists(
const std::wstring&
name)
const;
413 void tokenize(std::wistream& source);
414 wchar_t getNextCharacter(std::wistream& source,
SourcePos& pos);
415 bool testNextCharacter(std::wistream& source,
SourcePos& pos,
wchar_t test, Token::Type tokenIfTrue);
416 void dumpTokens(std::wostream &dest)
const;
422 Node* parseProgram();
424 Node* parseStatement();
426 Node* parseBlockStatement();
429 void parseConstDef();
432 Node* parseAssignment();
433 Node* parseIfWhen(
bool edgeSensitive);
436 Node* parseOnEvent();
437 Node* parseEmit(
bool shorterArgsAllowed =
false);
438 Node* parseSubDecl();
439 Node* parseCallSub();
445 Node* parseCondition();
447 Node *parseBinaryOrExpression();
448 Node *parseBinaryXorExpression();
449 Node *parseBinaryAndExpression();
451 Node* parseShiftExpression();
452 Node* parseAddExpression();
453 Node* parseMultExpression();
454 Node* parseUnaryExpression();
455 Node* parseFunctionCall();
458 Node* parseConstantAndVariable();
460 unsigned parseVariableDefSize();
461 Node* tryParsingConstantExpression(
SourcePos pos,
int& constantResult);
std::wstring name
name of the function
Definition: compiler.h:93
ConstantsDefinitions constants
All globally defined constants.
Definition: compiler.h:248
std::map< unsigned, BytecodeVector > SubroutinesBytecode
Map of routines id to routine bytecode.
Definition: compiler.h:490
std::vector< NativeFunction > nativeFunctions
native functions
Definition: compiler.h:107
std::vector< LocalEvent > localEvents
events available locally on target
Definition: compiler.h:106
std::wstring description
description (some short documentation) of the event
Definition: compiler.h:74
SubroutinesBytecode subroutines
bytecode for routines
Definition: compiler.h:491
A bytecode element.
Definition: compiler.h:116
Definition: compiler.h:194
FunctionsMap getFunctionsMap() const
Get a FunctionsMap out of nativeFunctions.
Definition: compiler.cpp:84
A name - value pair.
Definition: compiler.h:215
An abstract node of syntax tree.
Definition: tree.h:47
std::map< unsigned, BytecodeVector > EventsBytecode
Map of events id to event bytecode.
Definition: compiler.h:486
Return error messages based on error ID (needed to translate messages for other applications) ...
Definition: compiler.h:169
unsigned maxStackDepth
maximum depth of the stack used by all the computations of the bytecode
Definition: compiler.h:134
Generic vector of name - value pairs.
Definition: compiler.h:231
unsigned size
size of variable in words
Definition: compiler.h:66
int value
value part of the pair
Definition: compiler.h:221
Typed parameter of native functions.
Definition: compiler.h:78
std::set< unsigned > ImplementedEvents
Lookup table to keep track of implemented events.
Definition: compiler.h:360
int iValue
int version of the value, 0 if not applicable
Definition: compiler.h:336
std::wstring message
message
Definition: compiler.h:184
NamedValuesVector EventsDescriptionsVector
Vector of events descriptions.
Definition: compiler.h:237
std::vector< SubroutineDescriptor > SubroutineTable
Lookup table for subroutines id => (name, address, line)
Definition: compiler.h:356
std::vector< NamedVariable > namedVariables
named variables
Definition: compiler.h:105
Description of target VM native function.
Definition: compiler.h:88
std::vector< std::wstring > VariablesNamesVector
Vector of names of variables.
Definition: compiler.h:212
VariablesMap variablesMap
variables lookup
Definition: compiler.h:466
Description of local event;.
Definition: compiler.h:71
A token is a parsed element of inputs.
Definition: compiler.h:262
uint16 crc() const
Compute the XModem CRC of the description, as defined in AS001 at https://aseba.wikidot.com/asebaspecifications.
Definition: compiler.cpp:42
std::deque< Token > tokens
parsed tokens
Definition: compiler.h:465
Definition: analysis.cpp:26
EventsMap globalEventsMap
global-events map
Definition: compiler.h:470
ImplementedEvents implementedEvents
list of implemented events
Definition: compiler.h:467
Node for L"callsub" no children.
Definition: tree.h:284
unsigned callDepth
for callable bytecode (i.e.
Definition: compiler.h:135
std::map< std::wstring, int > ConstantsMap
Lookup table for constant name => value.
Definition: compiler.h:362
NamedValuesVector ConstantsDefinitions
Vector of constants definitions.
Definition: compiler.h:240
std::wstring name
node name
Definition: compiler.h:98
std::map< std::wstring, unsigned > FunctionsMap
Lookup table for functions (name => id in target description)
Definition: compiler.h:55
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't exist, access to the full array is considered.
Definition: tree.h:552
unsigned column
column in source text
Definition: compiler.h:160
const TargetDescription * targetDescription
description of the target VM
Definition: compiler.h:476
NamedValue EventDescription
An event description is a name - value pair.
Definition: compiler.h:225
unsigned bytecodeSize
total amount of bytecode space
Definition: compiler.h:101
unsigned row
line in source text
Definition: compiler.h:159
Description of target VM named variable.
Definition: compiler.h:61
Compilation error.
Definition: compiler.h:181
BytecodeVector * current
pointer to bytecode being constructed
Definition: compiler.h:493
FunctionsMap functionsMap
functions lookup
Definition: compiler.h:468
Aseba Event Scripting Language compiler.
Definition: compiler.h:258
const std::wstring(* ErrorCallback)(ErrorCode error)
Type of the callback.
Definition: compiler.h:175
SubroutineReverseTable subroutineReverseTable
subroutine reverse lookup
Definition: compiler.h:473
bool valid
true if character, row and column hold valid values
Definition: compiler.h:161
Error()
Create an empty error.
Definition: compiler.h:188
unsigned variablesSize
total amount of variables space
Definition: compiler.h:102
Node for assignation.
Definition: tree.h:134
const CommonDefinitions * commonDefinitions
common definitions, such as events or some constants
Definition: compiler.h:477
unsigned short line
bytecode itself
Definition: compiler.h:125
std::wstring name
name of the parameter
Definition: compiler.h:84
NamedValue ConstantDefinition
An constant definition is a name - value pair.
Definition: compiler.h:228
unsigned character
position in source text
Definition: compiler.h:158
std::map< std::wstring, std::pair< unsigned, unsigned > > VariablesMap
Lookup table for variables name => (pos, size))
Definition: compiler.h:50
Description of target VM.
Definition: compiler.h:58
unsigned short uint16
16 bits unsigned integer
Definition: types.h:36
EventsMap allEventsMap
all-events map
Definition: compiler.h:471
std::wstring name
name part of the pair
Definition: compiler.h:220
Description of a subroutine.
Definition: compiler.h:347
std::map< std::wstring, unsigned > SubroutineReverseTable
Reverse Lookup table for subroutines name => id.
Definition: compiler.h:358
std::vector< NativeFunctionParameter > parameters
for each argument of the function, its size in words or, if negative, its template ID ...
Definition: compiler.h:95
ConstantsMap constantsMap
constants map
Definition: compiler.h:469
int size
if > 0 size of the parameter, if < 0 template id, if 0 any size
Definition: compiler.h:83
Bytecode array in the form of a dequeue, for construction.
Definition: compiler.h:129
EventsBytecode events
bytecode for events
Definition: compiler.h:487
std::wstring name
name of the variable
Definition: compiler.h:67
NamedValue(const std::wstring &name, int value)
Create a filled pair.
Definition: compiler.h:218
std::wstring name
name of the event
Definition: compiler.h:73
Node for assembling values into an array children[x] is the x-th Node to be assembled.
Definition: tree.h:524
Definitions common to several nodes, such as events or some constants.
Definition: compiler.h:243
unsigned endVariableIndex
(endMemory - endVariableIndex) is pointing to the first free variable at the end
Definition: compiler.h:475
unsigned lastLine
last line added, normally equal *this[this->size()-1].line, but may differ for instance on loops ...
Definition: compiler.h:136
unsigned protocolVersion
version of the aseba protocol
Definition: compiler.h:99
unsigned freeVariableIndex
index pointing to the first free variable
Definition: compiler.h:474
std::wstring description
description (some short documentation) of the function
Definition: compiler.h:94
SourcePos pos
position of error in source string
Definition: compiler.h:183
SubroutineTable subroutineTable
subroutine lookup
Definition: compiler.h:472
std::wstring sValue
string version of the value
Definition: compiler.h:335
std::map< unsigned, unsigned > EventAddressesToIdsMap
A map of event addresses to identifiers.
Definition: compiler.h:148
Bytecode use for compilation previous to linking.
Definition: compiler.h:483
SourcePos pos
position of token in source code
Definition: compiler.h:337
std::vector< short int > VariablesDataVector
Vector of data of variables.
Definition: compiler.h:255
std::map< std::wstring, unsigned > EventsMap
Lookup table for event name => id.
Definition: compiler.h:364
void clear()
Clear all the content.
Definition: compiler.h:251
EventsDescriptionsVector events
All defined events.
Definition: compiler.h:246
BytecodeVector()
Constructor.
Definition: compiler.h:132
Position in a source file or string. First is line, second is column.
Definition: compiler.h:156
VariablesMap getVariablesMap(unsigned &freeVariableIndex) const
Get a VariablesMap out of namedVariables, overwrite freeVariableIndex.
Definition: compiler.cpp:70
unsigned stackSize
depth of execution stack
Definition: compiler.h:103