Rudiments
|
Inherits object.
Public Member Functions | |
compiler () | |
virtual | ~compiler () |
void | appendModulePath (const char *path) |
void | clearModulePaths () |
linkedlist< char * > * | getModulePaths () |
void | appendPreProcessor (compilerpreprocessor *module) |
bool | appendPreProcessor (const char *filename, const char *classname, const char *id, domnode *parameters) |
bool | appendPreProcessor (const char *filename, const char *classname, const char *id, const char *parameters) |
void | clearPreProcessors () |
linkedlist< compilermodule * > * | getPreProcessors () |
void | appendProcessor (compilerprocessor *module) |
bool | appendProcessor (const char *filename, const char *classname, const char *id, domnode *parameters) |
bool | appendProcessor (const char *filename, const char *classname, const char *id, const char *parameters) |
void | clearProcessors () |
linkedlist< compilermodule * > * | getProcessors () |
void | appendPostProcessor (compilerpostprocessor *module) |
bool | appendPostProcessor (const char *filename, const char *classname, const char *id, domnode *parameters) |
bool | appendPostProcessor (const char *filename, const char *classname, const char *id, const char *parameters) |
void | clearPostProcessors () |
linkedlist< compilermodule * > * | getPostProcessors () |
bool | setInputGrammar (const char *grammar, const char *startsymbol) |
bool | setOutputGrammar (const char *grammar) |
void | setMetaData (domnode *metadata) |
bool | compile (const char *input, stringbuffer *output) |
bool | process (domnode *root) |
const char * | getError () |
void | setDebugLevel (uint8_t debuglevel) |
Public Member Functions inherited from object | |
virtual | ~object () |
The compiler class provides a generic compiler framework.
Though targeted at code-to-code translation (transcompiling), it could be used as the basis for any type of translation or compilation system.
Translation/compilation is achieved in three steps:
In the pre-processing step, a unit of code and optional metadata is passed through a chain of pre-processing modules. The output of each module is fed to the next as input.
In the processing step, the output of the final preprocessing module is parsed by an instance of the codetree class, using the provided input grammar, producing an dom tree which represents the code. The tree and optional metadata is then passed through a chain of processing modules which convert the tree from representing one type of code to a tree representing another type of code. The output of the final processing module is converted back to code, using the provided output grammar.
In the post-processing step, the output of the processing step is passed through a chain of post-processing modules. The output of each module is fed to the next as input. Ultimately a unit of compiled code is produced.
Pre-processing modules should inherit from the compilerpreprocessor class. Processing modules should inherit from the compilerprocessor class. Post-processing modules should inherit from the compilerpostprocessor class. All modules must implement the process() method.
The pre and post-processing modules directly manipulate code and might make heavy use of the file, charstring/bytestring and stringbuffer/bytebuffer classes. The processing modules manipulate an dom tree representing code and would likely make heavy use of the domnode class.
The input and output code may be text or binary. The framework is geared toward text processing, but could be used to process binary data as well.
See the codetree class for information on the grammars used to define the input and output formats.
compiler::compiler | ( | ) |
Creates a new instance of the compiler class.
|
virtual |
Deletes this instance of the compiler class.
void compiler::appendModulePath | ( | const char * | path | ) |
Appends "path" to the list of paths that will be searched for pre-processor, processor and post-processor modules.
void compiler::appendPostProcessor | ( | compilerpostprocessor * | module | ) |
Appends postprocessor "module" to the list of postprocessors.
bool compiler::appendPostProcessor | ( | const char * | filename, |
const char * | classname, | ||
const char * | id, | ||
const char * | parameters | ||
) |
Dynamically loads postprocessor module "filename". Calls function new_"classname" to create an instance of the postprocessor class. Sets its "id" and "parameters". Appends the instance to the list of postprocessors. Returns true on success and false on failure.
bool compiler::appendPostProcessor | ( | const char * | filename, |
const char * | classname, | ||
const char * | id, | ||
domnode * | parameters | ||
) |
Dynamically loads postprocessor module "filename". Calls function new_"classname" to create an instance of the postprocessor class. Sets its "id" and "parameters". Appends the instance to the list of postprocessors. Returns true on success and false on failure.
void compiler::appendPreProcessor | ( | compilerpreprocessor * | module | ) |
Appends preprocessor "module" to the list of preprocessors.
bool compiler::appendPreProcessor | ( | const char * | filename, |
const char * | classname, | ||
const char * | id, | ||
const char * | parameters | ||
) |
Dynamically loads preprocessor module "filename". Calls function new_"classname" to create an instance of the preprocessor class. Sets its "id" and "parameters". Appends the instance to the list of preprocessors. Returns true on success and false on failure.
bool compiler::appendPreProcessor | ( | const char * | filename, |
const char * | classname, | ||
const char * | id, | ||
domnode * | parameters | ||
) |
Dynamically loads preprocessor module "filename". Calls function new_"classname" to create an instance of the preprocessor class. Sets its "id" and "parameters". Appends the instance to the list of preprocessors. Returns true on success and false on failure.
void compiler::appendProcessor | ( | compilerprocessor * | module | ) |
Appends processor "module" to the list of processors.
bool compiler::appendProcessor | ( | const char * | filename, |
const char * | classname, | ||
const char * | id, | ||
const char * | parameters | ||
) |
Dynamically loads processor module "filename". Calls function new_"classname" to create an instance of the processor class. Sets its "id" and "parameters". Appends the instance to the list of processors. Returns true on success and false on failure.
bool compiler::appendProcessor | ( | const char * | filename, |
const char * | classname, | ||
const char * | id, | ||
domnode * | parameters | ||
) |
Dynamically loads processor module "filename". Calls function new_"classname" to create an instance of the processor class. Sets its "id" and "parameters". Appends the instance to the list of processors. Returns true on success and false on failure.
void compiler::clearModulePaths | ( | ) |
Removes all currently loaded module paths.
void compiler::clearPostProcessors | ( | ) |
Removes all currently loaded postprocessor modules.
void compiler::clearPreProcessors | ( | ) |
Removes all currently loaded preprocessor modules.
void compiler::clearProcessors | ( | ) |
Removes all currently loaded processor modules.
bool compiler::compile | ( | const char * | input, |
stringbuffer * | output | ||
) |
Passes "input" through the preprocessors, input grammar, processors, output grammar, and postprocessors.
If "output" is specified then the output is written to that buffer.
Neither "input" nor "output" need necessarily be supplied.
Alternatively, preprocessing modules could load a file or files, the locations of which are known or supplied in the metadata, and the postprocessing modules could write output to a file or files, the locations of which may be known by the calling process or returned in the metadata.
Returns true on success and false on failure.
const char* compiler::getError | ( | ) |
Returns the error that occurred if compile() fails.
linkedlist<char *>* compiler::getModulePaths | ( | ) |
Returns the list of loaded module paths.
linkedlist<compilermodule *>* compiler::getPostProcessors | ( | ) |
Returns the list of loaded postprocessor modules.
linkedlist<compilermodule *>* compiler::getPreProcessors | ( | ) |
Returns the list of loaded preprocessor modules.
linkedlist<compilermodule *>* compiler::getProcessors | ( | ) |
Returns the list of loaded processor modules.
Passes already-parsed tree "root" through the set of loaded processor modules.
No pre-processing, parsing, post-processing, or writing back out is done.
This is useful for nesting sub-compilers inside of processor modules but could be used for other purposes as well.
Returns true on success and false on failure.
void compiler::setDebugLevel | ( | uint8_t | debuglevel | ) |
Sets the debug level. Debug is written to standard out.
bool compiler::setInputGrammar | ( | const char * | grammar, |
const char * | startsymbol | ||
) |
Sets the input grammar and start symbol. Returns true on success and false if "grammar" is not a valid grammar.
void compiler::setMetaData | ( | domnode * | metadata | ) |
Sets the compilation metadata.
The compilation metadata will be passed into every module and may be examined and/or altered by any of them.
The metadata may contain any information the user wishes to provide to or collect from the modules.
For example, during input, it may contain the names and locations of source code files, headers or other support files, search paths, database access information, etc.
When the compilation is complete, it may contain the locations of files that were created by the process.
bool compiler::setOutputGrammar | ( | const char * | grammar | ) |
Sets the output grammar. Returns true on success and false if "grammar" is not a valid grammar.