Class: GoodGuide::Gibbon::Program
- Inherits:
-
Object
- Object
- GoodGuide::Gibbon::Program
- Defined in:
- lib/goodguide/gibbon.rb
Instance Attribute Summary collapse
-
#compiled ⇒ Object
readonly
Returns the value of attribute compiled.
-
#syntax ⇒ Object
readonly
Returns the value of attribute syntax.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #call(runtime_client, entity_id) ⇒ Object
-
#initialize(info = {}) ⇒ Program
constructor
A new instance of Program.
- #semantics ⇒ Object
Constructor Details
#initialize(info = {}) ⇒ Program
241 242 243 244 245 |
# File 'lib/goodguide/gibbon.rb', line 241 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
#compiled ⇒ Object (readonly)
Returns the value of attribute compiled.
236 237 238 |
# File 'lib/goodguide/gibbon.rb', line 236 def compiled @compiled end |
#syntax ⇒ Object (readonly)
Returns the value of attribute syntax.
236 237 238 |
# File 'lib/goodguide/gibbon.rb', line 236 def syntax @syntax end |
Class Method Details
.compile(source, info = {}) ⇒ Object
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/goodguide/gibbon.rb', line 216 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) semantics, errors = context.analyze(syntax, global, client.to_js) 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
257 258 259 260 261 262 263 |
# File 'lib/goodguide/gibbon.rb', line 257 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_json ⇒ Object
247 248 249 250 251 252 253 254 255 |
# File 'lib/goodguide/gibbon.rb', line 247 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
265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/goodguide/gibbon.rb', line 265 def call(runtime_client, entity_id) values, error = @context.run_compiled(@compiled, entity_id, runtime_client.to_js) case error when String raise GoodGuide::Gibbon::Failure.new(error) when nil values else raise error end end |
#semantics ⇒ Object
237 238 239 |
# File 'lib/goodguide/gibbon.rb', line 237 def semantics as_json['compiled']['semantics'] end |