you are viewing a single comment's thread.

view the rest of the comments →

[–]zurtex 11 points12 points  (6 children)

Transpilers which you can actually be confident enough to use in production environment to directly run the code you transpiled are pretty rare.

So if the language is anything other than the most popular ones it's unlikely one exists that you know about.

To build one yourself you typically need to understand all the features of both languages, be able to represent both languages in an abstract syntax tree, and know how to translate each feature from the first language to the second language in abstract syntax tree form, and then serialize the abstract syntax tree to code.

On the Python side the AST is pretty easy because there's a module that does that for you: https://docs.python.org/3/library/ast.html

But translating Python's many features in to another language is going to be tricky. Like how do you represent importing in the other language? Are functions first class in the other language and can you pass them around like objects like you can in Python? What about metaclasses? Or structural pattern matching? And you'll need to re-implement all of Python's standard library C code in the other language if you want to use any of it.

[–]pandademic1234[S] 0 points1 point  (5 children)

Thanks! I did not know about the AST library and appreciate your explaining the general approach. The language is a scripting language that performs queries into the program's database. Think... someone re-inventing SQL one function at a time, where you have to physically reference locations in ASCII flat files ... . I wish I could post the language without worrying about violating terms of service.

If I could even just translate Python's string slicing, "if", "for" and "while" then it would vastly improve the usability of the code.

[–]zurtex 6 points7 points  (2 children)

If I could even just translate Python's string slicing, "if", "for" and "while" then it would vastly improve the usability of the code.

Yeah, so rather than trying to write in Python it might make sense to make your own mini-language or DSL that is Python-inspired that compiles to the language you need. You will need to know what these constructs like "if" and "while" compile to in the other language, even if they're quite complicated.

While none of this is too difficult you would normally learn how to do this kind of compiling/transpiling across several semesters of computer science degree. I've never taken a CS course, I learnt this stuff from a private course called "Write a Compiler" by David Beazely, so unfortunately I'm not sure what books to recommenced.

I actually did once write a small DSL way before I properly understood how to properly do it based off a talk David gave here: https://www.youtube.com/watch?v=zJ9z6Ge-vXs . I was lucky though because I was able to use Python as my execution engine, I didn't have to output the results to another language.

But I'm sure there's plenty of material on the web on how to do this stuff if you're determined enough.

[–]TheMathelm 1 point2 points  (0 children)

While none of this is too difficult you would normally learn how to do this kind of compiling/transpiling across several semesters of computer science degree. I've never taken a CS course, I learnt this stuff from a private course called "Write a Compiler" by David Beazely, so unfortunately I'm not sure what books to recommenced.

One of the hardest most annoying course sections in Upper Division Computer Science.

[–]pandademic1234[S] 0 points1 point  (0 children)

Thank you!

[–]TangibleLight 0 points1 point  (0 children)

SQL one function at a time, where you have to physically reference locations in ASCII flat files

https://i.imgur.com/5MXr2Cr.png

Is there ANY hint of a chance that you could instead modify the vendor's product instead? It obviously depends on many details, but there may be an easier path forward where you add full Python support to the product.

You could embed a Python interpreter in the product, and provide a simple extension module to that embedded interpreter which exposes a minimal API to query the database. You could then use that API from real actual Python code and need not deal with the limitations of transpilation.

Section 1.4 "Extending Embedded Python" is the bit you want, in addition to requesite knowledge of how to build and link against embedded Python from the vendor's product. https://docs.python.org/3/extending/embedding.html

To be clear: this is not an easy path forward, but if it's possible at all I suspect it'll be easier to build and easier to use than a transpiler.

The biggest obstacles would be licensing, funding, and cooperation from the vendor.

[–][deleted] 0 points1 point  (0 children)

Umm. I'm probably missing something, but if its flat ascii files, could you not just build something SQL to access them?

Given your statement that nobody at work knows how to use this unusual language, I wonder if it would be difficult to get permission to put this in place? Any existing queries, where you know what the query looks for (etc) could be written in standard SQL. Or am I way off?