[Ironpython-users] Modifying ASTs when embedding IronPython

Tuomas Utrecht tuomas.utrecht at gmail.com
Fri Mar 2 14:31:01 CET 2012

Le 1 mars 2012 21:58, Curt Hagenlocher <curt at hagenlocher.org> a écrit :

> I would argue that this is a bad idea. How Python-compatible do you want
> this simple Excel-like language to be?

I have body of possible Excel formulas which are identical to Python
syntax, except for the use of ^.

> If it's really just a small subset of the full Python language, you may be
> better off writing a simple parser that emits Python text as its back end
> and prevents the users from doing anything more complicated.

At the point where I can generate an AST on my own, I may be better off not
even using Ipy. I'm hoping to leverage the work already done in this area.
The upside to Python is the introduction of new syntax/functionality, but
supporting the existing formulas is paramount.

> This should not be significantly more complex than what you propose to do.
> If you really want to offer the full power of the Python language, then you
> should consider whether you are doing your users a disservice by teaching
> them a language that's almost the same as Python, but different in just one
> minor and hard-to-debug respect.

As you know, it is difficult to encourage even programmers to learn new
programming languages. :)

The use case here is the ability to paste something from Excel into a C#
application for it to be run. Launching Excel from the application is an
obvious but undesirable solution due to some third-party plug-in
limitations, among other reasons.

> On Thu, Mar 1, 2012 at 12:31 PM, Tuomas Utrecht <tuomas.utrecht at gmail.com>wrote:
>> Hello,
>> 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.
>> 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.
>> If this is just not possible without rebuilding IronPython, do let me
>> know.
>> 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.
>>     var engine = Python.CreateEngine();
>>     var s =
>> HostingHelpers.GetSourceUnit(engine.CreateScriptSourceFromString("3^4"));
>>     var cc = new CompilerContext(s, new PythonCompilerOptions(),
>> ErrorSink.Default);
>>     var p = Parser.CreateParser(cc, new PythonOptions());
>>     PythonAst ast = p.ParseFile(false);
>>     // I originally tried this with a PythonWalker, but this is more
>> succinct for the purpose of this example
>>     SuiteStatement body = (SuiteStatement)ast.Body;
>>     ExpressionStatement st = (ExpressionStatement)body.Statements[0];
>>     BinaryExpression exp = (BinaryExpression) st.Expression;
>>     //exp.Operator = PythonOperator.Power; // Were it only so easy...
>> Thanks for reading!
>> _______________________________________________
>> Ironpython-users mailing list
>> Ironpython-users at python.org
>> http://mail.python.org/mailman/listinfo/ironpython-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20120302/37ded336/attachment.html>

More information about the Ironpython-users mailing list