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
33
34
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 24

def compileEach args
  if args.nil? || args.count > 2
    raise StandardError.new "Each statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end
  # Allow variables to be a key, value pair
  args = args.join ","

  variables, collection = args.split(" in ")

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

#compileEachElse(args) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 36

def compileEachElse args
  if args.nil? || args.count > 2
    raise StandardError.new "Each statement: wrong number of arguments (given #{args&.count || 0}, expecting 1)"
  end
  # Allow variables to be a key, value pair
  args = args.join ","
  @loop_else_counter += 1

  variables, collection = args.split(" in ")

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

#compileEmptyObject



66
67
68
69
70
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 66

def compileEmpty
  @loop_else_counter -= 1

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

#compileFor(args) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 49

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



57
58
59
60
61
62
63
64
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 57

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}=false;for #{args[0]};_looped_#{@loop_else_counter}=true;"
end

#compileNext(args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 72

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



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 84

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



96
97
98
99
100
101
102
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 96

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



104
105
106
107
108
109
110
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 104

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