Class: Duby::Compiler::JavaSource::SimpleWhileLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/duby/jvm/source_generator/loops.rb

Direct Known Subclasses

ComplexWhileLoop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loop, compiler) ⇒ SimpleWhileLoop

Returns a new instance of SimpleWhileLoop.



4
5
6
7
# File 'lib/duby/jvm/source_generator/loops.rb', line 4

def initialize(loop, compiler)
  @loop = loop
  @compiler = compiler
end

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler.



3
4
5
# File 'lib/duby/jvm/source_generator/loops.rb', line 3

def compiler
  @compiler
end

#loopObject (readonly)

Returns the value of attribute loop.



3
4
5
# File 'lib/duby/jvm/source_generator/loops.rb', line 3

def loop
  @loop
end

Instance Method Details

#breakObject



9
10
11
# File 'lib/duby/jvm/source_generator/loops.rb', line 9

def break
  compiler.method.puts "break;"
end

#compile(expression) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/duby/jvm/source_generator/loops.rb', line 21

def compile(expression)
  prepare
  @loop.init.compile(compiler, false) if @loop.init?
  @start.call
  compiler.method.block do
    @loop.pre.compile(compiler, false) if @loop.pre?
    compile_body
    @loop.post.compile(compiler, false) if @loop.post?
  end
  if @end_check
    @end_check.call
    compiler.method.puts ';'
  end
  if expression
    compiler.method.puts "#{compiler.lvalue}null;"
  end
end

#compile_bodyObject



39
40
41
# File 'lib/duby/jvm/source_generator/loops.rb', line 39

def compile_body
  loop.body.compile(compiler, false)
end

#nextObject



13
14
15
# File 'lib/duby/jvm/source_generator/loops.rb', line 13

def next
  compiler.method.puts "continue;"
end

#prepareObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/duby/jvm/source_generator/loops.rb', line 43

def prepare
  predicate = loop.condition.predicate.precompile(compiler)
  negative = loop.negative ? '!' : ''
  check = lambda do
    compiler.method.print "while (#{negative}"
    predicate.compile(compiler, true)
    compiler.method.print ')'
  end
  if loop.check_first
    @start = check
  else
    @start = lambda {compiler.method.print 'do'}
    @end_check = check
  end
end

#redoObject



17
18
19
# File 'lib/duby/jvm/source_generator/loops.rb', line 17

def redo
  raise "#{self.class.name} doesn't support redo"
end