Class: Rubex::AST::Statement::VarDecl

Inherits:
Base
  • Object
show all
Defined in:
lib/rubex/ast/statement/var_decl.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#location

Instance Method Summary collapse

Methods inherited from Base

#==, #statement?

Constructor Details

#initialize(type, name, value, location) ⇒ VarDecl

Returns a new instance of VarDecl.



7
8
9
10
11
12
# File 'lib/rubex/ast/statement/var_decl.rb', line 7

def initialize(type, name, value, location)
  super(location)
  @name = name
  @value = value
  @type = type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/rubex/ast/statement/var_decl.rb', line 5

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/rubex/ast/statement/var_decl.rb', line 5

def value
  @value
end

Instance Method Details

#analyse_statement(local_scope, extern: false) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rubex/ast/statement/var_decl.rb', line 14

def analyse_statement(local_scope, extern: false)
  # TODO: Have type checks for knowing if correct literal assignment
  # is taking place. For example, a char should not be assigned a float.
  @type = Helpers.determine_dtype @type, ''
  c_name = extern ? @name : Rubex::VAR_PREFIX + @name
  if @value
    @value.analyse_for_target_type(@type, local_scope)
    @value.allocate_temps local_scope
    @value = Helpers.to_lhs_type(self, @value)
    @value.release_temps local_scope
  end

  local_scope.declare_var name: @name, c_name: c_name, type: @type,
                          value: @value, extern: extern
end

#generate_code(code, local_scope) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/rubex/ast/statement/var_decl.rb', line 37

def generate_code(code, local_scope)
  if @value
    @value.generate_evaluation_code code, local_scope
    lhs = local_scope.find(@name).c_name
    code << "#{lhs} = #{@value.c_code(local_scope)};"
    code.nl
    @value.generate_disposal_code code
  end
end

#rescan_declarations(scope) ⇒ Object



30
31
32
33
34
35
# File 'lib/rubex/ast/statement/var_decl.rb', line 30

def rescan_declarations(scope)
  if @type.is_a? String
    @type = Rubex::CUSTOM_TYPES[@type]
    scope[@name].type = @type
  end
end