Automatic types
From dis-Emi-A
Contents |
Introduction
First, by example, an automatic type used in a function:
sum :> ( a, b -> a + b ); . . . q = 1; r = 2; s = sum( q, r );
There is no explicit typing. The compiler is required to determine the type of the parmaeters when the function is called. For the variable declaractions it has to determine what storage the type requires, based on its use and reference.
This solves a few problems:
- memory management is completely automatic, no failures to delete, no worrying about global/heap/stack/register
- a function with not explicit typing is automatically a parametric function
- no hellish C++ type syntax for assignments, an auto-type will just use the correct types
- room for optimization, the compiler can chose to produce special versions of the function based on calling patterns
Autotyping mechanism
(Expanded from description in compiler.h)
Compilation of a program passes through these stages:
- Parse
- units are parsed
- AssignContext
- items and references are tracked into an appropriate context hierarchy
- TraceFunctions
- a call graph is created for use in up typing
- UpType
- working from leaf nodes upwards fill in as many details as possible about function scenarios
- DownType
- starting from an entry point completes all types to produce a well defined program.
- Emit
- Produce the output of the program, that is the final compiler form suitable for translation into a binary
Parse
This is where the source files get translated into abstract representations based on the language syntax.
This is a traditional topic not needing a lot of discussion, but we don't need to make sure we make a language that is parseable in an umabiguous fashion and without exotic parsing requirements.
Assign Context
This is a strictly technical stage that does the mapping between source symbols and logical symbols. The typing mechanism starts minimally at this stage by determining what the possible meanings of a symbol are -- though beyond that this is a rather uncomplicated stage and well explored by almost all languages (though not necessarily as a distinct stage)
Trace Functions
As preparation for up-typing it is necessary to have a complete graph of the function calls within the system. This additionally includes any references to types and global accesses which may be used.
Up-Typing
This is basically the first significant half of the auto-typing mechanism and involves applying types from leaf nodes upwards.
See the section Up-Typing
Down-Typing
This is the second half of the auto-typing mechanism and involves apply types from the main (or entry) function down towards the leaves of the graph.
See the section Down-Typing
