Module: Rubex::SymbolTable::Scope

Included in:
Klass, Local, StructOrUnion
Defined in:
lib/rubex/symbol_table/scope.rb,
lib/rubex/symbol_table/scope.rb

Defined Under Namespace

Classes: BeginBlock, Klass, Local, NoGilBlock, StructOrUnion

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#arg_entriesObject

Returns the value of attribute arg_entries.



6
7
8
# File 'lib/rubex/symbol_table/scope.rb', line 6

def arg_entries
  @arg_entries
end

#c_method_entriesObject

Returns the value of attribute c_method_entries.



12
13
14
# File 'lib/rubex/symbol_table/scope.rb', line 12

def c_method_entries
  @c_method_entries
end

#carray_entriesObject

Returns the value of attribute carray_entries.



10
11
12
# File 'lib/rubex/symbol_table/scope.rb', line 10

def carray_entries
  @carray_entries
end

#entriesObject

Returns the value of attribute entries.



4
5
6
# File 'lib/rubex/symbol_table/scope.rb', line 4

def entries
  @entries
end

#free_temp_entriesObject

Returns the value of attribute free_temp_entries.



19
20
21
# File 'lib/rubex/symbol_table/scope.rb', line 19

def free_temp_entries
  @free_temp_entries
end

#global_entriesObject

Returns the value of attribute global_entries.



20
21
22
# File 'lib/rubex/symbol_table/scope.rb', line 20

def global_entries
  @global_entries
end

#klass_nameObject (readonly)

Returns the value of attribute klass_name.



21
22
23
# File 'lib/rubex/symbol_table/scope.rb', line 21

def klass_name
  @klass_name
end

#nameObject (readonly)

Returns the value of attribute name.



21
22
23
# File 'lib/rubex/symbol_table/scope.rb', line 21

def name
  @name
end

#outer_scopeObject

Returns the value of attribute outer_scope.



5
6
7
# File 'lib/rubex/symbol_table/scope.rb', line 5

def outer_scope
  @outer_scope
end

#ruby_class_entriesObject

Returns the value of attribute ruby_class_entries.



14
15
16
# File 'lib/rubex/symbol_table/scope.rb', line 14

def ruby_class_entries
  @ruby_class_entries
end

#ruby_constant_entriesObject

Returns the value of attribute ruby_constant_entries.



16
17
18
# File 'lib/rubex/symbol_table/scope.rb', line 16

def ruby_constant_entries
  @ruby_constant_entries
end

#ruby_method_entriesObject

Returns the value of attribute ruby_method_entries.



15
16
17
# File 'lib/rubex/symbol_table/scope.rb', line 15

def ruby_method_entries
  @ruby_method_entries
end

#ruby_obj_entriesObject

Returns the value of attribute ruby_obj_entries.



9
10
11
# File 'lib/rubex/symbol_table/scope.rb', line 9

def ruby_obj_entries
  @ruby_obj_entries
end

#self_nameObject

Returns the value of attribute self_name.



17
18
19
# File 'lib/rubex/symbol_table/scope.rb', line 17

def self_name
  @self_name
end

#sue_entriesObject

Returns the value of attribute sue_entries.



11
12
13
# File 'lib/rubex/symbol_table/scope.rb', line 11

def sue_entries
  @sue_entries
end

#temp_entriesObject

Returns the value of attribute temp_entries.



18
19
20
# File 'lib/rubex/symbol_table/scope.rb', line 18

def temp_entries
  @temp_entries
end

#typeObject

Returns the value of attribute type.



7
8
9
# File 'lib/rubex/symbol_table/scope.rb', line 7

def type
  @type
end

#type_entriesObject

Returns the value of attribute type_entries.



13
14
15
# File 'lib/rubex/symbol_table/scope.rb', line 13

def type_entries
  @type_entries
end

#var_entriesObject

Returns the value of attribute var_entries.



8
9
10
# File 'lib/rubex/symbol_table/scope.rb', line 8

def var_entries
  @var_entries
end

Instance Method Details

#[](entry) ⇒ Object



158
159
160
161
# File 'lib/rubex/symbol_table/scope.rb', line 158

def [] entry
  @entries[entry] or raise(Rubex::SymbolNotFoundError,
    "Symbol #{entry} does not exist in this scope.")
end

#add_c_method(name:, c_name:, scope:, arg_list:, return_type:, extern: false) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/rubex/symbol_table/scope.rb', line 111

def add_c_method name:, c_name:, scope:, arg_list:, return_type:, extern: false
  type = Rubex::DataType::CFunction.new(
    name, c_name, arg_list, return_type, scope)
  entry = Rubex::SymbolTable::Entry.new name, c_name, type, nil
  entry.extern = extern
  @entries[name] = entry
  @c_method_entries << entry

  entry
end

#add_carray(name:, c_name:, dimension:, type:, value: nil) ⇒ Object

Add a C array to the current scope.



91
92
93
94
95
96
97
98
# File 'lib/rubex/symbol_table/scope.rb', line 91

def add_carray name: ,c_name: ,dimension: ,type: ,value: nil
  type = Rubex::DataType::CArray.new dimension, type
  entry = Rubex::SymbolTable::Entry.new name, c_name, type, value
  @entries[name] = entry
  @carray_entries << entry

  entry
end

#add_ruby_class(name:, c_name:, scope:, ancestor:, extern:) ⇒ Object

Add a Ruby class to the current scope.



101
102
103
104
105
106
107
108
109
# File 'lib/rubex/symbol_table/scope.rb', line 101

def add_ruby_class name: , c_name:, scope:, ancestor:, extern:
  type = Rubex::DataType::RubyClass.new name, c_name, scope, ancestor
  entry = Rubex::SymbolTable::Entry.new name, c_name, type, nil
  entry.extern = extern
  @entries[name] = entry
  @ruby_class_entries << entry

  entry
end

#add_ruby_method(name:, c_name:, scope:, arg_list:, extern: false) ⇒ Object

name: name of the method c_name: c_name of the method extern: whether it is defined within the Rubex script or in a scope

outside the Rubex script.


126
127
128
129
130
131
132
133
134
# File 'lib/rubex/symbol_table/scope.rb', line 126

def add_ruby_method name:, c_name:, scope:, arg_list:, extern: false
  type = Rubex::DataType::RubyMethod.new name, c_name, scope, arg_list
  entry = Rubex::SymbolTable::Entry.new name, c_name, type, nil
  entry.extern = extern
  @entries[name] = entry
  @ruby_method_entries << entry unless extern

  entry
end

#add_ruby_obj(name:, c_name:, value: nil) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/rubex/symbol_table/scope.rb', line 81

def add_ruby_obj name: , c_name:, value: nil
  entry = Rubex::SymbolTable::Entry.new(
    name, c_name, Rubex::DataType::RubyObject.new, value)
  @entries[name] = entry
  @ruby_obj_entries << entry

  entry
end

#allocate_temp(type) ⇒ Object

allocate a temp and return its c_name



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rubex/symbol_table/scope.rb', line 137

def allocate_temp type
  if @free_temp_entries.empty?
    @temp_counter += 1
    c_name = Rubex::TEMP_PREFIX + @temp_counter.to_s
    entry = Rubex::SymbolTable::Entry.new c_name, c_name, type, 
      Expression::Literal::CNull.new('NULL')
    @entries[c_name] = entry
    @temp_entries << entry
  else
    entry = @free_temp_entries.pop
    c_name = entry.c_name
  end

  c_name
end

#check_entry(name) ⇒ Object



45
46
47
48
49
# File 'lib/rubex/symbol_table/scope.rb', line 45

def check_entry name
  if @entries.has_key? name
    raise "Symbol name #{name} already exists in this scope."
  end
end

#declare_sue(name:, c_name:, type:, extern:) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/rubex/symbol_table/scope.rb', line 62

def declare_sue name:, c_name:, type:, extern:
  entry = Rubex::SymbolTable::Entry.new(
    name, c_name, type, nil)
  entry.extern = extern
  @entries[name] = entry
  @sue_entries << entry
  @type_entries << entry

  entry
end

#declare_type(type:, extern:) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/rubex/symbol_table/scope.rb', line 73

def declare_type type:, extern:
  entry = Rubex::SymbolTable::Entry.new(nil, nil, type, nil)
  entry.extern = extern
  @type_entries << entry

  entry
end

#declare_var(name: "", c_name: "", type: nil, value: nil, extern: false) ⇒ Object

vars - Rubex::AST::Statement::VarDecl/CPtrDecl



52
53
54
55
56
57
58
59
60
# File 'lib/rubex/symbol_table/scope.rb', line 52

def declare_var(name: "", c_name: "", type: nil, value: nil, extern: false)
  entry = Rubex::SymbolTable::Entry.new(name, c_name, type, value)
  entry.extern = extern
  check_entry name
  @entries[name] = entry
  @var_entries << entry

  entry
end

#find(name) ⇒ Object

Find an entry in this scope or the ones above it recursively.



168
169
170
# File 'lib/rubex/symbol_table/scope.rb', line 168

def find name
  return recursive_find(name, self)
end

#has_entry?(entry) ⇒ Boolean

Returns:



163
164
165
# File 'lib/rubex/symbol_table/scope.rb', line 163

def has_entry? entry
  !!@entries[entry]
end

#initialize(outer_scope = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubex/symbol_table/scope.rb', line 23

def initialize outer_scope=nil
  @outer_scope = outer_scope
  @entries = {}
  @arg_entries = []
  @var_entries = []
  @type = nil
  @ruby_obj_entries = []
  @carray_entries = []
  @sue_entries = []
  @c_method_entries = []
  @type_entries = []
  @ruby_class_entries = []
  @ruby_method_entries = []
  @ruby_constant_entries = []
  @self_name = ""
  @klass_name = ""
  @temp_entries = []
  @free_temp_entries = []
  @global_entries = []
  @temp_counter = 0
end

#release_temp(c_name) ⇒ Object

release a temp of name ‘c_name’ for reuse



154
155
156
# File 'lib/rubex/symbol_table/scope.rb', line 154

def release_temp c_name
  @free_temp_entries << @entries[c_name]
end