Package org.ek9lang.compiler.phase1
This phase actually has a number of passes through the 'ANTLR' 'AST', in fact the EK9 Compiler uses both the 'ANTLR' Listener and Visitor models to process the 'AST'.
The reason for the multiple passes is to enable 'forward referencing', each successive pass tends to add more detail to the Symbols that are recorded.
The passes in phase 1 are:
These are configured in FrontEndSupplier
The SymbolDefinition
mainly uses the
DefinitionListener
with an 'ANTLR' - tree walker to traverse the 'AST'.
There are quite a few rules and checks - even at this early stage. Most of these are simple Java 'Consumers'
or 'BiConsumers' and do simple basic checks - issuing errors by calling
ErrorListener.semanticError(org.antlr.v4.runtime.Token, java.lang.String, org.ek9lang.compiler.common.ErrorListener.SemanticClassification)
with an appropriate ErrorListener.SemanticClassification
.
In general many of the phases use the 'Listener' approach and 'walk the tree'. Sometimes they process
'ANTLR' contexts on entry and at other times on exit. The 'Listeners' use
SymbolsAndScopes
and this uses a
ScopeStack
to 'push and 'pop' scopes as the 'AST' is traversed.
In this way it is possible to add Symbols to the correct scope during AST traversal.
Most of the checks on symbols and constructs are very focussed and have a 'single responsibility'. They are then composed and applied. Initially most of these were just methods on classes. But this became quite complex, so it was refactored in to 'many' separate classes (Functions/Consumers). While there are many more it has helped enable more focus.
The latter two passes are really just cross source file and module checks and as such run in a single threaded manner.
See org.ek9lang.compiler.phase2
for the next stage of compilation.
-
ClassDescriptionChecks for the appropriate use of the APPLICATION of xyz on a method declaration.Checks if the body of a function is appropriate.Creates a suitable scope name for a token position.Just resolves a number of built-in types so that internal compiler code can be more explicit.Designed to encapsulate the fact that two token are in conflict for a Symbol.Checks that either all parameters are named when making calls, or none are named at all.Just go through and define the symbols and scopes putting into the ParsedModule against the appropriate context.Checks that a dynamic class does not used naming when used inside a generic type/function.Dynamic Variables when captured have to either be identifiers in their own right, but if they are some type of expression, method call, etc.Error if a construct and a reference conflict with each other.Error when a reference is made, but it cannot be resolved.To be called when an unreachable statement is encountered to emit an error.Checks that a constructor method on a generic aggregate has the same number of parameters as the types being used in the generic type.Typically used with programs.Just checks that named operator methods: mod, rem, abs, sqrt, open, close, contains, matches, empty, length are not being used as just method names.Goes through each module name and checks each of the parsedModules in that module name to check there is only a single copy of that symbol in the whole module name space.Typically used for Constructors, but could be used in other contexts where a method is not expected to return anything.Aimed at methods in services and a program (which is a method).Just check is a scope has normal termination.Typically used in control expressions to check developer is not doing a while(true).Check not dispatcher methods.Only use when code being checked prohibits use in outer generic context.Processes and checks Methods that apply in all method contexts for basic correctness.Constructs like methods and functions may or may not always retun a value.Checks on whether the '?' can be used as a modifier then check the parent of this context.A program can only accept specific types of arguments to it.A program can only return an Integer (exit code).SINGLE THREADED - because we only want one reference shorthand for an item.Antlr listener for the references phase.Can be MULTI THREADED for developer source, but must be single threaded for bootstrapping.Check an assignment again 'super' use and some operators against 'this' use.Checks methods from various contexts, typically this is delegated to other functions.Error when use of ParameterisedType has additional expression parameters in wrong context.Checks that if a return is used (i.e.Error when a reference is attempted by the syntax is incorrect.Checks that variable declarations is acceptable.