Class: CType::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/caphir/ctype.rb

Overview

Union

Constant Summary collapse

@@enums =
{}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Enum

Returns a new instance of Enum.



201
202
203
204
# File 'lib/caphir/ctype.rb', line 201

def initialize(name)
	@name = name
	@members = nil
end

Class Method Details

.[](val = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/caphir/ctype.rb', line 186

def Enum.[](val = nil)
	# for unnamed enumerations
	return Enum.send(:new, nil) unless val

	result = @@enums[val]
	unless result
		result = @@enums[val] = Enum.send(:new, val)
	end
	result
end

Instance Method Details

#add(items) ⇒ Object



205
206
207
208
209
# File 'lib/caphir/ctype.rb', line 205

def add(items)
	warn "enum #{@name} already defined" if @members
	@members = items
	self
end

#evaluateObject



210
211
212
213
# File 'lib/caphir/ctype.rb', line 210

def evaluate
	CType.evaluation_error(identifier) unless identifier.empty?
	self
end

#to_def(indent = '') ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/caphir/ctype.rb', line 223

def to_def(indent='')
	str = to_s
	str << " {\n"
	@members.each do |name, num|
		str << indent << "\t" << name
		str << " = #{num.to_s}" if num
		str << ",\n"
	end
	str[-2, 2] = "\n" # get rid of trailing comma
	str << indent << "}"
end

#to_init_s(ident = nil) ⇒ Object



217
218
219
220
221
222
# File 'lib/caphir/ctype.rb', line 217

def to_init_s(ident=nil)
	str = 'enum'
	str << ' ' << @name if @name
	str << ' ' << ident if ident
	str
end

#to_sObject



214
215
216
# File 'lib/caphir/ctype.rb', line 214

def to_s
	to_init_s()
end