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, #rubify

Constructor Details

#initialize(cond, when_true, when_false) ⇒ Cond

Returns a new instance of Cond.



7
8
9
# File 'lib/nydp/cond.rb', line 7

def initialize cond, when_true, when_false
  @condition, @when_true, @when_false = cond, when_true, when_false
end

Instance Attribute Details

#conditionObject (readonly)

Returns the value of attribute condition.



5
6
7
# File 'lib/nydp/cond.rb', line 5

def condition
  @condition
end

#conditionalObject (readonly)

Returns the value of attribute conditional.



5
6
7
# File 'lib/nydp/cond.rb', line 5

def conditional
  @conditional
end

Class Method Details

.build(expressions, bindings, ns) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nydp/cond.rb', line 47

def self.build expressions, bindings, ns
  if expressions.is_a? Nydp::Pair
    cond       = Compiler.compile expressions.car, bindings, ns
    when_true  = Compiler.compile expressions.cdr.car, bindings, ns
    when_false = Compiler.compile expressions.cdr.cdr.car, bindings, ns
    csig       = sig(cond)
    xsig       = "#{sig(cond)}_#{sig(when_true)}_#{sig(when_false)}"

    # case csig
    # when "LEX"
    #   Cond_LEX.build(cond, when_true, when_false)
    # when "SYM"
    #   Cond_SYM.new(cond, when_true, when_false)
    # else
    #   new(cond, when_true, when_false)
    # end

    new(cond, when_true, when_false)
  else
    raise "can't compile Cond: #{expressions._nydp_inspect}"
  end
end

Instance Method Details

#compile_to_ruby(indent, srcs, opts = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/nydp/cond.rb', line 23

def compile_to_ruby indent, srcs, opts=nil
  if (!@when_false) || (@when_false.is_a?(Nydp::Literal) && !@when_false.expression)
    "#{indent}##> #{inspect.split(/\n/).join('\n')}
#{indent}if (#{@condition.compile_to_ruby "", srcs})
#{@when_true.compile_to_ruby(indent + "  ", srcs, cando: true)}
#{indent}end"
  else
    "#{indent}##> #{inspect.split(/\n/).join('\n')}
#{indent}if (#{@condition.compile_to_ruby "", srcs})
#{@when_true.compile_to_ruby(indent + "  ", srcs, cando: true)}
#{indent}else
#{@when_false.compile_to_ruby(indent + "  ", srcs, cando: true)}
#{indent}end"
  end
end

#execute(vm) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/nydp/cond.rb', line 15

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

#inspectObject



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

def inspect
  "(cond #{condition._nydp_inspect} #{@when_true._nydp_inspect} #{@when_false._nydp_inspect})"
end

#lexical_reach(n) ⇒ Object



11
12
13
# File 'lib/nydp/cond.rb', line 11

def lexical_reach n
  [@condition.lexical_reach(n), @when_true.lexical_reach(n), @when_false.lexical_reach(n)].max
end

#to_sObject



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

def to_s
  "(cond #{condition.to_s} #{@when_true.to_s} #{@when_false.to_s})"
end