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
15
16
17
18
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 8

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

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

#compileEach(args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rblade/compiler/statements/compiles_loops.rb', line 20

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



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

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



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

#compileUntil(args) ⇒ Object



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

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



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

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