Hello,<br><br>I apologize if this has been answered elsewhere, but I am unable to find anything up to date, or that covers my question in particular.<br><br>The short version is: Can I modify the AST of a parsed file before compiling/executing in an embedded context? I want to allow simple, Excel-like statements to be executed from a .NET application. One major hitch is that Excel uses ^ for power whereas Python uses **. Ideally, I would be able to catch calls to ^ and replace with ** at compile time.<br>
<br>If this is just not possible without rebuilding IronPython, do let me know.<br><br>I have gotten as far as the below, although the BinaryExpression node's Operator is only gettable. I also am unsure how to take an AST and compile it, or if that is even public/allowed.<br>
<br> var engine = Python.CreateEngine();<br> var s = HostingHelpers.GetSourceUnit(engine.CreateScriptSourceFromString("3^4"));<br> var cc = new CompilerContext(s, new PythonCompilerOptions(), ErrorSink.Default);<br>
var p = Parser.CreateParser(cc, new PythonOptions());<br> PythonAst ast = p.ParseFile(false);<br><br> // I originally tried this with a PythonWalker, but this is more succinct for the purpose of this example<br>
SuiteStatement body = (SuiteStatement)ast.Body;<br> ExpressionStatement st = (ExpressionStatement)body.Statements[0];<br> BinaryExpression exp = (BinaryExpression) st.Expression;<br> //exp.Operator = PythonOperator.Power; // Were it only so easy...<br>
<br><br>Thanks for reading!<br>