Semantic routines are coded in the language accepted by the GNU C compiler. Standard C does not accept local routines (i.e. defined inside another), but GNU C does. Much of the regularity of dulce stems from the qualities of local routines. Standard C might have been used, because techniques exist by which a programmer can take over the administration of a routine's environment to achieve the effect of local routines. However, they are best used in programs generated automatically, not written by humans.
Two possibly unfamiliar aspects of the coding discipline must be understood by contributors. One is the use of continuations the other the techniques used to implement types, and polymorphism in particular.
C programmers use a return statement to express the return from a routine and when appropriate to associate a value with the return. A continuation is a routine that is used as return with a return value, i.e. return x; might be expressed as Return(x); provided the name of the continuation is Return . A continuation will be a parameter of the routine to `return' from, but fortunately the generated external definitions will contain such parameters where they are needed. Consequently it should be easy for C programmers to adjust to the use of continuations.
Type operators are more complex to manage than continuations. A type operator has to be described by a value that can be given as an argument in a routine call. Some naming conventions should be known by contributors:
T_inf A value describing some type T typeinf The type of a value T_inf T_ptr x; or T_type *x; C-representation of a value x of type T in C
Polymorphism is reflected in a semantic routines by the presence of a parameter of type typeinf, T_inf say. A macro call TYPE(T); can be used to introduce declarations of T_type and T_ptr, c.f. Figure 2.3 , lines 24-25.
Definitions with TYPE must occur inside a routine, but the types T_ have to be used already in the parameter list. Consequently, if type T is a parameter it is given as void* before the definition in which it is a parameter, cf. lines 9-21 in Figure 2.3 .
A contributor will have to modify generated external definitions, and care is needed to manage values of unknown types. A fairly simple situation is illustrated by line 35 in Figure 2.4 : the declared type of First is effectively void*, but the intention is obviously that it should be T_ptr so a simple cast will do. This is unproblematic, if one trusts the type inference system of dulce, and if all types are implemented so that the assignment will perform as intended (for which contributors are responsible).
In Figure 2.4 , the generated lines 26-28 redefine types for parameters that can be called. The calls in lines 37-41 shows how to use these for casts, to prevent error messages in some cases (in this no errors would be reported anyhow).
A number of macros are defined in semantics.h to help with continuations. One of these is CVAL which defines (inline) a routine to be passed as a continuation that will assign a value to the variable given as second argument to CVAL along with its type as first argument.
Definition of a new type operator of arity zero can also be simple. The following is a fragment of the code generated from the signature of is_file.
i.e. the same pattern as in for_sem, only with no Attr_inf to start from.
The easy way out is to identify value Attr_inf and type Attr_ptr with int_inf and int_ptr . It is OK if contributors behave properly, but some might want to test types for equality by their typeinf values. Such tests we consider dubious, but some might want to depend on them sometimes. The proper way to introduce a new type operator is illustrated in the actual definition of the is_file-operation, cf. Figure 2.2.
Semantics of operators is defined just as that for operations. The most special case is the handling of a missing operand. A semantics can test for a missing operand simply by testing whether an operand reference is null. Special macros exist to define semantics of an operator that is identical to one of C's. Readers may want to look at the file op_sem.c in the operators subdirectory of the example-hierarchy.