Module: Jade::Stdlib::Intrinsics

Included in:
Basics, Bytes, Char, Debug, Decode, Dict, Encode, List, Set, Show, String, Task, Tuple
Defined in:
lib/jade/stdlib/intrinsics.rb

Instance Method Summary collapse

Instance Method Details

#default_implementation(params:, body:) ⇒ Object



220
221
222
# File 'lib/jade/stdlib/intrinsics.rb', line 220

def default_implementation(params:, body:)
  Symbol::StdlibImplementation[params, body]
end

#default_importing(imports) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/jade/stdlib/intrinsics.rb', line 208

def default_importing(imports)
  @default_imports = if imports == :*
    exposes
  else
    exposes.select { imports.include? it.name }
  end
end

#default_importsObject



216
217
218
# File 'lib/jade/stdlib/intrinsics.rb', line 216

def default_imports
  @default_imports || {}
end

#derive_runtime(qualified_fn_name, arity) ⇒ Object

Evaluated at the top level — the same constant-resolution context the template lands in when codegen splices it into compiled output — so a bare String means ::String, not Jade::Stdlib::String. Constants resolve at call time, so there's no stdlib load-order constraint.



111
112
113
114
115
116
117
118
# File 'lib/jade/stdlib/intrinsics.rb', line 111

def derive_runtime(qualified_fn_name, arity)
  Codegen::Inlines.for(qualified_fn_name).then do |template|
    next nil unless template

    args = Array.new(arity) { "a#{it}" }
    eval("->(#{args.join(', ')}) { #{template.call(*args)} }", TOPLEVEL_BINDING)
  end
end

#entryObject



191
192
193
194
195
196
197
198
# File 'lib/jade/stdlib/intrinsics.rb', line 191

def entry
  @entry || symbols
    .reduce(Registry.entry(module_name)) do |acc, sym|
      acc.define(sym)
    end
    .with(exposes:)
    .then { resolve_imports(it) }
end

#function(name, params, ret, constraints: [], body: nil, private: false, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jade/stdlib/intrinsics.rb', line 64

def function(name, params, ret, constraints: [], body: nil, private: false, &block)
  qualified_fn_name = "#{module_name}.#{name}"

  codegen = body || "Jade::Runtime.intr('#{qualified_fn_name}')"
  impl = runtime_impl(qualified_fn_name, params.size, block, body)

  Symbol
    .stdlib_function(
      name.to_s,
      params.transform_values { Symbol.parse(it) },
      Symbol.parse(ret),
      codegen,
      constraints:,
    )
    .with(module_name:)
    .tap { store(it) }
    .tap { private_names << name.to_s if private }
    .tap { Runtime.register(qualified_fn_name, &impl) }
end

#generate_entry(registry) ⇒ Object



8
9
10
11
# File 'lib/jade/stdlib/intrinsics.rb', line 8

def generate_entry(registry)
  @entry = entry
    .then { load_env(it, registry) }
end

#impl_type_ref(type) ⇒ Object

Accepts both bare/qualified names ('Int', 'Basics.Int') and full type expressions with vars ('Maybe(a)', 'Result(a, e)'). The impl record stores only the constructor ref (matching the forward-declaration convention at frontend/forward_declaration/implementation.rb:44).



182
183
184
185
# File 'lib/jade/stdlib/intrinsics.rb', line 182

def impl_type_ref(type)
  Symbol.parse(type) => Symbol::TypeApplication(constructor:)
  constructor
end

#implementation(interface_name, type, functions) ⇒ Object



146
147
148
149
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
# File 'lib/jade/stdlib/intrinsics.rb', line 146

def implementation(interface_name, type, functions)
  interface = imports
    .map(&:symbols)
    .then { [symbols, *it] }
    .flatten
    .find { it.is_a?(Symbol::Interface) && it.name == interface_name }

  interface_ref = interface ? interface.to_ref : interface_to_ref(interface_name)
  default = interface ? interface.default : {}
  type_ref = impl_type_ref(type)

  Symbol
    .implementation(
      interface_ref,
      type_ref,
      [],
      [],
      functions
        .transform_values { Symbol.value_ref(module_name, it) }
        .merge(default),
      [],
      nil,
    )
    .then { store(it) }

  if (ruby_classes = @native_types&.[](type))
    qualified_iface = "#{interface_to_ref(interface_name).module_name}.#{interface_name}"
    qualified_fns   = functions.transform_values { "#{module_name}.#{it}" }
    ruby_classes.each { Runtime.register_impl(qualified_iface, it, qualified_fns) }
  end
end

#import(module_name) ⇒ Object



200
201
202
# File 'lib/jade/stdlib/intrinsics.rb', line 200

def import(module_name)
  @imports = imports + [module_name]
end

#importsObject



204
205
206
# File 'lib/jade/stdlib/intrinsics.rb', line 204

def imports
  @imports || []
end

#interface(name, type_param, functions, default: {}) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/jade/stdlib/intrinsics.rb', line 120

def interface(name, type_param, functions, default: {})
  functions
    .map { |k, v| Symbol.parse(v).then { to_interface_function(name, k, it) }.with(module_name:) }
    .then { Symbol.interface(name, Symbol.parse(type_param), it, default, nil) }
    .with(module_name:)
    .then { store(it); it.functions.each { |fn| store(fn) } }
end

#native_type(jade_type_name, *ruby_classes) ⇒ Object

Declares that a Jade type is backed by one or more native Ruby classes. This auto-registers runtime dispatch for every implementation declared after this call, so there's no need for manual Runtime.register_impl calls.

Future direction: literals should eventually be wrapped in Jade's own Data.define types (e.g. Int[value: 42]) and only unwrapped at interop boundaries. That would make native_type unnecessary — implementation declarations would cover both compile-time and runtime dispatch on their own. It also opens up richer type definitions like:

Int   = InternalInt | Overflow
Float = InternalFloat | NaN | Infinity | NegInfinity

See Char for a preview of this direction: it's a distinct type from String even though it's still backed by a Ruby String at runtime.



141
142
143
144
# File 'lib/jade/stdlib/intrinsics.rb', line 141

def native_type(jade_type_name, *ruby_classes)
  @native_types ||= {}
  @native_types[jade_type_name.to_s] = ruby_classes
end

#private_namesObject

Private means defined but not exposed: codegen and implementation resolve it, user modules cannot name it.



86
87
88
# File 'lib/jade/stdlib/intrinsics.rb', line 86

def private_names
  @private_names ||= []
end

#runtime_impl(qualified_fn_name, arity, block, body) ⇒ Object

Approach A: the inline template is the single source of truth for a function's behaviour. A hand-written block always wins. With no block and a body: codegen override the function is codegen/dictionary-only (e.g. derived comparisons) and has no runtime entry. Otherwise the runtime proc is synthesised from the same template codegen emits, so the compiled and interpreted paths can't drift — and the absence of both a block and an inline is a definition error, not a lazy runtime surprise.



97
98
99
100
101
102
103
104
105
# File 'lib/jade/stdlib/intrinsics.rb', line 97

def runtime_impl(qualified_fn_name, arity, block, body)
  case [block, body]
  in [Proc => block, _] then block
  in [nil, nil]
    derive_runtime(qualified_fn_name, arity) ||
      fail("#{qualified_fn_name}: no runtime block and no inline to derive from")
  else nil
  end
end

#symbolsObject



187
188
189
# File 'lib/jade/stdlib/intrinsics.rb', line 187

def symbols
  @symbols || []
end

#union(name, *type_params, constructor: false) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/jade/stdlib/intrinsics.rb', line 46

def union(name, *type_params, constructor: false)
  union_symbol = Symbol
    .union(name.to_s, type_params.map { Symbol.var(it, nil) }, [], nil)
    .with(module_name:)
    .tap { store(it) }

  constructor_symbol = if constructor
    Symbol.constructor(
      name.to_s,
      type_params.map { Symbol.var(it, nil) },
      union_symbol,
      nil,
    )
      .with(module_name:)
      .then { store(it) }
  end
end

#variant(name, of:, args: []) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jade/stdlib/intrinsics.rb', line 13

def variant(name, of:, args: [])
  parent = @symbols
    &.find { it.is_a?(Symbol::Union) && it.name == of.to_s }

  union_ref = Symbol.type_ref(module_name, of.to_s)
  parsed_args = args.map { Symbol.parse(it) }

  variant_sym = Symbol::Variant.new(
    module_name:,
    name:        name.to_s,
    args:        parsed_args,
    union:       union_ref,
    decl_span:   nil,
  )

  constructor_sym = Symbol::Constructor.new(
    module_name:,
    name:        name.to_s,
    args:        parsed_args,
    parent:      union_ref,
    decl_span:   nil,
  )

  store(variant_sym)
  store(constructor_sym)

  if parent
    parent
      .with(variants: parent.variants + [variant_sym.to_ref])
      .then { @symbols[@symbols.index(parent)] = it }
  end
end