Class: RBlade::CompilesStatements::CompilesLoops

Inherits:
Object
  • Object
show all
Defined in:
lib/rblade/compiler/statements/compiles_loops.rb

Instance Method Summary collapse

Constructor Details

#initializeCompilesLoops

Returns a new instance of CompilesLoops.



4
5
6
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 4

def initialize
  @loop_else_counter = 0
end

Instance Method Details

#compileBreak(args) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 8

def compileBreak args
  if !args.nil?
    raise StandardError.new "Break statement: wrong number of arguments (given #{args&.count}, expecting 0)"
  end

  "break;"
end

#compileBreakIf(args) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 16

def compileBreakIf args
  if args&.count != 1
    raise StandardError.new "Break statement: wrong number of arguments (given #{args&.count}, expecting 1)"
  end

  "if #{args[0]};break;end;"
end

#compileEach(args) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 24

def compileEach args
  if args&.count != 1
    raise StandardError.new "Each statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end

  variables, collection = args[0].split(" in ")

  "#{collection}.each do |#{variables}|;"
end

#compileEachElse(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 34

def compileEachElse args
  if args&.count != 1
    raise StandardError.new "Each else statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end
  @loop_else_counter += 1

  variables, collection = args[0].split(" in ")

  "_looped_#{@loop_else_counter}=true;#{collection}.each do |#{variables}|;_looped_#{@loop_else_counter}=false;"
end

#compileEmptyObject



62
63
64
65
66
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 62

def compileEmpty
  @loop_else_counter -= 1

  "end;if _looped_#{@loop_else_counter + 1};"
end

#compileFor(args) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 45

def compileFor args
  if args&.count != 1
    raise StandardError.new "For statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end

  "for #{args[0]};"
end

#compileForElse(args) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 53

def compileForElse args
  if args&.count != 1
    raise StandardError.new "For else statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end
  @loop_else_counter += 1

  "_looped_#{@loop_else_counter}=true;for #{args[0]};_looped_#{@loop_else_counter}=false;"
end

#compileNext(args) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 68

def compileNext args
  if args&.count&.> 1
    raise StandardError.new "For statement: wrong number of arguments (given #{args&.count || 0}, expecting 0 or 1)"
  end

  if args.nil?
    "next;"
  else
    "next #{args[0]};"
  end
end

#compileNextIf(args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 80

def compileNextIf args
  if args.nil? || args.count > 2
    raise StandardError.new "For statement: wrong number of arguments (given #{args&.count || 0}, expecting 1 or 2)"
  end

  if args.count == 1
    "if #{args[0]};next;end;"
  else
    "if #{args[0]};next #{args[1]};end;"
  end
end

#compileUntil(args) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 92

def compileUntil args
  if args&.count != 1
    raise StandardError.new "Until statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end

  "until #{args[0]};"
end

#compileWhile(args) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 100

def compileWhile args
  if args&.count != 1
    raise StandardError.new "While statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end

  "while #{args[0]};"
end