3. The Language
- 3.1. Semantics
- 3.2. Program
- 3.3. Tokdec
- 3.4. Tokdef
- 3.5. Tagdec
- 3.6. Tagdef
- 3.7. Altagdef
- 3.8. Structdef
- 3.9. Procdef
3.1. Semantics
The basic philosophy of PL_TDF is to provide the glue
constructors of TDF automatically, while still allowing the programmer to use the significant constructors in their most general form. By glue
constructors, I mean those like make_link, make_group etc. which are there to provide tedious, but vital, constructions concerned with linking and naming. The significant
constructors really come in two groups, depending on their resulting SORTs. There are those SORTs like TOKDEC, whose SORTs are purely syntactic and can't be used as results of token applications or _cond constructions. On the other hand, the first-class SORTs, like EXP, can be used in those situations and generally have a much richer set of constructors. These first-class SORTs are precisely those which have SORTNAMEs. These SORTNAMEs appear in PL_TDF as expansions of <Sortname>:
<Sortname> ::= ACCESS <Sortname> ::= AL_TAG <Sortname> ::= ALIGNMENT <Sortname> ::= BITFIELD_VARIETY <Sortname> ::= BOOL <Sortname> ::= ERROR_TREATMENT <Sortname> ::= EXP <Sortname> ::= FLOATING_VARIETY <Sortname> ::= LABEL <Sortname> ::= NAT <Sortname> ::= NTEST <Sortname> ::= ROUNDING_MODE <Sortname> ::= SHAPE <Sortname> ::= SIGNED_NAT <Sortname> ::= STRING <Sortname> ::= TAG <Sortname> ::= TRANSFER_MODE <Sortname> ::= VARIETY
All of the significant constructors are expanded by non-terminals with names related to their resulting SORT e.g. all EXPs are expanded by <Exp> and all TOKDECs are expanded by <Tokdec>. Any first-class SORT can be expanded by using the constructor names given in the TDF specification, provided that the parameter SORTs are also first-class. For example, the following are all valid expansions of <Exp> :
make_top return(E) where E is an expansion of <Exp> goto(L) where L is an expansion of <Label> assign(E1, E2) where E1 and E2 are expansions of <Exp>
Any such use of TDF constructors will be checked for the SORT-correctness of their parameters. I will denote such a constructor as an <exp_constructor>; similarly for all the other first-class sorts.
Any of the first-class sorts may also be expanded by a token application. Tokens in PL_TDF are given <ident> names by <Tokdef> or <Tokdec> which must occur before their use in applications. In applications, these names will be denoted by <exp_token>, <shape_token> etc. , depending on the result sort of their introduction.
The principle of "no use before declaration" also applies to <ident> names given to TAGs.
3.2. Program
The root expansion of a PL_TDF program is given by <Program>:
<Program> ::= <ElementList> Keep ( <Item>-List-Opt ) <ElementList> ::= <Element> ; <ElementList> ::= <Element> ; <ElementList> <Element> ::= <Tokdec> <Element> ::= <Tokdef> <Element> ::= <Tagdec> <Element> ::= <Tagdef> <Element> ::= <Altagdef> <Element> ::= <Structdef> <Element> ::= <Procdef> <Item> ::= <tag> <Item> ::= <token> <item> ::= <altag>
A <Program> consists of a list of definitions and declarations giving meaning to various <ident>s, as TAGs, TOKENs and AL_TAGs. The <Item>-List-Opt indicates which of these names will be externally available via CAPSULE_LINKs; in addition any other names which are declared but not defined will also be linked externally.
A <Program> will produce a single TDF CAPSULE.
3.3. Tokdec
A <Tokdec> introduces an <ident> as a TOKEN:
<Tokdec> ::= Tokdec <ident><Signature>: [ <TokDecPar>-List-Opt ] <ResultSort> <ResultSort> ::= <Sortname> <TokDecPar> ::= <Sortname> <TokDecPar> ::= TOKEN [ <TokDecPar>-List-Opt ] <ResultSort> <Signature> ::= <String>-Opt
This produces a TOKDEC in a tokdec UNIT of the CAPSULE. Further uses of the introduced <ident> will be treated as a <x-token> where x is given by the <ResultSort>.
3.4. Tokdef
A <Tokdef> defines an <ident> as a TOKEN; this <ident> may have previously been introduced by a <Tokdec>:
<Tokdef> ::= Tokdef <ident><Signature> = <Tok_Defn> <Tok_Defn> ::= [ <TokDefPar>-List-Opt ] <ResultSort> <result_sort> <TokDefPar> ::= <ident> : <TokDecPar> <Signature> ::= <String>-Opt
This produces a TOKDEF in a tokdef UNIT of the CAPSULE. The expansion of <result_sort> depends on <ResultSort>, e.g. if <ResultSort> is EXP then <result_sort> ::= <Exp> and so on.
Each of the <ident>s in the <TokDefPar>s will be names for tokens whose scope is <result_sort>. A use of such a name within its scope will be expanded as a parameterless token application of the appropriate sort given by its <TokDecPar>. Note that this is still true if the <TokDecPar> is a TOKEN - if a <TokDefPar> is:
x: TOKEN[ LABEL ]EXP
then x[L] is expanded as:
exp_apply_token( token_apply_token(x, ()), L)
<Tok_defn> also occurs in an expansion of <Token>, as a parameter of a token application.
3.5. Tagdec
A <Tagdec> introduces an <ident> as a TAG:
<Tagdec> ::= <DecType> <ident> <Signature> <Access>-Opt : <Shape> <DecType> ::= Vardec <DecType> ::= Iddec <DecType> ::= Commondec <Signature> ::= <String>-Opt
This produces a TAGDEC in a tagdec UNIT of the CAPSULE, using a make_id_tagdec for the Iddec option, a make_var_tagdec for the Vardec option and a common_tagdec for the Commondec option.
The <Shape>s in both <Tagdec>s and <Tagdef>s will produce SHAPE TOKENs in a tagdef UNIT; these may be applied in various shorthand operations on TAG <ident>s.
3.6. Tagdef
A <Tagdef> defines an <ident> as a TAG. This <ident> may have previously been introduced by a <Tagdec>; if it has not the < : <Shape> >-Opt below must not be empty and a TAGDEC will be produced for it.
<Tagdef> ::= Var <ident><Signature> < : <Shape> >-Opt < = <Exp>>-Opt
Produces a make_var_tagdef.
<Tagdef> ::= Common <ident> <Signature>< : <Shape> >-Opt < = <Exp> >-Opt
Produces a common_tagdef.
<Tagdef> ::= Let <ident><Signature> < : <Shape> >-Opt = <Exp>
Produces a make_id_tagdef.
<Tagdef> ::= String <ident> <Variety>-Opt =<string>
This is a shorthand for producing names which have the properties of C strings. The <Variety>-Opt gives the variety of the characters with the string, an empty option giving unsigned chars. The TDF produced is a make_var_tagdef initialised by a make_nof_int. This means that given a String definition:
String format = "Result = %d\n"
the tag <ident>, format, could be used straightforwardly as the first parameter of printf, for example.
3.7. Altagdef
An <Altagdef> defines an <ident> as an AL_TAG:
<Altagdef> ::= Al_tagdef <ident> = <Alignment>
This produces an AL_TAGDEF in an al_tagdef UNIT of the CAPSULE. The <ident> concerned can be previously used in as an expansion of <Alignment>.
3.8. Structdef
A <Structdef> defines a TOKEN for a structure SHAPE, together with two TOKENs for each field of the structure to allow easy access to the offsets and contents of the field:
<Structdef> ::= Struct <Structname> ( <Field>-List ) <Structname> ::= <ident> <Field> ::= <Fieldname> : <Shape> <Fieldname> ::= <ident>
This produces a TOKDEF in a tokdef UNIT defining <Structname> as a SHAPE token whose expansion is an EXP OFFSET(a1,a2) where the OFFSET is the size of the structure with standard TDF padding and offset addition of the component SHAPEs and sizes (note that this may not correspond precisely with C sizes).Each <Fieldname> will produce two TOKENs. The first is named by <Fieldname> itself and is a [EXP]EXP which gives the value of the field of its structure parameter. The second is named by prefixing <Fieldname> by the.-symbol and is an [ ]EXP giving the OFFSET of the field from the start of the structure. Thus given:
Struct Complex (re: Double, im: Double)
Complex is a TOKEN for a SHAPE defining two Doubles; re[E] and im[E] will extract the components of E where E is an EXP of shape Complex; .re and.im give EXP OFFSETs of the the two fields from the start of the structure.
3.9. Procdef
A <Procdef> defines a TAG to be a procedure; it is simply an abreviation of a an Iddec <Tagdef>:
<Procdef> ::= Proc <ident> = <Proc_Defn> <Proc_Defn> ::= <Simple_Proc> <Proc_Defn> ::= <General_Proc> <Simple_Proc> ::= <Shape> ( <TagShAcc>-List-Opt <VarIntro>-Opt ) <ClosedExp> <TagShAcc> ::= <Parametername> <Access>-Opt : <Shape> <Parametername> ::= <ident> <VarIntro> ::= Varpar <Varparname> : <Alignment> <Varparname> ::= <ident> <General_Proc> ::= General <Shape> ( <For_Callers>; <For_Callees>) <ProcProps>-Opt <ClosedExp> <For_Callers> ::= <TagShAcc>-List-Opt <...>-Opt <For_Callees> ::= <TagShAcc>-List-Opt <...>-Opt <ProcProps> ::= <untidy>-Opt <check_stack>-Opt
A <Procdef> produces a TAGDEF in a tagdef UNIT and and, possibly, a TAGDEC in a tagdef UNIT.
A <Simple_Proc> produces a make_proc with the obvious operands. The scope of the tag names introduced by <Parametername> and <Varparname> is the <ClosedExp> (see section 3.3).
A <General_Proc> produces a make_general_proc with formal caller parameters given by <For_callers> and the formal callee parameters given by <For_callees>; in both cases the <...> option says that the procedure can be called with a variable number of parameters. The scope of the tag names are the same as for <Simple_Proc>.