Class: Stone::AST::Lambda

Inherits:
Expression show all
Defined in:
lib/stone/ast/lambda.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes inherited from Stone::AST

#children, #name

Instance Method Summary collapse

Constructor Details

#initialize(parameters, statements) ⇒ Lambda



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

def initialize(parameters, statements)
  @name = :lambda
  @parameters = parameters
  @block = Block.new(statements)
  @lambda_id = next_lambda_id
end

Class Attribute Details

.lambda_countObject

Returns the value of attribute lambda_count.



13
14
15
# File 'lib/stone/ast/lambda.rb', line 13

def lambda_count
  @lambda_count
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



10
11
12
# File 'lib/stone/ast/lambda.rb', line 10

def block
  @block
end

#parametersObject (readonly)

Returns the value of attribute parameters.



10
11
12
# File 'lib/stone/ast/lambda.rb', line 10

def parameters
  @parameters
end

Instance Method Details

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



25
26
27
28
# File 'lib/stone/ast/lambda.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 lambda).
  mod.functions[function_name] || create_function(mod, function_name, function_type, scope)
end

#to_sObject



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

def to_s
  "λ(#{parameters.join(', ')}) { #{block.statements.join("\n")} }"
end

#type(context = nil) ⇒ Object



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

def type(context = nil)
  return_type = @block.type(context) || Stone::Type::Int
  param_types = parameters.map { Stone::Type::Int }
  Stone::Type.function(param_types:, return_type:)
end