Aseba Event Scripting Language compiler. More...
#include <compiler.h>
Classes | |
struct | SubroutineDescriptor |
Description of a subroutine. More... | |
struct | Token |
A token is a parsed element of inputs. More... | |
Public Types | |
typedef std::map< std::wstring, int > | ConstantsMap |
Lookup table for constant name => value. | |
typedef std::map< std::wstring, unsigned > | EventsMap |
Lookup table for event name => id. | |
typedef std::map< std::wstring, unsigned > | FunctionsMap |
Lookup table for functions (name => id in target description) | |
typedef std::set< unsigned > | ImplementedEvents |
Lookup table to keep track of implemented events. | |
typedef std::map< std::wstring, unsigned > | SubroutineReverseTable |
Reverse Lookup table for subroutines name => id. | |
typedef std::vector < SubroutineDescriptor > | SubroutineTable |
Lookup table for subroutines id => (name, address, line) | |
typedef std::map< std::wstring, std::pair< unsigned, unsigned > > | VariablesMap |
Lookup table for variables name => (pos, size)) | |
Public Member Functions | |
bool | compile (std::wistream &source, BytecodeVector &bytecode, unsigned &allocatedVariablesCount, Error &errorDescription, std::wostream *dump=0) |
Compiler () | |
Constructor. You must setup a description using setTargetDescription() before any call to compile(). | |
const TargetDescription * | getTargetDescription () const |
const VariablesMap * | getVariablesMap () const |
void | setCommonDefinitions (const CommonDefinitions *definitions) |
Set the common definitions, such as events or some constants. | |
void | setTargetDescription (const TargetDescription *description) |
Set the description of the target as returned by the microcontroller. You must call this function before any call to compile(). | |
void | setTranslateCallback (ErrorMessages::ErrorCallback newCB) |
Static Public Member Functions | |
static std::wstring | translate (ErrorCode error) |
Protected Member Functions | |
void | allocateTemporaryMemory (const SourcePos varPos, const unsigned size, unsigned &varAddr) |
AssignmentNode * | allocateTemporaryVariable (const SourcePos varPos, Node *rValue) |
void | buildMaps () |
Build variables and functions maps. | |
bool | constantExists (const std::wstring &name) const |
Return true if a constant of a given name exists. | |
void | disassemble (BytecodeVector &bytecode, const PreLinkBytecode &preLinkBytecode, std::wostream &dump) const |
Disassemble a microcontroller bytecode and dump it. | |
void | dumpTokens (std::wostream &dest) const |
Debug print of tokens. | |
std::wstring | eventName (unsigned eventId) const |
Return the name of an event given its identifier. | |
void | expect (const Token::Type &type) const |
Check if next token is of type, produce an exception otherwise. | |
int | expectAbsoluteInt16Literal (bool negative) const |
Check if next token is the absolute part of a 16 bits signed integer literal. If so, return it, if not, throw an exception. | |
unsigned | expectAnyEventId () const |
Check if next token is a known local or global event identifier. | |
int | expectConstant () const |
Check if next toxen is a valid 16 bits signed integer constant. | |
int | expectConstantExpression (SourcePos pos, Node *tree) |
unsigned | expectGlobalEventId () const |
Check if next token is a known global event identifier. | |
int | expectInt16LiteralOrConstant () |
Check and return either a 16 bits signed integer or the value of a valid constant. | |
template<int length> | |
void | expectOneOf (const Token::Type types[length]) const |
Check if next token is of one of the following types, produce an exception otherwise. | |
unsigned | expectPositiveConstant () const |
Check if next toxen is a valid positive part of a 16 bits signed integer constant. | |
unsigned | expectPositiveInt16Literal () const |
Check if next token is the positive part of a 16 bits signed integer literal. If so, return it, if not, throw an exception. | |
unsigned | expectPositiveInt16LiteralOrConstant () const |
Check and return either the positive part of a 16 bits signed integer or the value of a valid constant. | |
unsigned | expectUInt12Literal () const |
Check if next token is an unsigned 12 bits integer literal. If so, return it, if not, throw an exception. | |
unsigned | expectUInt16Literal () const |
Check if next token is an unsigned 16 bits integer literal. If so, return it, if not, throw an exception. | |
EventsMap::const_iterator | findAnyEvent (const std::wstring &name, const SourcePos &pos) const |
ConstantsMap::const_iterator | findConstant (const std::wstring &name, const SourcePos &pos) const |
Look for a constant of a given name, and if found, return an iterator; if not, return an exception. | |
FunctionsMap::const_iterator | findFunction (const std::wstring &name, const SourcePos &pos) const |
Look for a function of a given name, and if found, return an iterator; if not, return an exception. | |
EventsMap::const_iterator | findGlobalEvent (const std::wstring &name, const SourcePos &pos) const |
Look for a global event of a given name, and if found, return an iterator; if not, return an exception. | |
SubroutineReverseTable::const_iterator | findSubroutine (const std::wstring &name, const SourcePos &pos) const |
Look for a subroutine of a given name, and if found, return an iterator; if not, return an exception. | |
VariablesMap::const_iterator | findVariable (const std::wstring &name, const SourcePos &pos) const |
Look for a variable of a given name, and if found, return an iterator; if not, return an exception. | |
void | freeTemporaryMemory () |
wchar_t | getNextCharacter (std::wistream &source, SourcePos &pos) |
void | internalCompilerError () const |
There is a bug in the compiler, ask for a bug report. | |
template<int length> | |
bool | isOneOf (const Token::Type types[length]) const |
Return true if next token is of the following types. | |
bool | link (const PreLinkBytecode &preLinkBytecode, BytecodeVector &bytecode) |
Create the final bytecode for a microcontroller. | |
Node * | parseAddExpression () |
Parse "add_expression" grammar element. | |
Node * | parseAnd () |
Parse "and" grammar element. | |
Node * | parseAssignment () |
Parse "assignment" grammar element. | |
Node * | parseBinaryAndExpression () |
Parse "binary and" grammar element. | |
Node * | parseBinaryOrExpression () |
Parse "binary or" grammar element. | |
Node * | parseBinaryXorExpression () |
Parse "binary xor" grammar element. | |
Node * | parseBlockStatement () |
Parse "block statement" grammar element. | |
Node * | parseCallSub () |
Parse "subcall" grammar element, call of subroutine. | |
Node * | parseCondition () |
Parse "condition" grammar element. | |
Node * | parseConstantAndVariable () |
Node * | parseEmit () |
Parse "event" grammar element. | |
Node * | parseFor () |
Parse "for" grammar element. | |
Node * | parseFunctionCall () |
Parse "function_call" grammar element. | |
Node * | parseIfWhen (bool edgeSensitive) |
Parse "if" grammar element. | |
Node * | parseMultExpression () |
Parse "mult_expression" grammar element. | |
Node * | parseNot () |
Parse "not" grammar element. | |
Node * | parseOnEvent () |
Parse "onevent" grammar element. | |
Node * | parseOr () |
Parse "or" grammar element. | |
Node * | parseProgram () |
Parse "program" grammar element. | |
Node * | parseReturn () |
Parse "return statement" grammar element. | |
Node * | parseShiftExpression () |
Parse "shift_expression" grammar element. | |
Node * | parseStatement () |
Parse "statement" grammar element. | |
Node * | parseSubDecl () |
Parse "sub" grammar element, declaration of subroutine. | |
TupleVectorNode * | parseTupleVector (bool compatibility=false) |
Parse "[ .... ]" grammar element. | |
Node * | parseUnaryExpression () |
Parse "unary_expression" grammar element. | |
Node * | parseVarDef () |
Parse "var def" grammar element. | |
AssignmentNode * | parseVarDefInit (MemoryVectorNode *lValue) |
MemoryVectorNode * | parseVariable () |
unsigned | parseVariableDefSize () |
Node * | parseWhile () |
Parse "while" grammar element. | |
bool | testNextCharacter (std::wistream &source, SourcePos &pos, wchar_t test, Token::Type tokenIfTrue) |
void | tokenize (std::wistream &source) |
Node * | tryParsingConstantExpression (SourcePos pos, int &constantResult) |
bool | verifyStackCalls (PreLinkBytecode &preLinkBytecode) |
Verify that no call path can create a stack overflow. | |
Protected Attributes | |
EventsMap | allEventsMap |
all-events map | |
const CommonDefinitions * | commonDefinitions |
common definitions, such as events or some constants | |
ConstantsMap | constantsMap |
constants map | |
unsigned | endVariableIndex |
(endMemory - endVariableIndex) is pointing to the first free variable at the end | |
unsigned | freeVariableIndex |
index pointing to the first free variable | |
FunctionsMap | functionsMap |
functions lookup | |
EventsMap | globalEventsMap |
global-events map | |
ImplementedEvents | implementedEvents |
list of implemented events | |
SubroutineReverseTable | subroutineReverseTable |
subroutine reverse lookup | |
SubroutineTable | subroutineTable |
subroutine lookup | |
const TargetDescription * | targetDescription |
description of the target VM | |
std::deque< Token > | tokens |
parsed tokens | |
ErrorMessages | translator |
VariablesMap | variablesMap |
variables lookup |
Aseba Event Scripting Language compiler.
Definition at line 250 of file compiler.h.
typedef std::map<std::wstring, int> Aseba::Compiler::ConstantsMap |
Lookup table for constant name => value.
Definition at line 356 of file compiler.h.
typedef std::map<std::wstring, unsigned> Aseba::Compiler::EventsMap |
Lookup table for event name => id.
Definition at line 358 of file compiler.h.
typedef std::map<std::wstring, unsigned> Aseba::Compiler::FunctionsMap |
Lookup table for functions (name => id in target description)
Definition at line 339 of file compiler.h.
typedef std::set<unsigned> Aseba::Compiler::ImplementedEvents |
Lookup table to keep track of implemented events.
Definition at line 354 of file compiler.h.
typedef std::map<std::wstring, unsigned> Aseba::Compiler::SubroutineReverseTable |
Reverse Lookup table for subroutines name => id.
Definition at line 352 of file compiler.h.
typedef std::vector<SubroutineDescriptor> Aseba::Compiler::SubroutineTable |
Lookup table for subroutines id => (name, address, line)
Definition at line 350 of file compiler.h.
typedef std::map<std::wstring, std::pair<unsigned, unsigned> > Aseba::Compiler::VariablesMap |
Lookup table for variables name => (pos, size))
Definition at line 337 of file compiler.h.
void Aseba::Compiler::allocateTemporaryMemory | ( | const SourcePos | varPos, |
const unsigned | size, | ||
unsigned & | varAddr | ||
) | [protected] |
Definition at line 237 of file parser.cpp.
AssignmentNode * Aseba::Compiler::allocateTemporaryVariable | ( | const SourcePos | varPos, |
Node * | rValue | ||
) | [protected] |
Definition at line 249 of file parser.cpp.
void Aseba::Compiler::dumpTokens | ( | std::wostream & | dest | ) | const [protected] |
std::wstring Aseba::Compiler::eventName | ( | unsigned | eventId | ) | const [protected] |
Return the name of an event given its identifier.
Definition at line 188 of file parser.cpp.
void Aseba::Compiler::expect | ( | const Token::Type & | type | ) | const [protected] |
Check if next token is of type, produce an exception otherwise.
Definition at line 44 of file parser.cpp.
int Aseba::Compiler::expectAbsoluteInt16Literal | ( | bool | negative | ) | const [protected] |
Check if next token is the absolute part of a 16 bits signed integer literal. If so, return it, if not, throw an exception.
Definition at line 87 of file parser.cpp.
unsigned Aseba::Compiler::expectAnyEventId | ( | ) | const [protected] |
Check if next token is a known local or global event identifier.
Definition at line 174 of file parser.cpp.
int Aseba::Compiler::expectConstant | ( | ) | const [protected] |
Check if next toxen is a valid 16 bits signed integer constant.
Definition at line 118 of file parser.cpp.
int Aseba::Compiler::expectConstantExpression | ( | SourcePos | pos, |
Node * | tree | ||
) | [protected] |
This is a generalization of expectPositiveInt16LiteralOrConstant() Try to reduce the expression into a single figure, if not raise an exception The tree pointed by "tree" is deleted during execution, not safe to use it after
Definition at line 1217 of file parser.cpp.
unsigned Aseba::Compiler::expectGlobalEventId | ( | ) | const [protected] |
Check if next token is a known global event identifier.
Definition at line 160 of file parser.cpp.
int Aseba::Compiler::expectInt16LiteralOrConstant | ( | ) | [protected] |
Check and return either a 16 bits signed integer or the value of a valid constant.
Definition at line 144 of file parser.cpp.
void Aseba::Compiler::expectOneOf | ( | const Token::Type | types[length] | ) | const [protected] |
Check if next token is of one of the following types, produce an exception otherwise.
Definition at line 216 of file parser.cpp.
unsigned Aseba::Compiler::expectPositiveConstant | ( | ) | const [protected] |
Check if next toxen is a valid positive part of a 16 bits signed integer constant.
Definition at line 101 of file parser.cpp.
unsigned Aseba::Compiler::expectPositiveInt16Literal | ( | ) | const [protected] |
Check if next token is the positive part of a 16 bits signed integer literal. If so, return it, if not, throw an exception.
Definition at line 76 of file parser.cpp.
unsigned Aseba::Compiler::expectPositiveInt16LiteralOrConstant | ( | ) | const [protected] |
Check and return either the positive part of a 16 bits signed integer or the value of a valid constant.
Definition at line 135 of file parser.cpp.
unsigned Aseba::Compiler::expectUInt12Literal | ( | ) | const [protected] |
Check if next token is an unsigned 12 bits integer literal. If so, return it, if not, throw an exception.
Definition at line 54 of file parser.cpp.
unsigned Aseba::Compiler::expectUInt16Literal | ( | ) | const [protected] |
Check if next token is an unsigned 16 bits integer literal. If so, return it, if not, throw an exception.
Definition at line 65 of file parser.cpp.
void Aseba::Compiler::freeTemporaryMemory | ( | ) | [protected] |
Definition at line 232 of file parser.cpp.
wchar_t Aseba::Compiler::getNextCharacter | ( | std::wistream & | source, |
SourcePos & | pos | ||
) | [protected] |
const TargetDescription* Aseba::Compiler::getTargetDescription | ( | ) | const [inline] |
Definition at line 363 of file compiler.h.
const VariablesMap* Aseba::Compiler::getVariablesMap | ( | ) | const [inline] |
Definition at line 364 of file compiler.h.
void Aseba::Compiler::internalCompilerError | ( | ) | const [protected] |
There is a bug in the compiler, ask for a bug report.
Definition at line 38 of file parser.cpp.
bool Aseba::Compiler::isOneOf | ( | const Token::Type | types[length] | ) | const [protected] |
Return true if next token is of the following types.
Definition at line 204 of file parser.cpp.
Node * Aseba::Compiler::parseAddExpression | ( | ) | [protected] |
Parse "add_expression" grammar element.
Definition at line 924 of file parser.cpp.
Node * Aseba::Compiler::parseAnd | ( | ) | [protected] |
Parse "and" grammar element.
Definition at line 767 of file parser.cpp.
Node * Aseba::Compiler::parseAssignment | ( | ) | [protected] |
Parse "assignment" grammar element.
Definition at line 418 of file parser.cpp.
Node * Aseba::Compiler::parseBinaryAndExpression | ( | ) | [protected] |
Parse "binary and" grammar element.
Definition at line 889 of file parser.cpp.
Node * Aseba::Compiler::parseBinaryOrExpression | ( | ) | [protected] |
Parse "binary or" grammar element.
Definition at line 857 of file parser.cpp.
Node * Aseba::Compiler::parseBinaryXorExpression | ( | ) | [protected] |
Parse "binary xor" grammar element.
Definition at line 873 of file parser.cpp.
Node * Aseba::Compiler::parseBlockStatement | ( | ) | [protected] |
Parse "block statement" grammar element.
Definition at line 304 of file parser.cpp.
Node * Aseba::Compiler::parseCallSub | ( | ) | [protected] |
Parse "subcall" grammar element, call of subroutine.
Definition at line 735 of file parser.cpp.
Node * Aseba::Compiler::parseCondition | ( | ) | [protected] |
Parse "condition" grammar element.
Definition at line 826 of file parser.cpp.
Node * Aseba::Compiler::parseConstantAndVariable | ( | ) | [protected] |
Definition at line 1079 of file parser.cpp.
Node * Aseba::Compiler::parseEmit | ( | ) | [protected] |
Parse "event" grammar element.
Definition at line 672 of file parser.cpp.
Node * Aseba::Compiler::parseFor | ( | ) | [protected] |
Parse "for" grammar element.
Definition at line 539 of file parser.cpp.
Node * Aseba::Compiler::parseFunctionCall | ( | ) | [protected] |
Parse "function_call" grammar element.
Definition at line 1266 of file parser.cpp.
Node * Aseba::Compiler::parseIfWhen | ( | bool | edgeSensitive | ) | [protected] |
Parse "if" grammar element.
Definition at line 476 of file parser.cpp.
Node * Aseba::Compiler::parseMultExpression | ( | ) | [protected] |
Parse "mult_expression" grammar element.
Definition at line 943 of file parser.cpp.
Node * Aseba::Compiler::parseNot | ( | ) | [protected] |
Parse "not" grammar element.
Definition at line 783 of file parser.cpp.
Node * Aseba::Compiler::parseOnEvent | ( | ) | [protected] |
Parse "onevent" grammar element.
Definition at line 656 of file parser.cpp.
Node * Aseba::Compiler::parseOr | ( | ) | [protected] |
Parse "or" grammar element.
Definition at line 751 of file parser.cpp.
Node * Aseba::Compiler::parseProgram | ( | ) | [protected] |
Parse "program" grammar element.
Definition at line 266 of file parser.cpp.
Node * Aseba::Compiler::parseReturn | ( | ) | [protected] |
Parse "return statement" grammar element.
Definition at line 321 of file parser.cpp.
Node * Aseba::Compiler::parseShiftExpression | ( | ) | [protected] |
Parse "shift_expression" grammar element.
Definition at line 905 of file parser.cpp.
Node * Aseba::Compiler::parseStatement | ( | ) | [protected] |
Parse "statement" grammar element.
Definition at line 289 of file parser.cpp.
Node * Aseba::Compiler::parseSubDecl | ( | ) | [protected] |
Parse "sub" grammar element, declaration of subroutine.
Definition at line 713 of file parser.cpp.
TupleVectorNode * Aseba::Compiler::parseTupleVector | ( | bool | compatibility = false | ) | [protected] |
Parse "[ .... ]" grammar element.
Definition at line 1027 of file parser.cpp.
Node * Aseba::Compiler::parseUnaryExpression | ( | ) | [protected] |
Parse "unary_expression" grammar element.
Definition at line 962 of file parser.cpp.
Node * Aseba::Compiler::parseVarDef | ( | ) | [protected] |
Parse "var def" grammar element.
Definition at line 329 of file parser.cpp.
AssignmentNode * Aseba::Compiler::parseVarDefInit | ( | MemoryVectorNode * | lValue | ) | [protected] |
Definition at line 386 of file parser.cpp.
MemoryVectorNode * Aseba::Compiler::parseVariable | ( | ) | [protected] |
Definition at line 1096 of file parser.cpp.
unsigned Aseba::Compiler::parseVariableDefSize | ( | ) | [protected] |
Definition at line 1160 of file parser.cpp.
Node * Aseba::Compiler::parseWhile | ( | ) | [protected] |
Parse "while" grammar element.
Definition at line 631 of file parser.cpp.
void Aseba::Compiler::setTranslateCallback | ( | ErrorMessages::ErrorCallback | newCB | ) | [inline] |
Definition at line 367 of file compiler.h.
bool Aseba::Compiler::testNextCharacter | ( | std::wistream & | source, |
SourcePos & | pos, | ||
wchar_t | test, | ||
Token::Type | tokenIfTrue | ||
) | [protected] |
void Aseba::Compiler::tokenize | ( | std::wistream & | source | ) | [protected] |
static std::wstring Aseba::Compiler::translate | ( | ErrorCode | error | ) | [inline, static] |
Definition at line 368 of file compiler.h.
Node * Aseba::Compiler::tryParsingConstantExpression | ( | SourcePos | pos, |
int & | constantResult | ||
) | [protected] |
Use parseBinaryOrExpression() and try to reduce the result into an integer If successful, return NULL and the integer in constantResult If unsuccessful, return the parsed tree (constantResult useless in this case)
Definition at line 1197 of file parser.cpp.
EventsMap Aseba::Compiler::allEventsMap [protected] |
all-events map
Definition at line 458 of file compiler.h.
const CommonDefinitions* Aseba::Compiler::commonDefinitions [protected] |
common definitions, such as events or some constants
Definition at line 464 of file compiler.h.
ConstantsMap Aseba::Compiler::constantsMap [protected] |
constants map
Definition at line 456 of file compiler.h.
unsigned Aseba::Compiler::endVariableIndex [protected] |
(endMemory - endVariableIndex) is pointing to the first free variable at the end
Definition at line 462 of file compiler.h.
unsigned Aseba::Compiler::freeVariableIndex [protected] |
index pointing to the first free variable
Definition at line 461 of file compiler.h.
FunctionsMap Aseba::Compiler::functionsMap [protected] |
functions lookup
Definition at line 455 of file compiler.h.
EventsMap Aseba::Compiler::globalEventsMap [protected] |
global-events map
Definition at line 457 of file compiler.h.
ImplementedEvents Aseba::Compiler::implementedEvents [protected] |
list of implemented events
Definition at line 454 of file compiler.h.
subroutine reverse lookup
Definition at line 460 of file compiler.h.
SubroutineTable Aseba::Compiler::subroutineTable [protected] |
subroutine lookup
Definition at line 459 of file compiler.h.
const TargetDescription* Aseba::Compiler::targetDescription [protected] |
description of the target VM
Definition at line 463 of file compiler.h.
std::deque<Token> Aseba::Compiler::tokens [protected] |
parsed tokens
Definition at line 452 of file compiler.h.
ErrorMessages Aseba::Compiler::translator [protected] |
Definition at line 466 of file compiler.h.
VariablesMap Aseba::Compiler::variablesMap [protected] |
variables lookup
Definition at line 453 of file compiler.h.