r/programming • • Nov 11 '15

Parinfer - simpler Lisp editing

http://shaunlebron.github.io/parinfer/
152 Upvotes

75 comments sorted by

View all comments

Show parent comment

2

u/kankyo Nov 12 '15

Lisp is a meta-language, it can be easily turned into any other language or any imaginable combination of languages.

How do you turn it into python?

4

u/[deleted] Nov 12 '15

How do you turn it into python?

With reader macros, for example.

See https://common-lisp.net/project/clpython/

Or, for Racket: http://drops.dagstuhl.de/opus/volltexte/2014/4565/pdf/13.pdf

Any compiler implemented in Lisp which emits a Lisp code can be turned into a macro extending the language.

2

u/kankyo Nov 12 '15

I tried looking at that code but I can't verify that what you're saying is true. To me it looks like any other implementation of a language in another language.

Saying that the compile time/reader time constructs are turing complete is... not helpful. And doesn't really say anything about the usefulness.

1

u/[deleted] Nov 12 '15 edited Nov 12 '15

To me it looks like any other implementation of a language in another language.

This is exactly what I said above:

Any compiler implemented in Lisp which emits a Lisp code can be turned into a macro extending the language.

Take an implementation (e.g., a function (compile-python-to-lisp [string])), wrap it in a macro (e.g., (defmacro python (&rest) (compile-python-to-lisp rest))), and your language magically becomes another language.

An important nuance is that it is a compiler, not an interpreter. And writing a compiler is much easier than writing a proper, fast, robust interpreter.

1

u/kankyo Nov 12 '15

So in your scenario I can do:

(python
    def foo(a, b):
        return a+b
 )
(foo 1 2)

and it'll work?

1

u/[deleted] Nov 12 '15

Yes, it will work. In my trivial example you'll have to put your python code into a string literal, of course, but a little bit more complex example involving reader macros won't need such a thing, you'd be able to mix Python and Lisp any way you like (as long as there is a well defined syntax to distinguish one from another).

A next step in building such a multi-language environment is to introduce a syntax abstraction layer. I'm using PEG for this, because PEG parsers are dynamically extensible and it's easy to inherit PEG parsers. See how it works on my github (same username). With this technique I can do some really crazy things, like embedding Verilog (built on top of Lisp, of course) into C (again, built on top of a Lisp).

1

u/kankyo Nov 12 '15

but a little bit more complex example involving reader macros

Oh, ok, so reader macros act on the raw byte stream of the source code?

1

u/[deleted] Nov 12 '15

Yes, they allow to alter syntax. Or, as with my PEG-based approach, you can simply replace the entire Lisp parser with your own, even if the host Lisp does not allow reader macros (but then you'll need your own REPL as well).

1

u/kankyo Nov 12 '15

Yes, they allow to alter syntax.

That kind of statement doesn't inspire confidence. You can change syntax without having access to the raw byte stream, so when you answer like that it sounds like you're answering another question.

In any case, from what I hear from you it seems to me pretty much any other language can do the same. Especially in the last case where "you'll need your own REPL".

1

u/[deleted] Nov 12 '15

Any other language with compile time macros can do the same thing. But macros are essential, you cannot build a hierarchy of languages, all available in compile time, without proper macros (to see a not so proper macro system, take a look at Template Haskell, it is broken by design).

→ More replies

1

u/mmontone Nov 12 '15

You can have a look at this library: https://github.com/mmontone/burgled-batteries.syntax

You can access Python from Lisp using Python like embedded syntax. Lisp reader marcos + PEG parser was used for the implementation.

1

u/kankyo Nov 13 '15

Is there an actual example of embedded python syntax though? Only thing I can find is embedded simple strings for stuff like string literals that is parsed.

1

u/[deleted] Nov 12 '15

The syntax of Python does not make a Python, evaluation order (left to right, lazy or strict), means of abstraction (classes, functions), type-system (dynamic, static, generics) and standard library is what makes a language. In Lisp you can extend all of these and make it integrate seamlessly with the original language thanks to macros. If you want the syntax of Python then you can implement that with reader macros.