Class: WType

Inherits:
Object show all
Includes:
Log
Defined in:
lib/rlang/parser/wtype.rb

Constant Summary collapse

WASM_TYPE_MAP =
{
  UI64: Type::UI64,
  I64:  Type::I64,
  UI32: Type::UI32,
  I32:  Type::I32,
  F64:  Type::F64,
  F32:  Type::F32
}
CAST_PRECEDENCE =

Implicit Type cast order in decreasing order of precedence Class types have precedence over default integer type because of pointer arithmetics

[:F64, :F32, :UI64, :I64, :Class, :UI32, :I32]
DEFAULT =
self.new(WASM_TYPE_MAP.key(Type::DEFAULT))
UNSIGNED_DEFAULT =
self.new(WASM_TYPE_MAP.key(Type::UNSIGNED_DEFAULT))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

included, logger, #logger, logger=

Constructor Details

#initialize(name) ⇒ WType

Name is a symbol of the form :A or :“A::B” or :“A::B::C” or a string “A” or “A::B” or “A::B::C” or it can also be an array of symbols [:A], [:A, :B] or [:A, :B, :C] (It is not a class object)



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rlang/parser/wtype.rb', line 53

def initialize(name)
  if name.is_a? Symbol
    @name = name
  elsif name.is_a? String
    @name = name.to_sym
  elsif name.is_a? Array
    @name = name.map(&:to_s).join('::').to_sym
  else
    raise "Unknown type for WType name (got #{name}, class: #{name.class}"
  end
  raise "Invalid WType #{name.inspect}" unless self.valid?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



34
35
36
# File 'lib/rlang/parser/wtype.rb', line 34

def name
  @name
end

Class Method Details

.leading(wtypes) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/rlang/parser/wtype.rb', line 40

def self.leading(wtypes)
  logger.debug "wtypes: #{wtypes}"
  # The compact below is to remove the nil 
  # when wtype is blank or class
  leading_idx = wtypes.map {|wt| wt.class? ? :Class : wt.name}. \
    map {|wtn| CAST_PRECEDENCE.index(wtn) }.compact.each_with_index.min.last
  wtypes[leading_idx]
end

.legit?(name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rlang/parser/wtype.rb', line 36

def self.legit?(name)
  WASM_TYPE_MAP.has_key? name
end

Instance Method Details

#==(other) ⇒ Object



96
97
98
# File 'lib/rlang/parser/wtype.rb', line 96

def ==(other)
  @name == other.name
end

#blank?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/rlang/parser/wtype.rb', line 87

def blank?
  @name == :none || @name == :nil
end

#class?Boolean

Returns:

  • (Boolean)


91
92
93
94
# File 'lib/rlang/parser/wtype.rb', line 91

def class?
  # name starts with uppercase and is not a native type
  !self.native? && ('A'..'Z').include?(@name.to_s[0])
end

#class_pathObject



66
67
68
69
# File 'lib/rlang/parser/wtype.rb', line 66

def class_path
  return [] if self.blank?
  name.to_s.split('::').map(&:to_sym)
end

#default?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/rlang/parser/wtype.rb', line 71

def default?
  @name == WType::DEFAULT.name
end

#inspectObject



131
132
133
# File 'lib/rlang/parser/wtype.rb', line 131

def inspect
  self.to_s
end

#native?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/rlang/parser/wtype.rb', line 79

def native?
  WASM_TYPE_MAP.has_key? @name
end

#signed?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/rlang/parser/wtype.rb', line 83

def signed?
  self.native? && WASM_TYPE_MAP[@name].signed?
end

#sizeObject



100
101
102
103
104
105
106
107
108
# File 'lib/rlang/parser/wtype.rb', line 100

def size
  if self.blank?
    0
  elsif self.native?
    WASM_TYPE_MAP[@name].size
  else
    Type::DEFAULT.size
  end
end

#to_sObject



127
128
129
# File 'lib/rlang/parser/wtype.rb', line 127

def to_s
  ":#{@name}"
end

#valid?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/rlang/parser/wtype.rb', line 75

def valid?
  self.blank? || self.native? || self.class?
end

#wasm_typeObject

returns a String with the proper WASM type that can be used for code generation



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rlang/parser/wtype.rb', line 112

def wasm_type
  if self.blank?
    ''
  elsif self.native?
    WASM_TYPE_MAP[@name].wasm_type
  elsif self.class?
    # it's a class name return DEFAULT WASM
    # type because this is always the class of
    # amemory address in VASM VM
    Type::DEFAULT.wasm_type
  else
    raise "Unknown WType #{self.inspect}"
  end
end