SPPCompiler.SemanticAnalysis.Asts.ArrayLiteralRepeatedElementAst module

class SPPCompiler.SemanticAnalysis.Asts.ArrayLiteralRepeatedElementAst.ArrayLiteralRepeatedElementAst(pos=0, tok_l=None, elem=None, tok_semi_colon=None, size=None, tok_r=None, *, _ctx=None, _scope=None, is_type_ast=False)

Bases: Ast, TypeInferrable

The ArrayLiteralRepeatedElementAst class is an AST node that represents an array literal with a repeated element. The number of repeats is the size of the array, with the array element type being inferrable from the value. Note that the element is evaluated once, then copied N times, not constructed N times. Therefore, the element type must superimpose Copy.

Example:

let x = [0_32; 10]

This will create a Arr[U32, 10] type.

tok_l: Asts.TokenAst

The opening [ token marking an array literal.

elem: Asts.ExpressionAst

The element that will be repeated to form the array.

tok_semi_colon: Asts.TokenAst

The semicolon ; token separating the element and size of the array.

size: Asts.ExpressionAst

The number representing the size of the array.

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::array::Arr, with its generic arguments determined by the element and the size.

Parameters:
  • sm – The scope manager.

  • kwargs – Additional keyword arguments.

Returns:

The inferred array type.

analyse_semantics(sm, **kwargs)

Analyse the element of the array literal, to make sure it is a valid element. This is done to ensure that the array type can be inferred correctly.

Parameters:
  • sm (ScopeManager) – The scope manager.

  • kwargs – Additional keyword arguments.

Return type:

None

code_gen_pass_2(sm, llvm_module, **kwargs)

The array type is std::array::Arr[T, n], which maps to the llvm array type. This AST means that there are no elements in each slot of the array, but the array itself is initialized. The code generation will create an array type object, on the stack, to be used in expression contexts.

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.