SPPCompiler.SemanticAnalysis.Asts.ArrayLiteralExplicitElementsAst module¶
- class SPPCompiler.SemanticAnalysis.Asts.ArrayLiteralExplicitElementsAst.ArrayLiteralExplicitElementsAst(pos=0, tok_l=None, elems=<factory>, tok_r=None, *, _ctx=None, _scope=None, is_type_ast=False)¶
Bases:
Ast
,TypeInferrable
The ArrayLiteralExplicitElementAst class is an AST node that represents an array literal with n elements. The type of the element is never given, because every expression in S++ is type-inferrable on declaration. This means that the type of the array is inferred from the first element in the array.
Example:
let x = [1_u8, 2_u8, 3_u8, 4_u8]
This will create a std::array::Arr[std::number::U8, 4] type. Arrays in S++ are low-level constructs, and map directly to memory. For example, this array will be stored in memory as 4 consecutive bytes. It is analogous to a C array[], but as a first-class, safe type.
- tok_l: Asts.TokenAst¶
The opening
[
token marking an array literal.
- elems: list[Asts.ExpressionAst]¶
The expressions representing the elements in the array literal.
- tok_r: Asts.TokenAst¶
The closing
]
token marking the end of an array literal.
- 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.
- infer_type(sm, **kwargs)¶
The inferred type will always be std::Arr, with its generic arguments determined by the inferred element type, and the number of elements. The type will be “std::Arr[Element=<self.element_type>, n=<self.size>]”.
- Parameters:
sm – The scope manager.
kwargs – Additional keyword arguments.
- Returns:
The inferred array type.
- analyse_semantics(sm, **kwargs)¶
As this literal takes values inside the brackets, the type of element is inferred from them. All the elements must be the same type. To enforce memory rules, none of these elements can be borrows, in the same way class attributes cannot be borrows.
- Parameters:
sm (
ScopeManager
) – The scope manager.kwargs – Additional keyword arguments.
- Raises:
SemanticErrors.ExpressionTypeInvalidError – This exception is raised if an expression for a value is syntactically valid but makes no sense in this context (“..” or a type).
SemanticErrors.ArrayElementsDifferentTypesError() – This exception is raised if an array element has a different type that the first element. It is a specialized type mismatch error.
SemanticErrors.ArrayElementBorrowedError() – This exception is raised if an array element is in the borrowed state, invalidating it from being stored in an array.
- Return type:
None
- code_gen_pass_2(sm, llvm_module, **kwargs)¶
This array AST will create an array with the element all created, by recursively calling
code_gen_pass_2
on each element AST belonging to this array literal. The array will be created on the stack, and the elements will be stored in the array.- Parameters:
sm (
ScopeManager
) – The scope manager.llvm_module (
Module
) – The LLVM module to generate code into.kwargs – Additional keyword arguments.
- Return type:
AllocaInstr
- Returns:
The LLVM array object that can be used in the expression context.