SPPCompiler.SemanticAnalysis.Asts.AnnotationAst module¶
- class SPPCompiler.SemanticAnalysis.Asts.AnnotationAst.AnnotationAst(pos=0, tok_at=None, name=None, *, _ctx=None, _scope=None, is_type_ast=False)¶
Bases:
Ast
The AnnotationAst class is used to represent annotations applied to ASTs. Annotations alter the behaviour of an AST, but do not generate code. For example marking a method as
virtual_method
will trigger specific compiler behaviour, but will not generate any code.There are a small number of annotations that have specific placements, such as
virtual_method
orabstract_method
only being applicable to methods, orhot
only being applicable to functions.Example:
@hot fun function() -> Void { ... }
Custom annotations will be supported in the future, either once the compiler is self-hosting (easier token injection and modification), or compile time functions are supported.
- tok_at: Asts.TokenAst¶
The
@
character that marks the annotation. This must be present for each individual annotation.
- name: Asts.IdentifierAst¶
The name of the annotation.
- print(printer)¶
Print an AST with indentation for inner scopes. The decorator and AstPrinter object “printer” work together to auto-format the output. This function will result in almost identical code to the source code being parsed and generated; the only difference will be the preprocessing applications, which mainly focus on function generation. Some ASTs will be re-ordered, with no effect on code execution.
- Parameters:
printer (
AstPrinter
) – The auto-formatting AstPrinter object. It can be created inline, and the top-most AST it is created in will be the most un-tabbed AST.- Return type:
str
- Returns:
The output string to be printed, fully formatted and pre-processed.
- property pos_end: int¶
The
pos_end
property gets the final index spanned to by this AST. Implementations recursively choose the final AST from their attributes and get that AST’s end position. This will end up with either an identifier of token’s end position, and for either AST this is its start position + length of identifier/token.- Returns:
The final index spanned by this AST.
- pre_process(ctx)¶
Mark the context AST with the correct attributes and values depending on the annotation. This allows all future compiler stages to utilize the additional behaviour based on the annotations.
Most of the values set are setting this AST node itself as a flag (ie
Asts.FunctionPrototypeAst._virtual = self
), but the visibility annotations also include the mappedVisibility
, so the mapping doesn’t have to be recomputed per attribute access.- Parameters:
ctx (
TypeAliasType
) – The context AST to apply the annotation to.- Return type:
None
- generate_top_level_scopes(sm)¶
All annotation context checks, conflict checks, and validity checks are performed at this stage. They require a scope manager for error messages, so can’t be performed at the
pre_process
stage. Although this stage of the compiler is typically used for generating a top level scope for a function or class, it is just utilized as an annotation analysis method that happens with scope access.It cannot be done in the analyse_semantics stage, because certain checks must happen before the behaviour based of annotations is utilized. For example, if both
virtual_method
andabstract_method
were applied, their behaviour may both be applied in an inheritance analysis before these annotations were checked and found to be invalid.- Parameters:
sm (
ScopeManager
) – The scope manager.- Raises:
SemanticErrors.AnnotationInvalidLocationError – This exception is thrown if an annotation is applied to an invalid context. For example, applying
virtual_method
to a free function, orhot
to a class.SemanticErrors.AnnotationConflictError – This exception is raised if the annotation conflicts with another annotation; an example would be both
hot
andcold
being applied to a function.SemanticErrors.AnnotationInvalidError – This exception is thrown if the annotation is unknown. In future versions of the compiler, custom annotations will be supported.
- Return type:
None