Class: CType::StructUnion

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

Direct Known Subclasses

Struct, Union

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ StructUnion

Returns a new instance of StructUnion.


80
81
82
83
# File 'lib/caphir/ctype.rb', line 80

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

Class Method Details

.get(type, name, members = nil) ⇒ Object

want only one instance of each unique Struct or Union


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/caphir/ctype.rb', line 64

def StructUnion.get(type, name, members = nil)
	case type
		when 'struct'
			if members
				Struct[name].add(members)
			else
				Struct[name]
			end
		when 'union'
			if members
				Union[name].add(members)
			else
				Union[name]
			end
	end
end

Instance Method Details

#add(items) ⇒ Object


84
85
86
87
88
# File 'lib/caphir/ctype.rb', line 84

def add(items)
	#warn "structure or union #{@name} already defined" if @members
	@members = items.flatten
	self
end

#evaluate(identifier) ⇒ Object


99
100
101
102
103
104
105
106
107
108
109
# File 'lib/caphir/ctype.rb', line 99

def evaluate(identifier)
	return self if identifier.empty?
	CType.evaluation_error(identifier) unless identifier =~ /\A\.([A-Za-z_]\w*)/
	post_match = $'
	p_type = lookup($1)
	if p_type
		p_type.evaluate(post_match)
	else
		CType.evaluation_error(identifier)
	end
end

#get_membersObject


95
96
97
# File 'lib/caphir/ctype.rb', line 95

def get_members
	@members.dup
end

#keywordObject


111
112
113
114
115
116
117
# File 'lib/caphir/ctype.rb', line 111

def keyword
	if self.class == Struct
		'struct'
	elsif self.class == Union
		'union'
	end
end

#lookup(identifier) ⇒ Object


89
90
91
92
93
# File 'lib/caphir/ctype.rb', line 89

def lookup(identifier)
	@members.find do |m|
		identifier == m.identifier
	end
end

#to_def(indent = '') ⇒ Object

to_def is pretty much depreciated - might be useful for debugging in the future to_def methods would have to be added to all classes in this module though.


131
132
133
134
135
136
137
138
# File 'lib/caphir/ctype.rb', line 131

def to_def(indent='')
	str =	to_s
	str << " {\n"
	@members.each do |m|
		str << indent << "\t" << m.to_def(indent + "\t") << ";\n"
	end
	str << indent << '}'
end

#to_init_s(ident = nil) ⇒ Object


121
122
123
124
125
126
127
# File 'lib/caphir/ctype.rb', line 121

def to_init_s(ident = nil)
	str = ''
	str << keyword
	str << ' ' << @name if @name
	str << ' ' << ident if ident
	str # make sure we don't return nil
end

#to_sObject


118
119
120
# File 'lib/caphir/ctype.rb', line 118

def to_s
	to_init_s()
end