IronPython's internal ASTs also happen to be Expression trees, so you just need to get the AST for your code, which you can do using the IronPython.Compiler.Parser
class. The Parser.ParseFile method will return a IronPython.Compiler.Ast.PythonAst
instance representing the code.
Using the parser is a bit tricky, but you can look at the BuildAst
method of the _ast module for some hints. Basically, it's:
Parser parser = Parser.CreateParser(
new CompilerContext(sourceUnit, opts, ThrowingErrorSink.Default),
(PythonOptions)context.LanguageContext.Options);
PythonAst ast = parser.ParseFile(true);
ThrowingErrorSink
also comes from the _ast
module. You can get a SourceUnit
instance like so (c.f. compile
builtin):
SourceUnit sourceUnit = context.LanguageContext.CreateSnippet(source, filename, SourceCodeKind.Statements);
You then have to walk the AST to get useful information out of it, but they should be similar (but not identical to) C# expression trees.