The Framework

This library defines a simple framework that can be used to extend the behavior of this library. There are four fundamental types that are used: IParser, ICodeCompiler, ILuaRuntime, and ILuaEnvironment. Any of these objects can be replaced either by extending the default classes or by defining your own types. The environment holds the globals table and an instance of each of the other types. The parser parses plain-text code into a code tree. The compiler converts the code into an executable format and the runtime defines how the code executes. See also Extending the Framework.

The Environment

The environment is the main type and defines how the framework will behave. This object simply contains a reference to the global table and one of the other three types. It also contains an IThreadFactory object that will create LuaThread objects. This type does little actual work and simply contains each of the other types. This framework assumes that any property will never return a null value and it will be a valid reference. The default implementation is LuaEnvironment and can be extended to alter its behavior.

The Parser

The parser is in charge of converting input code into a DOM (Document Object Model) tree. This tree is represented as a tree of IParseItem objects. Each item is defined in ModMaker.Lua.Parser.Items. There is also an IParseItemVisitor that can be used with the visitor pattern to traverse the tree. The only method defined is the one that will parse the code. It can throw an exception if there is a syntax error or if there is an invalid argument. If the method returns, it must return a valid IParseItem tree. This object can also optionally use a cache to reduce the time it takes to parse the same code twice. The default implementation is PlainParser and can be extended to alter its behavior.

The Compiler

The compiler is in charge of converting an IParseItem tree into an invokable object (IMethod). The exact object that is returned is not important; however calling invoke on the object should execute the given code. There is a helper visitor object GetInfoVisitor that will traverse a tree and get information about it and store it in the objects. It is important to use this so labels and goto's are resolved. Also, this will get information about captured variables. This object also creates delegate objects from an IMethod object. This is used for dynamic binding so IMethod objects can be stored as delegates. The default implementation is CodeCompiler and cannot be extended.

The Runtime

The runtime defines how the code executes. The methods in the runtime are invoked by the generated code when certain operations are performed. Most of the complicated logic is performed by the runtime allowing users to alter the behavior of Lua code by changing the runtime object. The default runtime is LuaRuntime and can be extended to alter its behavior.

Last edited May 26, 2013 at 4:07 AM by ModMaker, version 1


No comments yet.