Module: Liquidscript::Compiler::ICR::Expressions

Included in:
Liquidscript::Compiler::ICR
Defined in:
lib/liquidscript/compiler/icr/expressions.rb

Instance Method Summary collapse

Instance Method Details

#_compile_catchObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 201

def _compile_catch
  shift :catch
  shift :lparen
  var = shift :identifier
  shift :rparen
  shift :lbrace
  catch_body = collect_compiles(:expression, :rbrace)

  next_part = if peek?(:finally)
    _compile_finally
  end

  code :catch, var, catch_body, next_part
end

#_compile_finallyObject



216
217
218
219
220
221
222
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 216

def _compile_finally
  shift :finally
  shift :lbrace
  finally_body = collect_compiles(:expression, :rbrace)

  code :finally, finally_body
end

#compile_assignment(identifier) ⇒ ICR::Code

Handles an assignment of the form ‘identifier = expression`, with the argument being the identifier, and the position of the compiler being after it.

Returns:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 70

def compile_assignment(identifier)
  shift :equal

  if identifier.type == :identifier
    variable = set(identifier)
    value    = compile_vexpression
    variable.value = value
  else
    value    = compile_vexpression
    variable = identifier
  end

  code :set, variable, value
end

#compile_binop(left) ⇒ Object



56
57
58
59
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 56

def compile_binop(left)
  code :binop, shift(:binop, :minus, :plus), left,
    compile_vexpression
end

#compile_elseObject



178
179
180
181
182
183
184
185
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 178

def compile_else
  shift :else
  shift :lbrace

  body = collect_compiles(:expression, :rbrace)

  code :else, body
end

#compile_expressionICR::Code

Compiles an expression. This is primarily used in a general context, such that anything can be returned.

Returns:



10
11
12
13
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 10

def compile_expression
  expect :if, :unless, :class, :module, :loop, :for,
         :while, :action, :try, :_ => :vexpression
end

#compile_lparenICR::Code

We don’t know, at this point, whether this is a function declaration, or an expression; if it’s a function declaration, the contents of the lparen can only be commas and identifiers; if it’s an expression, it can have anything but commas in it.

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 91

def compile_lparen
  shift :lparen
  maybe_func = 1
  components = []

  unless peek?(:identifier, :rparen)
    maybe_func = 0
  end

  expression = action do
    maybe_func = 0
    components << compile_vexpression
  end

  ident = action do |i|
    if peek?(:comma)
      maybe_func = 2
      components << i
    elsif peek?(:rparen)
      components << i
    else
      components << value_expect(compile_identifier(i))
    end
  end

  loop do
  case maybe_func
  when 0
    expect :rparen => action.end_loop,
      :_           => expression
  when 1
    expect :rparen => action.end_loop,
      :comma       => action { maybe_func = 2 },
      :identifier  => ident,
      :_           => expression
  when 2
    expect :rparen => action.end_loop,
      :comma       => action.shift,
      :identifier  => action { |i| components << i }
  end
  end

  func_decl = (maybe_func == 1 && peek?(:arrow)) ||
    (maybe_func == 2)

  if func_decl
    compile_function_with_parameters(components)
  else
    code(:expression, components.map do |c|
      if c.is_a?(Scanner::Token)
       compile_identifier(c)
      else
        c
      end
    end)
  end
end

#compile_minusObject



44
45
46
47
48
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 44

def compile_minus
  shift :minus

  code :neg, compile_vexpression
end

#compile_plusObject



50
51
52
53
54
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 50

def compile_plus
  shift :plus

  code :pos, compile_vexpression
end

#compile_tryObject



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 187

def compile_try
  shift :try
  shift :lbrace
  try_body = collect_compiles(:expression, :rbrace)

  next_part = if peek?(:catch)
    _compile_catch
  elsif peek?(:finally)
    _compile_finally
  end

  code :try, try_body, next_part
end

#compile_unlessObject



167
168
169
170
171
172
173
174
175
176
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 167

def compile_unless
  shift :unless
  shift :lparen
  conditional = compile_vexpression
  shift :rparen
  shift :lbrace

  body = collect_compiles(:expression, :rbrace)
  code :unless, conditional, body
end

#compile_unopObject



61
62
63
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 61

def compile_unop
  code :unop, shift(:unop, :preunop), compile_vexpression
end

#compile_vexpressionICR::Code

Compiles an expression that returns a value.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 18

def compile_vexpression
  out = expect :number,     :identifier,
               :istring,    :lparen,
               :sstring,    :operator,
               :keyword,
               :regex,
               :newline,
               :istring_begin,
               :iheredoc_begin,
               :plus, :minus,
               :lbrace   => :object,
               :lbrack   => :array,
               :arrow    => :function,
               [
                :preunop, :unop
               ]         => :unop,
               [
                :heredoc_ref, :iheredoc_ref
               ]         => :href,
               [
                :heredoc, :iheredoc
               ]         => :heredoc

  value_expect(out)
end