Class: Kumi::Syntax::Root

Inherits:
Struct
  • Object
show all
Includes:
Node
Defined in:
lib/kumi/syntax/root.rb

Overview

Represents the root of the Abstract Syntax Tree. It holds all the top-level declarations parsed from the source.

Instance Attribute Summary collapse

Attributes included from Node

#hints, #loc

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Node

#==, #initialize

Instance Attribute Details

#importsObject

Returns the value of attribute imports

Returns:

  • (Object)

    the current value of imports



7
8
9
# File 'lib/kumi/syntax/root.rb', line 7

def imports
  @imports
end

#inputsObject

Returns the value of attribute inputs

Returns:

  • (Object)

    the current value of inputs



7
8
9
# File 'lib/kumi/syntax/root.rb', line 7

def inputs
  @inputs
end

#traitsObject

Returns the value of attribute traits

Returns:

  • (Object)

    the current value of traits



7
8
9
# File 'lib/kumi/syntax/root.rb', line 7

def traits
  @traits
end

#valuesObject

Returns the value of attribute values

Returns:

  • (Object)

    the current value of values



7
8
9
# File 'lib/kumi/syntax/root.rb', line 7

def values
  @values
end

Class Method Details

.stable_encode(value) ⇒ Object

Recursively encode an AST fragment into a string whose format does not depend on the Ruby version (unlike ‘inspect`/`to_s`). Structs are encoded by class name + ordered members; collections and scalars by an explicit tagged form. `loc` is intentionally excluded — it does not affect codegen.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kumi/syntax/root.rb', line 34

def self.stable_encode(value)
  case value
  when Kumi::Syntax::Node
    members = value.respond_to?(:members) ? value.members : []
    fields = members.map { |m| "#{m}=#{stable_encode(value[m])}" }
    "#{value.class.name}(#{fields.join(',')};hints=#{stable_encode(value.hints)})"
  when Array
    "[#{value.map { |v| stable_encode(v) }.join(',')}]"
  when Hash
    pairs = value.map { |k, v| "#{stable_encode(k)}=>#{stable_encode(v)}" }.sort
    "{#{pairs.join(',')}}"
  when Symbol
    ":#{value}"
  when String
    value.dump
  else
    value.to_s
  end
end

Instance Method Details

#childrenObject



10
# File 'lib/kumi/syntax/root.rb', line 10

def children = [inputs, values, traits, imports]

#digestObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kumi/syntax/root.rb', line 12

def digest
  # The digest must be stable and depend on anything that could change the
  # compiled output: the AST and the Kumi version (compiler changes). It is
  # deliberately Ruby-version-independent — the generated code is plain Ruby
  # with identical semantics across supported Rubies, so a version-sensitive
  # digest would needlessly bust the compile cache and make the codegen
  # goldens unverifiable across the CI Ruby matrix.
  #
  # The AST is serialized through a format-pinned encoder rather than
  # `inspect`/`to_s`, because `Struct#to_s` and `Hash#inspect` changed their
  # rendering in Ruby 3.4 (`{:a=>1}` → `{a: 1}`), which would otherwise leak
  # the Ruby version into the hash.
  digest_input = "#{Kumi::VERSION}-#{Digest::SHA256.hexdigest(self.class.stable_encode([self, hints]))}"

  # Ruby constants cannot start with a number, so we add a prefix.
  "KUMI_#{Digest::SHA256.hexdigest(digest_input)}"
end