Generating a datcontag-function

A datcontag-function is very similar to a datcon-function. Given a datatype, its datcontag-function is the one that takes a value of the datatype and then returns the tag (which is a small integer) assigned to the (outmost) constructor in the construction of the value. We can use the following directive to indicate (to the ATS compiler) that the datcontag-function for the datatype expr needs to be generated:

#codegen2("datcontag", expr)

By default, the name of the generated function is datcontag_expr_. If a different name is needed, it can be supplied as the third argument of the #codegen2-directive. For instance, the following directive indicates that the generated function is of the given name my_datcontag_expr:

#codegen2("datcontag", expr, my_datcontag_expr)

The following ATS code is expected to be generated that implements datcontag_expr_:

(* ****** ****** *) // implement {}(*tmp*) datcontag_expr_ (arg0) = ( case+ arg0 of | Int _ => 0 | Var _ => 1 | Add _ => 2 | Sub _ => 3 | Mul _ => 4 | Div _ => 5 | Ifgtz _ => 6 | Ifgtez _ => 7 ) // (* ****** ****** *)

Note that the funtion template datcontag_expr_ is required to be declared somewhere in order for the generated code to be compiled properly:

fun{} datcontag_expr_ : (expr) -> intGte(0) // a function template

Please find on-line the entirety of this presented example plus a Makefile (for illustrating the code generation process).