Skip to content

More with the ScriptEngine class

Exporting the AST to XML

You can easily export an Abstract Syntax Tree to XML format by invoking the ExportXml static method of the ScriptEngine class.

Simply do as follows:

C#
1
2
3
4
var program = ScriptEngine.ParseString(txtScript.Text);
// Alternatively: var program = ScriptEngine.ParseFile(@"path/to/my/script");
ScriptEngine.ExportXml(program, @"C:\myScript.xml");
// Alternatively: ScriptEngine.ExportXml(program, someStream);

That functionality is specially helpful for debugging purpose. You can use it to ensure that the parsed script has the expected logical structure.

Regenerating the source code

The ScriptEngine class has a GenerateCode static method than can be used to regenerate the source of a script given its AST (as an instance of the AddyScript.Ast.Program class). The generated source code is so nicely formatted than the "Reformat code" functionality of the AddyScript Graphical Editor (asgui) fully relies on the ScriptEngine.GenerateCode method.

Here is an example of how to use it:

C#
var program = ScriptEngine.ParseString(txtScript.Text);
txtScript.Text = ScriptEngine.GenerateCode(program);