Class: Jade::Frontend::PatternAnalysis::Matrix

Inherits:
Data
  • Object
show all
Defined in:
lib/jade/frontend/pattern_analysis/matrix.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows

Returns:

  • (Object)

    the current value of rows



4
5
6
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 4

def rows
  @rows
end

#typesObject (readonly)

Returns the value of attribute types

Returns:

  • (Object)

    the current value of types



4
5
6
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 4

def types
  @types
end

Class Method Details

.empty(types = []) ⇒ Object



12
13
14
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 12

def self.empty(types = [])
  self.new([], types)
end

.wildcard(types) ⇒ Object



5
6
7
8
9
10
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 5

def self.wildcard(types)
  types
    .map { Wildcard[] }
    .then { [it] }
    .then { Matrix[it, types] }
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 34

def any?
  rows.any?
end

#concat(other) ⇒ Object



38
39
40
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 38

def concat(other)
  with(rows: rows + other.rows)
end

#empty?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 30

def empty?
  rows.empty?
end

#include?(row) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 22

def include?(row)
  rows.include?(row)
end

#mapObject



16
17
18
19
20
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 16

def map
  rows
    .map { yield it }
    .then { with(rows: it) }
end

#missing_patterns(env, seen_recursive_types = ::Set.new) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 42

def missing_patterns(env, seen_recursive_types = ::Set.new)
  if types.empty?
    return rows.empty? ? Matrix[[[]], types] : Matrix.empty
  end

  type = types.first

  return Matrix.empty if never?(type)
  return Matrix.wildcard(types) if rows.empty?

  if infinite?(type) || type_var?(type) || seen_recursive_types.include?(type)
    matrix = default
      .missing_patterns(env, seen_recursive_types)

    matrix
      .map { [Wildcard[]] + it }

  elsif expandable?(type, env)
    expand(env).missing_patterns(env, seen_recursive_types)

  else
    new_seen = seen_recursive_types | ::Set[type]
    constructors_of(type, env)
      .reduce(Matrix.empty.with(types:)) do |acc, constructor|
        missing = specialize(constructor)
          .missing_patterns(env, new_seen)

        missing
          .map do |row|
            new_args = row.take(constructor.args.size)
            tail = row.drop(constructor.args.size)

          [Constructor[constructor.name, new_args]] + tail
        end
        .then { acc.concat(it) }
      end
  end
end

#sizeObject



26
27
28
# File 'lib/jade/frontend/pattern_analysis/matrix.rb', line 26

def size
  rows.size
end