Class: RaaP::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/raap/type.rb

Overview

Type.new(“Integer”).pick(size: 10) #=> 2 Type.new(“Symbol”).pick(size: 6) #=> :abcdef Type.new(“Array“).pick(size: 3) #=> [1, 2, 3] Type.new(“Array”) { sized { |size| Array.new(size + 1) { integer.pick(size: size) } } }

Defined Under Namespace

Modules: Arithmetic

Constant Summary collapse

GENERATORS =
{}
SIMPLE_SOURCE =
('a'..'z').to_a << '_'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, range: nil..nil) ⇒ Type

Returns a new instance of Type.



123
124
125
126
127
# File 'lib/raap/type.rb', line 123

def initialize(type, range: nil..nil)
  @type = parse(type)
  @range = range
  @such_that = nil
end

Instance Attribute Details

#rangeObject (readonly)

Returns the value of attribute range.



121
122
123
# File 'lib/raap/type.rb', line 121

def range
  @range
end

#typeObject (readonly)

Returns the value of attribute type.



120
121
122
# File 'lib/raap/type.rb', line 120

def type
  @type
end

Class Method Details

.call_new_from(base, type, size:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/raap/type.rb', line 43

def self.call_new_from(base, type, size:)
  type_name = type.name.absolute!
  definition = RBS.builder.build_singleton(type_name)
  snew = definition.methods[:new]
  if snew
    # class
    rbs_method_type = snew.method_types.sample or raise
    type_params = definition.type_params_decl.dup.concat(rbs_method_type.type_params.drop(definition.type_params_decl.length))
    ts = TypeSubstitution.new(type_params, type.args)
    maped_rbs_method_type = ts.method_type_sub(rbs_method_type)
    method_type = MethodType.new(maped_rbs_method_type)

    begin
      args, kwargs, block = method_type.arguments_to_symbolic_call(size: size)
      [:call, base, :new, args, kwargs, block]
    rescue
      $stderr.puts "Fail with `#{rbs_method_type}`"
      raise
    end
  else
    [:call, Value::Module, :new, [type.to_s], { size: size }, nil]
  end
end

.randomObject



31
32
33
# File 'lib/raap/type.rb', line 31

def self.random
  Type.new("Integer | Float | Rational | Complex | String | Symbol | bool | Encoding | BasicObject | nil | Time")
end

.random_without_basic_objectObject



35
36
37
38
39
40
41
# File 'lib/raap/type.rb', line 35

def self.random_without_basic_object
  random.tap do |r|
    # @type var rtype: ::RBS::Types::Union
    rtype = r.type
    rtype.types.reject! { |t| t.to_s == "BasicObject" }
  end
end

.register(type_name, &block) ⇒ Object

Type.register “::Integer::positive” { sized { |size| size } }

Raises:

  • (ArgumentError)


24
25
26
27
28
29
# File 'lib/raap/type.rb', line 24

def self.register(type_name, &block)
  raise ArgumentError, "block is required" unless block

  type_name = "::#{type_name}" if !type_name.start_with?("::")
  GENERATORS[type_name] = __skip__ = block
end

Instance Method Details

#pick(size: 10) ⇒ Object



142
143
144
# File 'lib/raap/type.rb', line 142

def pick(size: 10)
  to_symbolic_caller(size: size).eval
end

#sized(&block) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/raap/type.rb', line 134

def sized(&block)
  Sized.new(&block).tap do |sized|
    if (s = @such_that)
      sized.such_that(&s)
    end
  end
end

#such_that(&block) ⇒ Object



129
130
131
132
# File 'lib/raap/type.rb', line 129

def such_that(&block)
  @such_that = block
  self
end

#to_symbolic_call(size: 10) ⇒ Object

Raises:

  • (TypeError)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/raap/type.rb', line 150

def to_symbolic_call(size: 10)
  raise TypeError, "size should be Integer" unless size.is_a?(Integer)
  raise ArgumentError, "negative size" if size.negative?

  case type
  when ::RBS::Types::Tuple
    type.types.map { |t| Type.new(t).to_symbolic_call(size: size) }
  when ::RBS::Types::Union
    type.types.sample&.then { |t| Type.new(t).to_symbolic_call(size: size) }
  when ::RBS::Types::Intersection
    if type.free_variables.empty?
      [:call, Value::Intersection, :new, [type.to_s], { size: size }, nil]
    else
      [:call, Value::Intersection, :new, [type], { size: size }, nil]
    end
  when ::RBS::Types::Interface
    if type.free_variables.empty?
      [:call, Value::Interface, :new, [type.to_s], { size: size }, nil]
    else
      [:call, Value::Interface, :new, [type], { size: size }, nil]
    end
  when ::RBS::Types::Variable
    [:call, Value::Variable, :new, [type.to_s], {}, nil]
  when ::RBS::Types::Bases::Void
    [:call, Value::Void, :new, [], {}, nil]
  when ::RBS::Types::Bases::Top
    [:call, Value::Top, :new, [], {}, nil]
  when ::RBS::Types::Bases::Bottom
    [:call, Value::Bottom, :new, [], {}, nil]
  when ::RBS::Types::Optional
    case Random.rand(2)
    in 0 then Type.new(type.type).to_symbolic_call(size: size / 2)
    in 1 then nil
    end
  when ::RBS::Types::Alias
    case gen = GENERATORS[type.name.absolute!.to_s]
    in Proc then instance_exec(&gen)
    in nil then Type.new(RBS.builder.expand_alias2(type.name, type.args)).to_symbolic_call(size: size)
    end
  when ::RBS::Types::Bases::Class
    RaaP.logger.warn("Unresolved `class` type, use Object instead.")
    Object
  when ::RBS::Types::Bases::Instance
    RaaP.logger.warn("Unresolved `instance` type, use Object.new instead.")
    Object.new
  when ::RBS::Types::Bases::Self
    raise "cannot resolve `self` type"
  when ::RBS::Types::ClassSingleton
    Object.const_get(type.name.to_s)
  when ::RBS::Types::ClassInstance
    case gen = GENERATORS[type.name.absolute!.to_s]
    in Proc then pick_by_generator(gen, size: size)
    in nil then to_symbolic_call_from_initialize(type, size: size)
    end
  when ::RBS::Types::Record
    type.fields.transform_values { |t| Type.new(t).to_symbolic_call(size: size / 2) }
  when ::RBS::Types::Proc
    Proc.new { Type.new(type.type.return_type).to_symbolic_call(size: size) }
  when ::RBS::Types::Literal
    type.literal
  when ::RBS::Types::Bases::Bool
    bool
  when ::RBS::Types::Bases::Any
    Type.random.to_symbolic_call(size: size)
  when ::RBS::Types::Bases::Nil
    nil
  else
    raise "not implemented #{type}"
  end
end

#to_symbolic_caller(size: 10) ⇒ Object



146
147
148
# File 'lib/raap/type.rb', line 146

def to_symbolic_caller(size: 10)
  SymbolicCaller.new(to_symbolic_call(size: size))
end