Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Clone this wiki locally
While the usual way to interact with other languages is to interact through C interfaces in both ATS and the other language, in some cases it may be desirable to directly compile ATS to another language.
Currently, this is possible for the following languages:
To make a code generator for language xyz, do following:
cd $PATSHOMERELOC/projects/MEDIUM/CATS-parsemit/. make cd $PATSHOMERELOC/projects/MEDIUM/CATS-atsccomp/CATS-atscc2xyz/. make build cp -f atscc2xyz $PATSHOME/bin/.
ATS2 compiler targets a subset of C programming language. There is a library/utility CATS-parsemit (see above for a link) that parses ATS2 compiler output and transforms it into executable code in other programming languages (e.g., currently, Python).
The subset is comprised by the following kinds of syntax (see atsparemit.sats):
- s0exp (static expressions, for types)
- d0exp (dynamic expressions, i.e. expressions that get executed at run-time to yield values, but not involving any control flow)
- instr (low-level instruction nodes, e.g. if-then-else, return, jump, assignment)
- d0ecl (declarations of types, macros, external functions, and functions compiled from ATS2 source code)
A function declaration (f0decl) consists of function kind (external/static), heading (i.e. signature, that is, list of (named) arguments and return type) and optional body (list of local variable definitions and list of instructions). Moreover, in accordance with C rules, every function is emitted twice: firstly, as a forward-declaration (contains no body, arguments might be unnamed), and secondly, as a definition (in this case, body is present).
Overview of making an ATS code generator
First clone the CATS-parsemit directory.
Say you want atscc2XYZ for a language called XYZ.
Create a directory of the name XYZ (or any other name you like) and some symbolic links:
mkdir XYZ cd XYZ ln -s ../*.?ats .
Now you can study atscc2py, atscc2js and atscc2php.
atscc2pyis the hardest as it does not support (1)
gotolabels or (2) switch statements.
atscc2phpis the easiest as it supports both (1) and (2).
atscc2jsis in the middle: it supports (2) but not (1).
Language translating functions
For more complete documentation, please see:
ATSINSlab/ATSINSgotoare used for pattern matching compilation.
ATSINSflab/ATSINSfgotoare used to support tail-call optimization.
ATSmove_boxrec(tmp, T)creates an uninitialized record of the type T, and assign it to tmp.
ATSmove_boxrecis always followed by a few assignments for field initialization; they should probably grouped together and then translated into something like:
tmp = (v1, v2, ..., vn) # (python)
Simple examples can be tried on-line.
make to build. Of course you need
- Please take a look at the examples in this directory. There is a Makefile there that shows how to generate JS code from ATS source.
Say you have
foo.dats. Essentially this is what you need to generate
patsopt -d foo.dats | atscc2js -o foo_dats.js
The directions for the Perl code generator (
atscc2pl) are similar to the other code generators, but we've included some possible directions for convenience.
# # Assumes $PATSHOMERELOC is set to your ATS-Postiats-contrib directory. # #Build atscc2pl cd $PATSHOMERELOC/projects/MEDIUM/CATS-parsemit/ make cd $PATSHOMERELOC/projects/MEDIUM/CATS-atsccomp/CATS-atscc2pl/ make build cp atscc2pl $PATSHOME/bin/ #Build libraries for Perl cd $PATSHOMERELOC/contrib/libatscc/libatscc2pl make make all_in_one export PERL5LIB=$PERL5LIB:$PATSHOMERELOC/contrib/libatscc/libatscc2pl
For compiling code to Python, there is currently no plan to handle pointer operations. Or one may go the Cython road.
Because Python does not support jumps, ATSINSgoto and ATSINSfgoto need to be removed. I will try to do it.