Class: Nydp::Cond

Inherits:
Object show all
Extended by:
Helper
Includes:
Helper
Defined in:
lib/nydp/cond.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper

cons, list, literal?, pair?, sig, sym, sym?

Methods included from Converter

#n2r, #r2n

Constructor Details

#initialize(cond, when_true, when_false) ⇒ Cond

Returns a new instance of Cond.



31
32
33
# File 'lib/nydp/cond.rb', line 31

def initialize cond, when_true, when_false
  @condition, @conditional = cond, cons(ExecuteConditionalInstruction.new(when_true, when_false))
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



29
30
31
# File 'lib/nydp/cond.rb', line 29

def condition
  @condition
end

#conditionalObject (readonly)

Returns the value of attribute conditional.



29
30
31
# File 'lib/nydp/cond.rb', line 29

def conditional
  @conditional
end

Class Method Details

.build(expressions, bindings) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nydp/cond.rb', line 52

def self.build expressions, bindings
  if expressions.is_a? Nydp::Pair
    cond       = Compiler.compile expressions.car, bindings
    when_true  = Compiler.compile expressions.cdr.car, bindings
    when_false = Compiler.compile expressions.cdr.cdr.car, bindings
    csig       = sig(cond)
    # puts cond_sig
    # TODO : handle literal nil explicitly (if x y) -> #when_false is literal nil, we can hardcode that
    # todo : handle "OR" explicitly -> (if x x y) -> when #cond equals #when_true, hardcode this case
    case csig
    when "LEX"
      Cond_LEX.build(cond, when_true, when_false)
    when "SYM"
      Cond_SYM.new(cond, cons(when_true), cons(when_false))
    else
      new(cond, when_true, when_false)
    end
  else
    raise "can't compile Cond: #{expressions.inspect}"
  end
end

Instance Method Details

#execute(vm) ⇒ Object



39
40
41
42
# File 'lib/nydp/cond.rb', line 39

def execute vm
  vm.push_ctx_instructions conditional
  condition.execute vm
end

#inspectObject



44
45
46
# File 'lib/nydp/cond.rb', line 44

def inspect
  "cond:#{condition.inspect}:#{conditional.inspect}"
end

#lexical_reach(n) ⇒ Object



35
36
37
# File 'lib/nydp/cond.rb', line 35

def lexical_reach n
  [@condition.lexical_reach(n), @conditional.car.lexical_reach(n)].max
end

#to_sObject



48
49
50
# File 'lib/nydp/cond.rb', line 48

def to_s
  "(cond #{condition.to_s} #{conditional.to_s})"
end