Falcon::Compiler Class Reference

FALCON source compiler. More...

#include <compiler.h>

Inheritance diagram for Falcon::Compiler:

Inheritance graph
[legend]

List of all members.

Public Member Functions

void addAttribute (const String &name, Value *val, uint32 line=0)
 Adds an attribute to the currently active context-sensible symbol.
void addClass (Statement *stm)
void addConstant (const String &name, Value *val, uint32 line=0)
void addEnumerator (const String &name)
 Add an enumeration item to current enumeration.
void addEnumerator (const String &name, Value *value)
 Add an enumeration item to current enumeration.
void addError ()
void addFunction (Statement *stm)
SymboladdGlobalSymbol (const String *symname)
SymboladdGlobalVar (const String *symname, VarDef *value)
 Creates a symbol that will be an initially defined global variable.
void addIntConstant (const String &name, int64 value, uint32 line=0)
virtual void addLoad (const String &name, bool isFilename)
 Adds a direct load request to the module being compiled.
SymboladdLocalSymbol (const String *symname, bool parameter)
virtual void addNamespace (const String &nspace, const String &alias, bool full=false, bool filename=false)
 Adds a known namespace.
void addNilConstant (const String &name, uint32 line=0)
void addNumConstant (const String &name, numeric value, uint32 line=0)
void addStatement (Statement *stm)
StringaddString (const String &str)
void addStringConstant (const String &name, const String &value, uint32 line=0)
void addSymdef (Value *val)
StmtFunctionbuildCtorFor (StmtClass *sym)
 Builds the constructor function for a given class.
bool checkLocalUndefined ()
 Checks if the current statemsnt has referenced a locally undefined symbol.
ValuecloseClosure ()
 Closes the currently worked on closure.
void closeFunction ()
 Cleanup for function closing.
bool compile (Module *mod, Stream *input)
 Compile a module from a stream.
bool compile ()
 Compiles the module given in the constructor.
 Compiler (Module *mod, Stream *input)
 Creates the compiler setting a default module and input stream.
 Compiler ()
 Creates an empty compiler.
void decClosureContext ()
bool defContext () const
void defContext (bool ctx)
void defineVal (ArrayDecl *val)
 Define all the values int the given array definition.
void defineVal (Value *val)
 Process an include instruction.
ErrordetachErrors ()
 Passes the ownership of the error structure to the caller.
int errors () const
const ValuegetConstant (const String &name)
 Seek a constant in the predefined constant list.
StatementgetContext () const
StatementListgetContextSet () const
FuncDefgetFunction () const
StmtFunctiongetFunctionContext () const
StatementgetLoop () const
Symbolglobalize (const String *str)
virtual SymbolimportAlias (const String *symName, const String *fromMod, const String *alias, bool filename=false)
 Import a single aliased symbol from a module.
virtual void importSymbols (List *lst, const String *prefix=0, const String *alias=0, bool filename=false)
 Import the symbols named in a List.
void incClosureContext ()
void incLambdaCount ()
bool isInteractive () const
 Checks if this compiler is as an interactive I/O.
bool isLocalContext ()
bool isNamespace (const String &symName)
 Return true if the current symbol is actually a namespace.
int lambdaCount () const
SrcLexerlexer () const
void metaCompile (const String &data, int startline)
 Performs a meta compilation.
Modulemodule () const
void parsingFtd (bool b)
bool parsingFtd () const
 Are we parsing a normal file or an escaped template file?
void popContext ()
void popContextSet ()
void popFunction ()
void popFunctionContext ()
void popLoop ()
void pushContext (Statement *stm)
void pushContextSet (StatementList *list)
void pushFunction (FuncDef *f)
void pushFunctionContext (StmtFunction *func)
void pushLoop (Statement *stm)
void raiseContextError (int code, int line, int startLine)
 Raises an error related to a context problem.
void raiseError (int errorNum, const String &errorp, int errorLine=0)
void raiseError (Error *e)
 Raises an error.
void raiseError (int errorNum, int errorLine=0)
 Front-end to raiseError( *e ).
void reset ()
 Reset compiler settings to defaults and prepares for a new compilation.
void resetEnum ()
SymbolsearchGlobalSymbol (const String *symname)
SymbolsearchLocalSymbol (const String *symname, bool bRecurse=false)
 Searches a symbol in the local context.
SymbolsearchOuterSymbol (const String *symname)
void searchPath (const String &path)
 Sets the compiler-specific search path.
const StringsearchPath () const
 Return the search path inherited from upper facilities.
void serviceLoader (ModuleLoader *l)
 Sets the service VM for this compiler.
ModuleLoaderserviceLoader () const
 Gets the service VM for this compiler.
void serviceVM (VMachine *vm)
 Sets the service VM for this compiler.
VMachineserviceVM () const
 Gets the service VM for this compiler.
bool setDirective (const String &directive, int64 value, bool bRaise=true)
 Set directive as string value.
bool setDirective (const String &directive, const String &value, bool bRaise=true)
 Set directive as string value.
void setInteractive (bool bint)
 Sets this compiler as interactive.
SourceTreesourceTree () const
void staticPrefix (const String *v)
const StringstaticPrefix () const
 Return the current static prefix, if any.
Streamstream () const
bool strictMode () const
void strictMode (bool breq)
 Activate "strict" feature.
int tempLine () const
void tempLine (int line)
 Store current line for later error signaling.
virtual ~Compiler ()
 Destroys the compiler.

Protected Types


Protected Member Functions

void addPredefs ()
 Add predefined symbols and constants.
void clear ()
 Removes all the structures and temporary data used to compile a file.
void init ()
 Initializes structures and variables used for compilation.

Protected Attributes

List m_alias
 Aliased symbols are stored here.
bool m_bInteractive
bool m_bParsingFtd
int m_closureContexts
Map m_constants
 Map of constants.
List m_context
 Leading instruction that owns currently parsed statements.
List m_contextSet
 Area to save currently parsed statements.
bool m_defContext
int64 m_enumId
int m_errors
List m_func_ctx
 Context limited to leading function instructions.
List m_functions
 Stack of currently active functions.
int m_lambdaCount
String m_language
 Directive language.
SrcLexerm_lexer
List m_loops
 Leading instruction, specialized for loops.
InteractiveCompilerm_metacomp
Modulem_module
 This is the module that is being formed in the meanwhile.
int64 m_modVersion
 Directive version.
Map m_namespaces
 Map of namespaces.
int m_optLevel
SourceTreem_root
Errorm_rootError
String m_searchPath
 Search path inherited from upper facilities.
ModuleLoaderm_serviceLoader
VMachinem_serviceVM
List m_statementVals
 Last statement's symbols.
const Stringm_staticPrefix
 The static prefix is the name of the symbol currently declaring the static namespace.
Streamm_stream
bool m_strict
 Directive strict.
int m_tempLine


Detailed Description

FALCON source compiler.

This is the class that is responsible to create a syntactic tree given a linear input stream. The stream may be from file, standard input or from a memory buffer; in this phase, the compiler does not need the ability of random file access, so even a network stream may be used.


Member Enumeration Documentation

Declaration context.

Depending on where a variable is initialized, or how a symbol is declared, the final symbol added to the module may be different. This enumeration is used to keep track of the current declaration context.

Enumerator:
e_dc_global 
e_dc_param 
e_dc_static_func 
e_dc_func_body 
e_dc_static_class 
e_dc_class_body 


Constructor & Destructor Documentation

Falcon::Compiler::Compiler (  ) 

Creates an empty compiler.

This constructor doesn't set a stream and a module for the compiler. It is intended for repeated usage through compile( Module *, Stream *).

Falcon::Compiler::Compiler ( Module mod,
Stream input 
)

Creates the compiler setting a default module and input stream.

This configures this instance as a single-file-compilation only compiler. After the compile() call, the instance may (should) be disposed. However, after calling this constructor it is possible to use the compiler( Module *, Stram *) as well.

virtual Falcon::Compiler::~Compiler (  )  [virtual]

Destroys the compiler.

Internally calls clear()


Member Function Documentation

void Falcon::Compiler::addAttribute ( const String name,
Value val,
uint32  line = 0 
)

Adds an attribute to the currently active context-sensible symbol.

void Falcon::Compiler::addClass ( Statement stm  )  [inline]

void Falcon::Compiler::addConstant ( const String name,
Value val,
uint32  line = 0 
)

void Falcon::Compiler::addEnumerator ( const String name  ) 

Add an enumeration item to current enumeration.

This version assigns the enumerated value a progressive integer.

void Falcon::Compiler::addEnumerator ( const String name,
Value value 
)

Add an enumeration item to current enumeration.

void Falcon::Compiler::addError (  )  [inline]

void Falcon::Compiler::addFunction ( Statement stm  )  [inline]

Symbol* Falcon::Compiler::addGlobalSymbol ( const String symname  ) 

Symbol* Falcon::Compiler::addGlobalVar ( const String symname,
VarDef value 
)

Creates a symbol that will be an initially defined global variable.

The global variables may be created with an initial values (i.e. for static declarations). This function adds the global symbol for the variable and sets it to the default value.

void Falcon::Compiler::addIntConstant ( const String name,
int64  value,
uint32  line = 0 
)

virtual void Falcon::Compiler::addLoad ( const String name,
bool  isFilename 
) [inline, virtual]

Adds a direct load request to the module being compiled.

Parameters:
name The name of the module to be loaded
isFilename True if the name to be loaded is actually a filename path.

Reimplemented in Falcon::InteractiveCompiler.

Symbol* Falcon::Compiler::addLocalSymbol ( const String symname,
bool  parameter 
)

virtual void Falcon::Compiler::addNamespace ( const String nspace,
const String alias,
bool  full = false,
bool  filename = false 
) [virtual]

Adds a known namespace.

Parameters:
nspace The namespace to be added (as module name).
alias If not empty, will be the alias under which the module will be locally known.
full If true, import all symbols.
filename If true, the load request was for a direct filename.

Reimplemented in Falcon::InteractiveCompiler.

void Falcon::Compiler::addNilConstant ( const String name,
uint32  line = 0 
)

void Falcon::Compiler::addNumConstant ( const String name,
numeric  value,
uint32  line = 0 
)

void Falcon::Compiler::addPredefs (  )  [protected]

Add predefined symbols and constants.

This method prepares the compiler so that it has basic constant symbols set and in place; embeding apps may wish to provide different base symbols.

void Falcon::Compiler::addStatement ( Statement stm  )  [inline]

String* Falcon::Compiler::addString ( const String str  )  [inline]

void Falcon::Compiler::addStringConstant ( const String name,
const String value,
uint32  line = 0 
)

void Falcon::Compiler::addSymdef ( Value val  )  [inline]

StmtFunction* Falcon::Compiler::buildCtorFor ( StmtClass sym  ) 

Builds the constructor function for a given class.

This is an utility that creates a ._init suffixed function statement and symbol; the symbol is added as the constructor for the class stored in the parameter, while the StmtFunction object is inserted in the functions syntax tree and returned.

Parameters:
sym a symbol containing a ClassDef for which a constructor function must be built.
Returns:
A newly created StmtFunction object to hold the source tree for the constructor.

bool Falcon::Compiler::checkLocalUndefined (  ) 

Checks if the current statemsnt has referenced a locally undefined symbol.

void Falcon::Compiler::clear (  )  [protected]

Removes all the structures and temporary data used to compile a file.

This function is called automatically by the various compile() and destructors.

Value* Falcon::Compiler::closeClosure (  ) 

Closes the currently worked on closure.

void Falcon::Compiler::closeFunction (  ) 

Cleanup for function closing.

bool Falcon::Compiler::compile ( Module mod,
Stream input 
)

Compile a module from a stream.

This version of the function is suitable to be used multiple times for the same compiler. The caller should call resetDefaults(), give the compiler the wished setings and then call compiler.

Parameters:
mod a newly allocated and empty module that will be filled by the compilation
input the stream from which to read the source
Returns:
false on compilation failed.

bool Falcon::Compiler::compile (  ) 

Compiles the module given in the constructor.

This method is to be used for one-time only compilation (build the compiler, compile, destroy the compiler), when the Compiler( Module *, Stream *) constructor version has been used.

Otherwise, it will raise an error and exit.

void Falcon::Compiler::decClosureContext (  )  [inline]

bool Falcon::Compiler::defContext (  )  const [inline]

void Falcon::Compiler::defContext ( bool  ctx  )  [inline]

void Falcon::Compiler::defineVal ( ArrayDecl val  ) 

Define all the values int the given array definition.

As the array definition is to the left of an assignment, all the atomic symbols that are found in the array definition are to be defined. Of course, non atomic symbols (as functions, other array defintions and so on) are NOT to be defined.

Parameters:
val The array of left-assigment values, possibly symbols

void Falcon::Compiler::defineVal ( Value val  ) 

Process an include instruction.

In Falcon, include is a compile time instruction more than a directive. Falcon does not support pre-processing or directives by design. The include statement takes as argument a single immediate string or an expression that can be statically evaluated into a string (i.e. consts or string sums), and includes it by changing the internal file stream. This causes an as-is inclusion at lexer level. Instruct the compiler that this value is used as a definition. This is used to turn local undefined into local variables, or global undefinded into globals. Variables may rightfully become something else later on (i.e. functions) however.

Parameters:
val The value to be inspected in search for defined symbols.

Error* Falcon::Compiler::detachErrors (  )  [inline]

Passes the ownership of the error structure to the caller.

This allows the caller to get the errors received during the processing of the last compilation and handle them separately.

The ownership is passed to the caller, or in other world, the list of errors in this compiler is zeroed and the reference count of the returned error list is not changed.

Returns:
0 if the compiler was not in error state, or a list of one or more errors if it raised some errors during the processing of the files.

int Falcon::Compiler::errors (  )  const [inline]

const Value* Falcon::Compiler::getConstant ( const String name  )  [inline]

Seek a constant in the predefined constant list.

If the constant is found, the function returns the value associated with the given constant. Constant values are owned by the compiler (yet the constant strings are still held in the module), and are destroyed at compiler destruction.

Note:
just a placeholder for now
Parameters:
name the constant to be searched
Returns:
the value of the constant or 0 if the constant doesn't exists.

Statement* Falcon::Compiler::getContext (  )  const [inline]

StatementList* Falcon::Compiler::getContextSet (  )  const [inline]

FuncDef* Falcon::Compiler::getFunction (  )  const [inline]

StmtFunction* Falcon::Compiler::getFunctionContext (  )  const [inline]

Statement* Falcon::Compiler::getLoop (  )  const [inline]

Symbol* Falcon::Compiler::globalize ( const String str  ) 

virtual Symbol* Falcon::Compiler::importAlias ( const String symName,
const String fromMod,
const String alias,
bool  filename = false 
) [virtual]

Import a single aliased symbol from a module.

This tries to resolve the given symbol in the given symName in the target from module during link time, and assings the local alias to it

Parameters:
symName The symbol name to be aliased.
fromMod The module name or path.
alias The namespace. If not given, will be the name of the module.
filename if true the module load request is for a direct file name.
return The newly added symbol.

virtual void Falcon::Compiler::importSymbols ( List lst,
const String prefix = 0,
const String alias = 0,
bool  filename = false 
) [virtual]

Import the symbols named in a List.

The lst parameter contains a list of String* which will be imported. If a prefix is given, then the prefix is added to the list of known namespaces, and it is added in front of the symbol to be imported.

This will force the VM to search the symbol only in the specified namespace (that is, in the module named after the prefix).

The function is meant to be used in the conjunction with the parser, and will destroy both the string in lst and lst itself.

Parameters:
lst The list of symbols (disposeable strings) to be loaded
prefix The module name or path
alias The namespace. If not given, will be the name of the module.
filename if true the module load request is for a direct file name

void Falcon::Compiler::incClosureContext (  )  [inline]

void Falcon::Compiler::incLambdaCount (  )  [inline]

void Falcon::Compiler::init (  )  [protected]

Initializes structures and variables used for compilation.

This function is called automatically by the various compile() and destructors.

bool Falcon::Compiler::isInteractive (  )  const [inline]

Checks if this compiler is as an interactive I/O.

bool Falcon::Compiler::isLocalContext (  )  [inline]

bool Falcon::Compiler::isNamespace ( const String symName  ) 

Return true if the current symbol is actually a namespace.

Checks if sym was previosly declared as a namespace with addNamspace().

Note:
Namespaces can contain dots.
Parameters:
symName the name that may be possibly a namespace.

int Falcon::Compiler::lambdaCount (  )  const [inline]

SrcLexer* Falcon::Compiler::lexer (  )  const [inline]

void Falcon::Compiler::metaCompile ( const String data,
int  startline 
)

Performs a meta compilation.

If any data is written on the metacompiler output stream, the stream is immediately sent to the lexer for further compilation.

Module* Falcon::Compiler::module (  )  const [inline]

void Falcon::Compiler::parsingFtd ( bool  b  ) 

bool Falcon::Compiler::parsingFtd (  )  const

Are we parsing a normal file or an escaped template file?

void Falcon::Compiler::popContext (  )  [inline]

void Falcon::Compiler::popContextSet (  )  [inline]

void Falcon::Compiler::popFunction (  ) 

void Falcon::Compiler::popFunctionContext (  )  [inline]

void Falcon::Compiler::popLoop (  )  [inline]

void Falcon::Compiler::pushContext ( Statement stm  )  [inline]

void Falcon::Compiler::pushContextSet ( StatementList list  )  [inline]

void Falcon::Compiler::pushFunction ( FuncDef f  ) 

void Falcon::Compiler::pushFunctionContext ( StmtFunction func  )  [inline]

void Falcon::Compiler::pushLoop ( Statement stm  )  [inline]

void Falcon::Compiler::raiseContextError ( int  code,
int  line,
int  startLine 
)

Raises an error related to a context problem.

The error reports the line where the problem has been detected, and the line that begun current faulty context.

Parameters:
code the error code.
line the line where the error is detected
startLine initial line of the context.

void Falcon::Compiler::raiseError ( int  errorNum,
const String errorp,
int  errorLine = 0 
)

void Falcon::Compiler::raiseError ( Error e  ) 

Raises an error.

The compiler doesn't throw the error list until the compilation is over. error raisal is delayed until the end of the compilation step.

void Falcon::Compiler::raiseError ( int  errorNum,
int  errorLine = 0 
)

Front-end to raiseError( *e ).

The compiler doesn't throw the error list until the compilation is over. error raisal is delayed until the end of the compilation step.

void Falcon::Compiler::reset (  ) 

Reset compiler settings to defaults and prepares for a new compilation.

Precisely, this function: # destroys tree and function information from previous run, if they exist. # clears the constants and fills them with the Falcon language default constants # clears the ftd compilation flag.

This function should be called before a repeated compilation; then the caller is free to add specific application constants and setting, and finally call the compile( Module *, Stream *) method.

Directives are automatically cleared at the end of a compilation, and they keep the value they had before. This allows to set directives from outside and have scripts locally modify their directives.

void Falcon::Compiler::resetEnum (  )  [inline]

Symbol* Falcon::Compiler::searchGlobalSymbol ( const String symname  ) 

Symbol* Falcon::Compiler::searchLocalSymbol ( const String symname,
bool  bRecurse = false 
)

Searches a symbol in the local context.

If the current context is the global context, then it just calls searchGlobalSymbol. If there is a local context, the symbol is serched in the current context; in case it's not found, this method returns if bRecurse is false.

If bRecurse is true, the symbol is searched down in the parent contexts until found or until there is no more local context. In that case, the method returns 0; so if there is the need to bind a local symbol or a global one if local symbols are not found, this must be done by the caller calling searchGlobalSymbol when this method returns 0.

Parameters:
symname The symbol name as a pointer to a module string.
bRecurse true to bind symbols from any parent, false to search in the local context.
Returns:
The symbol if found or 0 otherwise.

Symbol* Falcon::Compiler::searchOuterSymbol ( const String symname  ) 

void Falcon::Compiler::searchPath ( const String path  )  [inline]

Sets the compiler-specific search path.

This search path is used to drive module loading in macro compiler.

const String& Falcon::Compiler::searchPath (  )  const [inline]

Return the search path inherited from upper facilities.

This search path is used to drive module loading in macro compiler.

void Falcon::Compiler::serviceLoader ( ModuleLoader l  )  [inline]

Sets the service VM for this compiler.

Used to create the associate meta-compiler. The loader is owned by this compiler (or of its meta-compiler).

If a metacompilation is required, the ownership of this loader is transferred to the meta compiler. If a service loader is not set and a meta-compilation is requried, the compiler creates a loader on the fly.

ModuleLoader* Falcon::Compiler::serviceLoader (  )  const [inline]

Gets the service VM for this compiler.

Used to create the associate meta-compiler. The loader is owned by this compiler (or of its meta-compiler).

void Falcon::Compiler::serviceVM ( VMachine vm  )  [inline]

Sets the service VM for this compiler.

Used to create the associate meta-compiler. The VM is owned by this compiler (or of its meta-compiler).

If a metacompilation is required, the ownership of this VM is transferred to the meta compiler. If a service VM is not set and a meta-compilation is requried, the compiler creates a VM on the fly.

VMachine* Falcon::Compiler::serviceVM (  )  const [inline]

Gets the service VM for this compiler.

Used to create the associate meta-compiler. The VM is owned by this compiler (or of its meta-compiler).

bool Falcon::Compiler::setDirective ( const String directive,
int64  value,
bool  bRaise = true 
)

Set directive as string value.

In case the directive doesn't exist or doesnt accept the given value as valid, an error may be raised. Applications setting directives externally may give bRaise false to prevent error raising and manage internally directive set failure.

Parameters:
directive the name of the directive to be set.
value the value that the given directive should be given.
bRaise true in case of invalid directive or value, also raise an error
Returns:
true on success, false on failure

bool Falcon::Compiler::setDirective ( const String directive,
const String value,
bool  bRaise = true 
)

Set directive as string value.

In case the directive doesn't exist or doesnt accept the given value as valid, an error may be raised. Applications setting directives externally may give bRaise false to prevent error raising and manage internally directive set failure.

Parameters:
directive the name of the directive to be set.
value the value that the given directive should be given.
bRaise true in case of invalid directive or value, also raise an error
Returns:
true on success, false on failure

void Falcon::Compiler::setInteractive ( bool  bint  )  [inline]

Sets this compiler as interactive.

A compiler meant to run code from the command line has different rules; in example, it can accept autoexpressions without raising a "statement does nothing" error, as the meaning of expressions on the command line is just that to be evaluated and their result being interactively displayed.

SourceTree* Falcon::Compiler::sourceTree (  )  const [inline]

void Falcon::Compiler::staticPrefix ( const String v  )  [inline]

const String* Falcon::Compiler::staticPrefix (  )  const [inline]

Return the current static prefix, if any.

Zero shall be returned if the current symbol is not currently using the static prefix.

Stream* Falcon::Compiler::stream (  )  const [inline]

bool Falcon::Compiler::strictMode (  )  const [inline]

void Falcon::Compiler::strictMode ( bool  breq  )  [inline]

Activate "strict" feature.

When turned on, the compilre will raise an undefined symbol when assigning this values outside a "def" statement.

int Falcon::Compiler::tempLine (  )  const [inline]

void Falcon::Compiler::tempLine ( int  line  )  [inline]

Store current line for later error signaling.


Member Data Documentation

Aliased symbols are stored here.

List of alias maps.

Map of constants.

(String &, Value *)

Leading instruction that owns currently parsed statements.

Area to save currently parsed statements.

int Falcon::Compiler::m_errors [protected]

Context limited to leading function instructions.

Stack of currently active functions.

Can be nested in case of i.e. lambas. (FuncDef *)

Directive language.

Leading instruction, specialized for loops.

This is the module that is being formed in the meanwhile.

Directive version.

Map of namespaces.

(String &, void)

Search path inherited from upper facilities.

Last statement's symbols.

The type of symbols cannot be determined by the context while they are built; they get defined as the compiler understands the surrounding context. However, it is an error to reference undefined symbols in local context (i.e. lambdas, functions etc.). This list has a reference to each symbol that has been built during the last statement parsing. At the end of the statement, the list is scanned for undefined symbol, and an error is risen in case any is found. list of Value *

The static prefix is the name of the symbol currently declaring the static namespace.

Do not delete: we're not owners.

bool Falcon::Compiler::m_strict [protected]

Directive strict.


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

Generated on Mon Oct 19 10:11:32 2009 for Falcon_Core by  doxygen 1.5.8