Add a Custom Compiler

Documentation wrote using version 1.2

If you have wrote your own custom compiler, or code provider, you may easily add it to the rScript Engine via the ICompiler interface that is included with the engine. To add your compiler, you will first need to add a reference to your compiler within you project.
For this example, we will use the Visual Basic compiler as an example. It is a compiler included with the .NET language, however you would implement your own compiler in the same manor.
The Visual Basic compiler wrapper we will create will be added to the engine as a supported compiler via a new class that implements the ICompiler interface. The Interface requires you to provide the following Properties for the compiler wrapper.
  • System.CodeDom.Compiler.CompilerResult Result {get;set;}
    • Once compiling is completed, this will hold the results regarding the compilation. Things such as the fully compiled assembly, any error messages and the path to the compiled assembly.
  • string ScriptExtension {get;set;}
    • The file extension of your script files. You can preceed the file extension by a period or not, the engine will automatically take care of it.
    • Example:
  ScriptExtension = "vb";


is the same as
  ScriptExtension = ".vb";

The engine will know if you are missing the period and will automatically insert one for you.
  • List<string> AssemblyReference {get;set;}
    • The collection of assemblies that you want to reference. By default the engine only references System.dll, System.Core.dll and mscorlib.dll.
  • System.Collections.Generic.Dictionary<string,string> CompilerOptions {get;set;}
    • Compiler Options that can be passed to your custom compiler if it uses the standard CompilerOptions Dictionary approach.
  • bool Compile(System.CodeDom.Compiler.CompilerParameters param, string scriptRepository);
  • bool Compile(System.CodeDom.Compiler.CompilerParameters param, string[] scriptSourceCode);
  • bool Compile(System.CodeDom.Compiler.CompilerParameters param, System.IO.FileInfo scriptFile);
    • The actual Compile method with compiler parameters that will be supplied by the rScripting.CompileEngine.
    • The CompileEngine will expose the parameters as properties to the developers. Your compiler wrapper will need to support all of the parameters supplied within the CompilerParameters Type.

Finally, the following shows you how to wrap your compiler with the ICompiler interface.

using System;
using System.Collections.Generic;

namespace RuntimeExample
{
    /// <summary>
    /// The SampleVBCompiler Type is nothing more than a wrapper for the VBCodeProvider Type.
    /// The point is the demonstrate how developers can write their own .NET compiler and then wrap
    /// the compiler within a rScripting.ICompiler interface to provide instant scripting support for
    /// their custom compiler.
    /// </summary>
    public class SampleVBCompiler : rScripting.ICompiler
    {
        /// <summary>
        /// The file extension used for the script files.
        /// </summary>
        public String ScriptExtension { get; set; }

        /// <summary>
        /// Provides a collection of Assemblies that the compiler will add to its reference list.
        /// </summary>
        public List<String> AssemblyReferences { get; set; }

        /// <summary>
        /// Provides compiling options to various compilers, if they support this feature.
        /// </summary>
        public Dictionary<String, String> CompilerOptions { get; set; }

        public System.CodeDom.Compiler.CompilerResults Results { get; set; }

        public Boolean Compile(System.CodeDom.Compiler.CompilerParameters param, string scriptRepository)
        {
            Microsoft.VisualBasic.VBCodeProvider code = new Microsoft.VisualBasic.VBCodeProvider();

            //Compile all of the files found in the script repository directory
            Results = code.CompileAssemblyFromFile(param, System.IO.Directory.GetFiles(scriptRepository));

            if (Results.Errors.HasErrors)
                return false;
            else
                return true;
        }

        public Boolean Compile(System.CodeDom.Compiler.CompilerParameters param, string[] scriptSourceCode)
        {
            Microsoft.VisualBasic.VBCodeProvider code = new Microsoft.VisualBasic.VBCodeProvider();

            //Compile from source code.
            Results = code.CompileAssemblyFromSource(param, scriptSourceCode);

            if (Results.Errors.HasErrors)
                return false;
            else
                return true;
        }

        public Boolean Compile(System.CodeDom.Compiler.CompilerParameters param, System.IO.FileInfo scriptFile)
        {
            Microsoft.VisualBasic.VBCodeProvider code = new Microsoft.VisualBasic.VBCodeProvider();

            //Compile the script file.
            Results = code.CompileAssemblyFromFile(param, new String[] { scriptFile.FullName });

            if (Results.Errors.HasErrors)
                return false;
            else
                return true;
        }
    }
}

Last edited Dec 12, 2010 at 2:43 PM by Scionwest, version 2

Comments

No comments yet.