[IPython-dev] How to parse a domain-specific language on top of Python

Matthias BUSSONNIER bussonniermatthias at gmail.com
Wed Dec 3 12:26:04 EST 2014


Le 3 déc. 2014 à 17:55, Joachim Wuttke a écrit :

> Thank you, Doug!
> 
> Could you possibly give me a hint what you mean by "magic" under
> (2) and (3) ?

In both cases you need <your language>  to python converter. 
Magics  are a short syntactic sugar that allow you to transforme a string and do whatever. 
One of the use is to integrate with other language. 

Tipycally you can use 

%%ruby
puts 'hi' from ruby

(this one will call ruby in a subshell) 

or 

%%hy
(print "Hi from Hy")

And it will call hy for you (second one has integration with python)

Cf https://github.com/rossant/ipycache/blob/master/ipycache.py#L249-L289 for example as how to write a magic.

Nonetheless IPython itself does not provide anything to parse your language. 
So in the end what you want is write some (python) code that take a string as output and emit some python, 
after deciding in between a kernel and a maid is a detail.

Depending on your complexity you can try to produce either python code as string. 
Or do as Hy does and create an AST directly. I think there is an interesting talk as 
Pycon 2014 that I've some pointer on how they did this.

I'm sure this does not completely  help. 

Looking forward to et some news though, 
-- 
M

> 
> - Joachim
> 
>> 2) You could do this as a magic. You could use a simple parser to just
>> output regular Python. This is the least complex, and should be easier
>> to keep working over time.
>> 
>> 3) You could implement your own parser written in Python as its own
>> kernel (google "wrapper kernel"). You can even have your own magics etc.
>> (see https://github.com/blink1073/metakernel)
> 
> 
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20141203/7d749faf/attachment.html>


More information about the IPython-dev mailing list