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
Returns a new instance of Program.
216 217 218 219 220 |
# File 'lib/goodguide/gibbon.rb', line 216 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.
211 212 213 |
# File 'lib/goodguide/gibbon.rb', line 211 def compiled @compiled end |
#syntax ⇒ Object (readonly)
Returns the value of attribute syntax.
211 212 213 |
# File 'lib/goodguide/gibbon.rb', line 211 def syntax @syntax end |
Class Method Details
.compile(source, info = {}) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/goodguide/gibbon.rb', line 191 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) if errors compiled = context.compile(semantics) Program.new( :syntax => syntax, :compiled => compiled, :context => context, ) end |
.from_json(hash, context) ⇒ Object
229 230 231 232 233 234 235 |
# File 'lib/goodguide/gibbon.rb', line 229 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
222 223 224 225 226 227 |
# File 'lib/goodguide/gibbon.rb', line 222 def as_json @as_json ||= { 'syntax' => @context.send(:obj_to_ruby, @syntax), 'compiled' => @context.send(:obj_to_ruby, @compiled), } end |
#call(runtime_client, entity_id) ⇒ Object
237 238 239 240 241 242 |
# File 'lib/goodguide/gibbon.rb', line 237 def call(runtime_client, entity_id) values, error = @context.run_compiled(@compiled, entity_id, runtime_client.to_js) raise error if error values end |
#semantics ⇒ Object
212 213 214 |
# File 'lib/goodguide/gibbon.rb', line 212 def semantics as_json['compiled']['semantics'] end |