Skip to end of metadata
Go to start of metadata

This is actually relatively simple; we only need a few of the Boo.Lang.Compiler classes to make it happen.

The classes most important to us are:

  • Boo.Lang.Compiler.BooCompiler - the class that handles compilation of Boo code.
  • Boo.Lang.Compiler.Pipelines.CompileToMemory - a compiler pipeline that compiles Boo code into memory instead of a file output. You'll want this for most scripting situations.

Using these classes and reflection, we will be able compile Boo scripts to memory and run'em on inputted variables.

Suppose script.boo is a class that the user has written, and you want to consume it by your application without the user having to dance with booc.exe or some other fancy pants method.

script.boo

Calling scripting Boo from Boo is ridiculously easy--too easy to even explain, so here is the commented code.

runBoo.boo

That was painless, wasn't it? As you're imaging, you can instanize classes and call instance methods via the convience of duck typing in a similar fashion. Consider the code-block below.

That's it. Use myInstance as you would any other class, sans code completion (warning)

This method, of course, is not as interesting as using another language entirely to invoke our Boo script.
Let's see how we do it in C#:

runBoo.cs

I compiled the above runBoo.cs script with this command (you can change csc to gmcs if you use mono):

If you are on Windows, make sure you specify the paths to the boo dlls. After compiling, either move runBoo.exe into the same folder as the boo dlls or copy the dlls to the same folder as your exe. Then run the exe, making sure "script.boo" is in the same folder as your current directory:

As you can see the C# variant is a bit more verbose, but that's your bag, let it roll.

Here are the highlights of the C# version.

ScriptModule is the name of the class encapsulating the main method and the stringManip method of the compiled "script.boo" It is a uniquely generated name determined by the file-name plus a "Module" postfix.

This block of code invokes the stringManip method with one parameter: "Tag." The first parameter, which is normally an instance of a class, is null because the stringManip method is static and thus requires no instance method of ScriptModule.

It returns back the output of the stringManip method.

  • No labels