R
R
Rrooom2014-10-17 14:01:01
Classmates
Rrooom, 2014-10-17 14:01:01

How to write your own translator of one language into another?

My dream is to abandon js and write everything in a `normal` language (substitute your favorite language of your choice), and ideally use common entity classes in the frontend and backend.
But... I'm pretty vague about how to do this. Articles for dummies use regular expressions... But that's not true, is it? Yes, and it is impossible to describe everything on them.
They also often write their "compilers", on the stack, for example, but I don't need it.
I need to parse a language (by the way, almost all languages ​​are open, but where can I find a description of their syntax tree? For python or go? ) and match constructions from another language.
What do you need to know for this? There is such a mess of incomprehensible terms in my head, like "context-free grammars, bison, ast-trees", but I can’t think of where to start and what to study.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
Andrey K, 2014-10-17
@mututunus

Not only you have a dream to abandon js. Your translator is clearly not a solution to the problem.
You can look towards Dart . It has a translator in js and, in the future, Google wants to make it a universal language for frontend and backend.

T
Tyranron, 2014-10-17
@Tyranron

Actually, for Go, the parser and other goodies are supplied in its standard library:
golang.org/pkg/go
But, not everything is so simple. It is not enough just to take and generate another based on one syntax. Languages ​​can differ quite seriously in the model of working with memory. For example, be aware that JS runs on a single thread and Go doesn't have to, which makes many things in Go that are geared towards synchronization pointless. Also, for example, I can’t immediately figure out how to optimally relay such low-level things as pointers into JS, because JS itself does not allow you to “feel” them with handles, as Go allows. And if you dig further, there will be many more such moments, because the sweetness of Go is in its runtime, which cannot be just taken and dragged into the browser on the hump of JS.
You can, of course, take and stupidly impose the Go syntax on the single-threaded JS model, it should be enough, but it will no longer be pure Go, but a slightly different specification for a different memory model, with its own pitfalls. And here the question arises: is Go expressive enough for the tasks that JS solves in the browser? In pursuit of appearance and syntax, didn’t we attach a body from a tractor to a passenger car, as a result of achieving our goal, but having at least problems with parking? After all, languages ​​were created to solve different problems.
As for Python, things should be better here, since the language models are closer. I am sure that there are already some relays from Python to JS. I have heard many times that it would be nice to sculpt in a Python browser, and the world is full of enthusiasts =) .
Returning to the problem of relaying, @mututunus gave you the right vector. Study the issue in depth, look at the successes of others in this field. The same Dart and JS have very similar language models, and this is no accident. At first glance, Dart looks like JS on steroids with normal async, packages, optional static typing, and other sweets. Not surprisingly, it translates easily and fairly well to JS. Moreover, it was created just as a replacement for JS, that is, for solving problems in the browser, it is imprisoned for this in a certain sense, which, of course, results in a certain language design and certain concepts.

Q
q1t, 2014-10-18
@q1t

https://github.com/gopherjs/gopherjs

A
Alexander, 2014-11-16
@OneFive

opalrb.org

V
Vapaamies, 2015-01-06
@vapaamies

Why didn't anyone talk about Haxe ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question