Class: GoodGuide::Gibbon::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/goodguide/gibbon.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info = {}) ⇒ Program

Returns a new instance of Program.



257
258
259
260
261
# File 'lib/goodguide/gibbon.rb', line 257

def initialize(info={})
  @syntax = info[:syntax] or raise 'please pass syntax'
  @compiled = info[:compiled] or raise 'please pass a compiled program'
  @context = info[:context] || Context.new
end

Instance Attribute Details

#compiledObject (readonly)

Returns the value of attribute compiled.



252
253
254
# File 'lib/goodguide/gibbon.rb', line 252

def compiled
  @compiled
end

#syntaxObject (readonly)

Returns the value of attribute syntax.



252
253
254
# File 'lib/goodguide/gibbon.rb', line 252

def syntax
  @syntax
end

Class Method Details

.compile(source, info = {}) ⇒ Object

Raises:



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/goodguide/gibbon.rb', line 232

def self.compile(source, info={})
  client = info[:client] || raise('please pass a static client')
  global = info[:global] || raise('please pass a global table id')

  context = info[:context] || Context.new

  syntax = context.parse(source)
  errors, semantics = context.analyze(syntax, global, client.to_js(context.gibbon))

  raise SemanticError.new(errors, context) if errors

  compiled = context.compile(semantics)

  Program.new(
    :syntax => syntax,
    :compiled => compiled,
    :context => context,
  )
end

.from_json(hash, context) ⇒ Object



273
274
275
276
277
278
279
# File 'lib/goodguide/gibbon.rb', line 273

def self.from_json(hash, context)
  syntax = context.send(:obj_to_js, hash['syntax'])
  semantics = context.send(:obj_to_js, hash['compiled']['semantics'])
  blocks = context.send(:obj_to_js, hash['compiled']['blocks'])
  compiled = context.gibbon[:CompiledCode].new(semantics, blocks)
  new(syntax: syntax, compiled: compiled, context: context)
end

Instance Method Details

#as_jsonObject



263
264
265
266
267
268
269
270
271
# File 'lib/goodguide/gibbon.rb', line 263

def as_json
  @as_json ||= {
    'syntax' => @context.send(:obj_to_ruby, @syntax),
    'compiled' => {
      'semantics' => @context.send(:obj_to_ruby, @compiled.semantics),
      'blocks' => @context.send(:obj_to_ruby, @compiled.blocks)
    }
  }
end

#call(runtime_client, entity_id) ⇒ Object



281
282
283
# File 'lib/goodguide/gibbon.rb', line 281

def call(runtime_client, entity_id)
  @context.run_compiled(@compiled, entity_id, runtime_client.to_js(@context.gibbon))
end

#semanticsObject



253
254
255
# File 'lib/goodguide/gibbon.rb', line 253

def semantics
  as_json['compiled']['semantics']
end