Class: Stone::AST::Block

Inherits:
Expression show all
Includes:
TwoPhaseProcessing
Defined in:
lib/stone/ast/block.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Stone::AST

#children, #name

Instance Method Summary collapse

Constructor Details

#initialize(statements) ⇒ Block



19
20
21
22
23
# File 'lib/stone/ast/block.rb', line 19

def initialize(statements)
  @name = :block
  @statements = statements
  @block_id = next_block_id
end

Class Attribute Details

.block_countObject

Returns the value of attribute block_count.



14
15
16
# File 'lib/stone/ast/block.rb', line 14

def block_count
  @block_count
end

Instance Attribute Details

#statementsObject (readonly)

Returns the value of attribute statements.



11
12
13
# File 'lib/stone/ast/block.rb', line 11

def statements
  @statements
end

Instance Method Details

#evaluate_body_and_return(builder, mod, scope) ⇒ Object

Evaluate all statements in block and return value of last statement.



70
71
72
73
74
75
# File 'lib/stone/ast/block.rb', line 70

def evaluate_body_and_return(builder, mod, scope)
  child_scope = scope.child
  register_type_declarations(child_scope)
  last_result = compile_statements(builder, mod, child_scope)
  builder.ret(last_result)
end

#to_llir(_builder, mod, scope = Stone::Scope.top_level) ⇒ Object



25
26
27
28
# File 'lib/stone/ast/block.rb', line 25

def to_llir(_builder, mod, scope = Stone::Scope.top_level)
  # Check if function already exists (happens if to_llir called multiple times on same block).
  mod.functions[function_name] || create_function(mod, function_name, function_type, scope)
end

#to_sObject



30
31
32
# File 'lib/stone/ast/block.rb', line 30

def to_s
  "{ #{statements.join("\n")} }"
end

#type(context = nil) ⇒ Object



34
35
36
37
# File 'lib/stone/ast/block.rb', line 34

def type(context = nil)
  last_expression = statements.reverse.find { |stmt| stmt.type(context) }
  last_expression&.type(context)
end