Skip to content
bbarker edited this page Dec 20, 2014 · 24 revisions

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:

  • C (The default; several compilers are known to work, including GCC, LLVM/Clang, and TinyCC.)
  • Javascript (Compilation is supported by the emscripten compiler which compiles C code to javascript [1].)
  • Python
  • PHP
  • Perl

Introduction

ATS2 compiler targets a subset of C programming language. There is a library/utility ATS-parse-emit (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 ATS-parse-emit 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.

  1. atscc2py is the hardest as it does not support (1) goto labels or (2) switch statements.
  2. atscc2php is the easiest as it supports both (1) and (2).
  3. atscc2js is in the middle: it supports (2) but not (1).

Language translating functions

For more complete documentation, please see:

https://github.com/hsk/docs/blob/master/ats/compiled_c_macro.md

  • ATSINSlab/ATSINSgoto are used for pattern matching compilation.
  • ATSINSflab/ATSINSfgoto are used to support tail-call optimization.
  • ATSmove_boxrec(tmp, T) creates an uninitialized record of the type T, and assign it to tmp.
  • ATSmove_boxrec is 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)

Javascript

Simple examples can be tried on-line.

Here are steps to generate Javascript from ATS on your own system:

  1. Generating the command atscc2js. There is a packaged version on sourceforge (also on github).

Use make to build. Of course you need patscc and patsopt.

  1. 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 foo_dats.js:

patsopt -d foo.dats | atscc2js -o foo_dats.js

Python

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.

Clone this wiki locally