-
Notifications
You must be signed in to change notification settings - Fork 0
Mapping Modula 2 Identifiers to C
All Modula-2 identifiers are converted to a C representation in snake_case
or MACRO_CASE
, depending on the object they represent. In order to convert a Modula-2 identifier to its C representation, it is first split at word boundaries, the resulting components are then converted to all-lowercase or all-uppercase, and are then rejoined by inserting a lowline _
between any two components. Certain types of identifiers are additionally prefixed or suffixed or both.
Word boundaries in the Modula-2 identifier are determined as follows:
(1) a sequence of lowercase letters and digits preceding a capital letter, or
(2) a sequence of capital letters and digits preceding a sequence of a capital letter followed by lowercase letters and digits
sets the word boundary before the capital letter;
(3) a sequence of a capital letters followed by digits preceding a sequence of lowercase letters sets the word boundary before the first lowercase letter.
(4) where lowlines are enabled and used, any lowline sets the word boundary at the lowline.
The C representations of all exported and all imported Modula-2 identifiers are qualified with a module prefix, derived from the module identifier of the module that exports the identifier.
- To derive the module prefix for C macro identifiers, the module identifier is converted to
MACRO_CASE
. - To derive the module prefix for all other C identifiers, the module identifier is converted to
snake_case
.
To obtain the C representation of the fully qualified identifier, the derived module prefix is prepended to the C representation of the unqualified identifier while two lowlines __
are inserted between them.
With the exception of local variable identifiers, the C representation of all other local identifiers are qualified with a local suffix, derived from the base-36 representation of a hash value computed from the identifier of the surrounding procedure.
To obtain the C representation of the fully qualified identifier, the derived local suffix is appended to the C representation of the unqualified identifier while two lowlines __
are inserted between them. The suffix always starts with a decimal digit.
To obtain the C representation of a Modula-2 constant identifier, the identifier is converted to MACRO_CASE
.
Examples
- A non-local non-exported Modula-2 constant
FooBar
is mapped toFOO_BAR
. - A Modula-2 constant
BazBam
exported by moduleFooBar
is mapped toFOO_BAR__BAZ_BAM
. - A local Modula-2 constant
BazBam
in procedureFooBar
is mapped toBAZ_BAM__DXXXXX
whereDXXXXX
is the base-36 hash value ofFooBar
.
The C representations of all Modula-2 enumerated value identifiers are qualified with an enumeration prefix, derived from the type identifier of their respective enumeration type converted to MACRO_CASE
.
To obtain the C representation of a Modula-2 enumerated value identifier, the identifier is converted to MACRO_CASE
and the enumeration prefix is prepended while a lowline _
is inserted between them.
Examples
Given the enumeration type declaration TYPE Color = ( Red, Green, Blue );
- If type
Color
is non-local and non-exported,Red
is mapped toCOLOR_RED
. - If type
Color
is exported by moduleGraphics
,Red
is mapped toGRAPHICS__COLOR_RED
. - If type
Color
is declared local in procedureDraw
,Red
is mapped toCOLOR_RED__DXXXXX
whereDXXXXX
is the base-36 hash value ofDraw
.
The C representations of all Modula-2 type identifiers are marked with the type suffix _t
.
To obtain the C representation of a type identifier, the identifier is converted to snake_case
and the type suffix is appended.
Examples
- A non-local non-exported Modula-2 type identifier
FooBar
is mapped tofoo_bar_t
. - A Modula-2 type identifier
BazBam
exported by moduleFooBar
is mapped tofoo_bar__baz_bam_t
. - A local Modula-2 type identifier
BazBam
in procedureFooBar
is mapped tobaz_bam_t__DXXXXX
whereDXXXXX
is the base-36 hash value ofFooBar
.
To obtain the C representation of a Modula-2 variable identifier, the identifier is converted to snake_case
.
Examples
- A non-local non-exported Modula-2 variable identifier
fooBar
is mapped tofoo_bar
. - A Modula-2 variable identifier
bazBam
exported by moduleFooBar
is mapped tofoo_bar__baz_bam
. - A local Modula-2 variable identifier
bazBam
in procedureFooBar
is mapped tobaz_bam
without local suffix.
To obtain the C representation of a Modula-2 function identifier, the identifier is converted to snake_case
.
Examples
- A non-local non-exported Modula-2 function identifier
fooBar
is mapped tofoo_bar
. - A Modula-2 function identifier
bazBam
exported by moduleFooBar
is mapped tofoo_bar__baz_bam
. - A local Modula-2 function identifier
bazBam
in procedureFooBar
is mapped tobaz_bam__DXXXXX
whereDXXXXX
is the base-36 hash value ofFooBar
.
The C representations of all Modula-2 procedure identifiers are marked with the procedure prefix do_
.
To obtain the C representation of a procedure identifier, the identifier is converted to snake_case
and the procedure prefix is prepended.
Examples
- A non-local non-exported Modula-2 procedure identifier
FooBar
is mapped todo_foo_bar
. - A Modula-2 procedure identifier
BazBam
exported by moduleFooBar
is mapped tofoo_bar__do_baz_bam
. - A local Modula-2 procedure identifier
BazBam
in procedureFooBar
is mapped todo_baz_bam__DXXXXX
whereDXXXXX
is the base-36 hash value ofFooBar
.
Where the final C representation of a Modula-2 identifier coincides with a reserved word of C, its first letter is capitalised.
Example
- A local Modula-2 variable
switch
is mapped toSwitch
. - A non-exported Modula-2 function
float
is mapped toFloat
.
This mapping scheme was designed with the common Modula-2 naming convention in mind:
- Module, type and procedure identifiers are in
TitleCase
, - Math constant, variable and function identifiers are in
camelCase
, - Other constant identifiers are consistently either in
camelCase
orTitleCase
.
In the placeholder DXXXXX
, representing a local suffix, D
represents a decimal digit, X
a base-36 digit.
Copyright (C) 2023 Modula-2 Software Foundation