Aseba  1.5.5
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
Aseba::Compiler Class Reference

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::vector< SubroutineDescriptorSubroutineTable
 Lookup table for subroutines id => (name, address, line)
 
typedef std::map< std::wstring, unsigned > SubroutineReverseTable
 Reverse Lookup table for subroutines name => id.
 
typedef std::set< unsigned > ImplementedEvents
 Lookup table to keep track of implemented events.
 
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.
 

Public Member Functions

 Compiler ()
 Constructor. You must setup a description using setTargetDescription() before any call to compile().
 
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().
 
const TargetDescriptiongetTargetDescription () const
 
const VariablesMapgetVariablesMap () const
 
const SubroutineTablegetSubroutineTable () const
 
void setCommonDefinitions (const CommonDefinitions *definitions)
 Set the common definitions, such as events or some constants.
 
bool compile (std::wistream &source, BytecodeVector &bytecode, unsigned &allocatedVariablesCount, Error &errorDescription, std::wostream *dump=0)
 Compile a new condition. More...
 
void setTranslateCallback (ErrorMessages::ErrorCallback newCB)
 

Static Public Member Functions

static std::wstring translate (ErrorCode error)
 
static bool isKeyword (const std::wstring &word)
 Return whether a string is a language keyword.
 

Protected Member Functions

void internalCompilerError () const
 There is a bug in the compiler, ask for a bug report.
 
void expect (const Token::Type &type) const
 Check if next token is of type, produce an exception otherwise.
 
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.
 
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.
 
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 expectPositiveConstant () const
 Check if next toxen is a valid positive part of a 16 bits signed integer constant.
 
int expectConstant () const
 Check if next toxen is a valid 16 bits signed integer constant.
 
unsigned expectPositiveInt16LiteralOrConstant () const
 Check and return either the positive part of a 16 bits signed integer or the value of a valid constant.
 
int expectInt16Literal ()
 Check and return a 16 bits signed integer.
 
int expectInt16LiteralOrConstant ()
 Check and return either a 16 bits signed integer or the value of a valid constant.
 
unsigned expectGlobalEventId () const
 Check if next token is a known global event identifier.
 
unsigned expectAnyEventId () const
 Check if next token is a known local or global event identifier.
 
std::wstring eventName (unsigned eventId) const
 Return the name of an event given its identifier.
 
template<int length>
bool isOneOf (const Token::Type types[length]) const
 Return true if next token is of the following types.
 
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.
 
void freeTemporaryMemory ()
 
unsigned allocateTemporaryMemory (const SourcePos varPos, const unsigned size)
 
AssignmentNodeallocateTemporaryVariable (const SourcePos varPos, Node *rValue)
 
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.
 
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.
 
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.
 
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.
 
EventsMap::const_iterator findAnyEvent (const std::wstring &name, const SourcePos &pos) const
 
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.
 
bool constantExists (const std::wstring &name) const
 Return true if a constant of a given name exists.
 
void buildMaps ()
 Build variables and functions maps.
 
void tokenize (std::wistream &source)
 Parse source and build tokens vector. More...
 
wchar_t getNextCharacter (std::wistream &source, SourcePos &pos)
 
bool testNextCharacter (std::wistream &source, SourcePos &pos, wchar_t test, Token::Type tokenIfTrue)
 
void dumpTokens (std::wostream &dest) const
 Debug print of tokens.
 
bool verifyStackCalls (PreLinkBytecode &preLinkBytecode)
 Verify that no call path can create a stack overflow.
 
bool link (const PreLinkBytecode &preLinkBytecode, BytecodeVector &bytecode)
 Create the final bytecode for a microcontroller.
 
void disassemble (BytecodeVector &bytecode, const PreLinkBytecode &preLinkBytecode, std::wostream &dump) const
 Disassemble a microcontroller bytecode and dump it.
 
NodeparseProgram ()
 Parse "program" grammar element.
 
NodeparseStatement ()
 Parse "statement" grammar element.
 
NodeparseBlockStatement ()
 Parse "block statement" grammar element.
 
NodeparseReturn ()
 Parse "return statement" grammar element.
 
void parseConstDef ()
 Parse "const def" elements.
 
NodeparseVarDef ()
 Parse "var def" grammar element.
 
AssignmentNodeparseVarDefInit (MemoryVectorNode *lValue)
 
NodeparseAssignment ()
 Parse "assignment" grammar element.
 
NodeparseIfWhen (bool edgeSensitive)
 Parse "if" grammar element.
 
NodeparseFor ()
 Parse "for" grammar element.
 
NodeparseWhile ()
 Parse "while" grammar element.
 
NodeparseOnEvent ()
 Parse "onevent" grammar element.
 
NodeparseEmit (bool shorterArgsAllowed=false)
 Parse "event" grammar element.
 
NodeparseSubDecl ()
 Parse "sub" grammar element, declaration of subroutine.
 
NodeparseCallSub ()
 Parse "subcall" grammar element, call of subroutine.
 
NodeparseOr ()
 Parse "or" grammar element.
 
NodeparseAnd ()
 Parse "and" grammar element.
 
NodeparseNot ()
 Parse "not" grammar element.
 
NodeparseCondition ()
 Parse "condition" grammar element.
 
NodeparseBinaryOrExpression ()
 Parse "binary or" grammar element.
 
NodeparseBinaryXorExpression ()
 Parse "binary xor" grammar element.
 
NodeparseBinaryAndExpression ()
 Parse "binary and" grammar element.
 
NodeparseShiftExpression ()
 Parse "shift_expression" grammar element.
 
NodeparseAddExpression ()
 Parse "add_expression" grammar element.
 
NodeparseMultExpression ()
 Parse "mult_expression" grammar element.
 
NodeparseUnaryExpression ()
 Parse "unary_expression" grammar element.
 
NodeparseFunctionCall ()
 Parse "function_call" grammar element.
 
TupleVectorNodeparseTupleVector (bool compatibility=false)
 Parse "[ .... ]" grammar element.
 
NodeparseConstantAndVariable ()
 
MemoryVectorNodeparseVariable ()
 
unsigned parseVariableDefSize ()
 
NodetryParsingConstantExpression (SourcePos pos, int &constantResult)
 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)
 
int expectConstantExpression (SourcePos pos, Node *tree)
 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.
 

Protected Attributes

std::deque< Tokentokens
 parsed tokens
 
VariablesMap variablesMap
 variables lookup
 
ImplementedEvents implementedEvents
 list of implemented events
 
FunctionsMap functionsMap
 functions lookup
 
ConstantsMap constantsMap
 constants map
 
EventsMap globalEventsMap
 global-events map
 
EventsMap allEventsMap
 all-events map
 
SubroutineTable subroutineTable
 subroutine lookup
 
SubroutineReverseTable subroutineReverseTable
 subroutine reverse lookup
 
unsigned freeVariableIndex
 index pointing to the first free variable
 
unsigned endVariableIndex
 (endMemory - endVariableIndex) is pointing to the first free variable at the end
 
const TargetDescriptiontargetDescription
 description of the target VM
 
const CommonDefinitionscommonDefinitions
 common definitions, such as events or some constants
 
ErrorMessages translator
 

Friends

struct AssignmentNode
 
struct CallSubNode
 

Detailed Description

Aseba Event Scripting Language compiler.

Member Function Documentation

void Aseba::Compiler::tokenize ( std::wistream &  source)
protected

Parse source and build tokens vector.

Parameters
sourcesource code

References Aseba::SourcePos::character, Aseba::SourcePos::column, Aseba::Compiler::Token::pos, Aseba::SourcePos::row, and tokens.


The documentation for this class was generated from the following files: