Inclusion of External Code in ATS

Just like including assembly code inside C code, it is straightforward to include C code inside ATS code. For instance, the example appearing at the beginning of this chapter can be written as follows in a single file:

// extern fun fact (n: int): int extern fun fact2 (n: int, res: int): int = "ext#fact2_in_c" // implement fact (n) = fact2 (n, 1) // %{ int fact2_in_c (int n, int res) { while (n > 0) { res *= n ; n -= 1 ; } ; return res ; } %} //

For C code to appear inside ATS code, it needs to enclosed by the symbols %{ (opening) and %} (closing). Essentially, whatever code appearing between these two symbols is pasted into the generated C code at an unspecified position. If the enclosed code is intended to be put at the beginning of the generated C code, then the symbol %{^ should be used in place of %{. If the enclosed code is intended to be put at the bottom of the generated C code, then the symbol %{$ should be used in place of %{.

It is also allowed to put C code between the symbols %{# and %}. Suppose that there is a file of the name foo.sats that contains C code included in this manner. If foo.sats is staloaded in another file of the name foo.dats, then the lines between %{# and %} in foo.sats are pasted into the C code generated from compiling foo.dats.