Class: Nydp::Cond_LEX

Inherits:
CondBase show all
Defined in:
lib/nydp/cond.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CondBase

#initialize, #inspect, #to_s

Methods included from Helper

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

Methods included from Converter

#n2r, #r2n, #rubify

Constructor Details

This class inherits a constructor from Nydp::CondBase

Class Method Details

.build(cond, when_true, when_false) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/nydp/cond.rb', line 107

def self.build cond, when_true, when_false
  tsig       = sig(when_true)
  fsig       = sig(when_false)
  cond_sig = "#{tsig}_#{fsig}"

  if (cond == when_true) && (fsig == "LIT")
    OR_LEX_LIT.new cond, nil, when_false.expression
  elsif (cond == when_true) && (fsig == "LEX")
    OR_LEX_LEX.new cond, nil, when_false
  elsif (cond == when_true)
    OR_LEX_XXX.new cond, nil, when_false
  else
    case cond_sig
    when "LIT_LIT"
      # puts "building Cond_LEX_LIT_LIT #{[cond, when_true.expression, when_false.expression]}"
      Nydp::Cond_LEX_LIT_LIT.new(cond, when_true.expression, when_false.expression)
    when "LEX_LIT"
      Nydp::Cond_LEX_LEX_LIT.new(cond, when_true, when_false.expression)
    when "CND_LIT"
      Nydp::Cond_LEX_CND_LIT.new(cond, when_true, when_false.expression)
    when "NVB_LEX"
      Nydp::Cond_LEX_NVB_LEX.new(cond, when_true, when_false)
    when "NVB_LIT"
      Nydp::Cond_LEX_NVB_LIT.new(cond, when_true, when_false.expression)
    else
      new(cond, when_true, when_false)
    end
  end
end

Instance Method Details

#execute(vm) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/nydp/cond.rb', line 91

def execute vm
  if @condition.value vm.current_context
    @when_true.execute(vm)
  else
    @when_false.execute(vm)
  end
end

#lexical_reach(n) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/nydp/cond.rb', line 99

def lexical_reach n
  cr = @condition.lexical_reach(n)
  ct = @when_true.lexical_reach(n)
  cf = @when_false.lexical_reach(n)

  [cr, ct, cf].max
end