Module: Kumi::IR::Testing::SnastFactory

Defined in:
lib/kumi/ir/testing/snast_factory.rb

Defined Under Namespace

Classes: ModuleBuilder

Class Method Summary collapse

Class Method Details

.build {|builder| ... } ⇒ Object

Yields:

  • (builder)


9
10
11
12
13
# File 'lib/kumi/ir/testing/snast_factory.rb', line 9

def build
  builder = ModuleBuilder.new
  yield builder
  builder.build
end

.call(fn:, args:, axes: [], dtype: nil, loc: nil, opts: {}, meta: {}, id: nil) ⇒ Object



71
72
73
74
# File 'lib/kumi/ir/testing/snast_factory.rb', line 71

def call(fn:, args:, axes: [], dtype: nil, loc: nil, opts: {}, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Call.new(fn:, args:, opts:, loc:, meta: node_meta, id:)
end

.const(value, dtype:, axes: [], loc: nil, meta: {}, id: nil) ⇒ Object



34
35
36
37
# File 'lib/kumi/ir/testing/snast_factory.rb', line 34

def const(value, dtype:, axes: [], loc: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Const.new(value:, loc:, meta: node_meta, id:)
end

.declaration(name, body:, kind: :value, axes: [], dtype: nil, meta: {}, loc: nil, id: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kumi/ir/testing/snast_factory.rb', line 20

def declaration(name, body:, kind: :value, axes: [], dtype: nil, meta: {}, loc: nil, id: nil)
  declarative_meta = meta.dup
  declarative_meta[:kind] ||= kind
  declarative_meta[:stamp] ||= stamp(axes:, dtype:)

  Kumi::Core::NAST::Declaration.new(
    id: id,
    name: name,
    body:,
    loc: loc,
    meta: declarative_meta
  )
end

.ensure_declaration(val) ⇒ Object

Raises:

  • (ArgumentError)


121
122
123
124
125
# File 'lib/kumi/ir/testing/snast_factory.rb', line 121

def ensure_declaration(val)
  return val if val.is_a?(Kumi::Core::NAST::Declaration)

  raise ArgumentError, "expected NAST::Declaration, got #{val.class}"
end

.fold(fn:, arg:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil) ⇒ Object



81
82
83
84
# File 'lib/kumi/ir/testing/snast_factory.rb', line 81

def fold(fn:, arg:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Fold.new(fn:, arg:, loc:, meta: node_meta, id:)
end

.hash(pairs:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil) ⇒ Object



61
62
63
64
# File 'lib/kumi/ir/testing/snast_factory.rb', line 61

def hash(pairs:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Hash.new(pairs:, loc:, meta: node_meta, id:)
end

.index_ref(name:, input_fqn:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil) ⇒ Object



91
92
93
94
# File 'lib/kumi/ir/testing/snast_factory.rb', line 91

def index_ref(name:, input_fqn:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::IndexRef.new(name:, input_fqn:, loc:, meta: node_meta, id:)
end

.input_ref(path:, axes:, dtype:, key_chain: [], loc: nil, id: nil, fqn: nil, meta: {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kumi/ir/testing/snast_factory.rb', line 39

def input_ref(path:, axes:, dtype:, key_chain: [], loc: nil, id: nil, fqn: nil, meta: {})
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::InputRef.new(
    path: Array(path),
    fqn: fqn,
    key_chain: key_chain,
    loc:,
    meta: node_meta,
    id:
  )
end

.merge_stamp(meta, axes:, dtype:) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/kumi/ir/testing/snast_factory.rb', line 105

def merge_stamp(meta, axes:, dtype:)
  out = meta.dup
  s = stamp(axes:, dtype:)
  if s
    out[:stamp] = (out[:stamp] || {}).merge(s)
  end
  out
end

.normalize_dtype(dtype) ⇒ Object



114
115
116
117
118
119
# File 'lib/kumi/ir/testing/snast_factory.rb', line 114

def normalize_dtype(dtype)
  return nil if dtype.nil?
  return dtype if dtype.is_a?(Kumi::Core::Types::Type)

  Kumi::Core::Types.normalize(dtype)
end

.pair(key:, value:, axes: [], dtype: nil, meta: {}, id: nil) ⇒ Object



66
67
68
69
# File 'lib/kumi/ir/testing/snast_factory.rb', line 66

def pair(key:, value:, axes: [], dtype: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Pair.new(key:, value:, meta: node_meta, id:)
end

.reduce(fn:, arg:, over:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil) ⇒ Object



86
87
88
89
# File 'lib/kumi/ir/testing/snast_factory.rb', line 86

def reduce(fn:, arg:, over:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Reduce.new(fn:, arg:, over:, loc:, meta: node_meta, id:)
end

.ref(name:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil) ⇒ Object



51
52
53
54
# File 'lib/kumi/ir/testing/snast_factory.rb', line 51

def ref(name:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Ref.new(name:, loc:, meta: node_meta, id:)
end

.select(cond:, on_true:, on_false:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil) ⇒ Object



76
77
78
79
# File 'lib/kumi/ir/testing/snast_factory.rb', line 76

def select(cond:, on_true:, on_false:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Select.new(cond:, on_true:, on_false:, loc:, meta: node_meta, id:)
end

.snast_module(declarations = {}) ⇒ Object



15
16
17
18
# File 'lib/kumi/ir/testing/snast_factory.rb', line 15

def snast_module(declarations = {})
  decls = declarations.transform_values { ensure_declaration(_1) }
  Kumi::Core::NAST::Module.new(decls:)
end

.stamp(axes:, dtype:) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/kumi/ir/testing/snast_factory.rb', line 96

def stamp(axes:, dtype:)
  return nil if axes.nil? && dtype.nil?

  {
    axes: Array(axes || []),
    dtype: normalize_dtype(dtype)
  }
end

.tuple(args:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil) ⇒ Object



56
57
58
59
# File 'lib/kumi/ir/testing/snast_factory.rb', line 56

def tuple(args:, axes: [], dtype: nil, loc: nil, meta: {}, id: nil)
  node_meta = merge_stamp(meta, axes:, dtype:)
  Kumi::Core::NAST::Tuple.new(args:, loc:, meta: node_meta, id:)
end