
Haxe is a very convenient and practical language, but a small community and, as a result, a small number of libraries make me spend a lot of time preparing “header files” for integrating open source libraries into haxe. I would like to tell you a little about this language and ways to convert source code in different languages ​​below.
With the haxe programming language (then still his name was haXe) I met about three years ago and since then we have not parted. Since Since this language is poorly lit on Habré, for a start - about haxe "in a nutshell", as it is sung in a famous
song .
Oh haxe in a nutshell
For those unfamiliar with haxe, a little background information:
- the syntax of this language almost completely repeats ActionScript, which in turn is similar to JavaScript, but with data types;
- hard typing, but with automatic type inference (for simple cases);
- the absence of a rigidly bound runtime environment — the compiler only translates the haxe code into other languages ​​(currently supported: neko , php, javascript, flash / actionscript, c ++, java, c #; python is also on the way);
- very fast compiler.
Like other languages, haxe is not a silver bullet and, it seems to me, there are two main areas where it is useful:
- writing multi-platform applications (here it is worth mentioning the library for developing games OpenFL );
- writing complex js applications (because writing them directly to js is problematic due to the lack of typing).
Convert code
The ways to convert source code in one language into code in another language I see the following:
- through building a full parse tree (Abstract Source Tree = AST);
- through the use of tools that can transform source codes into something simpler (like xml);
- "Brute force" through the use of regular expressions.
Without a doubt, the mathematically correct path is the first, since allows you to do everything neatly and, ideally, get the output immediately compiled text in another language. Cons - a full analysis of complex, sensitive to detail. You can read about building AST trees in the compiler literature (see, for example,
Aho A., Seti R., Ulman J., Lam M. - Compilers. Principles, technologies, tools ).
The second way is possible only if there are suitable utilities for the source language. The author had to use
yuidoc when writing the haxe-wrapper generator for the popular js-library
easeljs , since the latter is well documented.
The third way - through processing by regular expressions - is relatively simple, although it does require file refinement of the resulting code. It is about this option will be discussed below.
Okay, regular, convert!
Regular expressions have a huge, in my opinion, plus — they are quickly written and, only, a couple of minuses:
- in principle, they cannot parse nested (recurrent) structures (with an arbitrary level of nesting);
- hard to read (and for large expressions - and no less hard to write).
The first drawback, as practice has shown, is not very critical for most languages, especially if we do not need a full-fledged conversion, but only “pulling out” the headings of classes and methods. The second one can be partially circumvented by entering constants that store small pieces of regular expressions and allowing them to be used to write more complex constructions.
')
As a result, we arrive at sets of transformation rules, where there are two types of these rules: constant declarations and regular expressions for search / replace. Here is a fragment of the rule file for conversion from c # to haxe:
ID = \b[_a-zA-Z][_a-zA-Z0-9]*\b LONGID = ID(?:[.]ID)* TYPE = LONGID(?:[<]\s*LONGID(?:\s*,\s*LONGID)*\s*[>])? // "int[]" => "Array<int>" /(TYPE)\s*\[\s*\]/Array<$1>/ // "int v" => "var v:int" /(TYPE)\s+(ID)/var $2:$1/
The matter remains small - to write a tool that would accept source files and a regex-rules file as input, and would output files with the result of applying these rules. And such a utility was written (
refactor ). Below I will provide some code to show (I hope) the simplicity and brevity of the haxe language.
Consider the code of the class that reads the file with the rules, parses where the constants are, and where are the regulars, and builds an array of regular expressions for the subsequent conversion of the source files:
import stdlib.Regex;
Conclusion
The author has been using haxe for three years to write web applications. It's great: the ability to write client and server code in the same language + strong typing + syntax close to js - all this is very good.
The created
refactor tool simplified the integration of haxe code with third-party libraries. For example, recently with its help the
wrapper for js-library
threejs was created.
I hope I managed to interest you, if not in the haxe language, then at least in the approach to processing the source texts of programs. After all, using this simple method, you can not only convert programs from language to language, but also simply make the text of the program beautiful (beautify).