Answer the question
In order to leave comments, you need to log in
How to implement a language that will compile to another programming language?
Good afternoon friends. Advise, tell, share, describe how to implement a language that will be compiled into another programming language? (example like coffee script in js). Or where to start, or how to start. Thank you very much in advance.
Answer the question
In order to leave comments, you need to log in
To do this, you need to learn how languages work in general. Read Donald Knuth, read about such a thing as DSL.
Your best bet is to take a dynamic language, flexible. I would recommend Clojure to you.
Clojure is good because you can come up with any design yourself, a strong macro mechanism will allow you to implement your idea.
google flex, bison, reverse polish notation.
In fact, only a parser is needed, which, seeing one language construct, produces another.
it’s not clear why this is necessary, but if something is primitive, it’s like I threw in a structure, and the source code was built from it, then you can go the simple way, a separate text template is created for each command, in which a certain part (parameters) will be replaced, the language must understand that there is a command, and what is a parameter and just replace them with templates.
As already noted - you need to read the theory of compilers.
You need to understand from which blocks the compiler is built and which of them are sufficient to solve your problem.
At a minimum, you need to describe the grammar of the language, both input and output. and transformation rules.
This process is called translation.
Did something like this not too long ago. it was necessary to make a compiler from something similar to excel in php.
the main steps for this
1) tokenization, the lexer module goes through the code character by character and splits the source code, in your new language, into tokens, numbers, keywords, brackets, etc., that is, each token is a certain sequence of characters example ("var", "name ", "=", "vasya", ";", "print", "(", "name", ")", ";").
2) then the parser goes through the tokens and determines their type, while as a rule an AST (abstract syntax tree) is built. it is a set of objects in which the source code is represented in the form of objects and operations on them.
in this form, the code can already be executed without generating code in the target language.
3) compilation. AST is used to generate code in another language.
the main keywords can be found here https://en.wikipedia.org/wiki/Lexical_analysis
if you are familiar with php, then here are examples of such compilers in php
on it, they turn out quite simply, because the language is high-level
https://github.com/symfony /expression-language (parses expressions and can calculate them based on AST, that is, without generating php code, although it can generate it)
https://github.com/twigphp/Twig (parses templates and generates php code)
the code is very similar, because they have one creator.
I also made my parser based on them, but I had to seriously add to it in order to turn expression-language into a full-fledged programming language, with support for loops, variables and other constructs.
code examples of modules from symfony expression language
1) tokenization
https://github.com/symfony/expression-language/blo...
2) parser
https://github.com/symfony/expression-language/blo...
are created here are the entities https://github.com/symfony/expression-language/tre...
that is, a node - a function, a constant, an array, a variable name, etc.
3) compiler
https://github.com/symfony/expression-language/blo...
but most of the code generation happens in the nodes themselves. example - https://github.com/symfony/expression-language/blo...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question