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_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.



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_expressionICR::Code

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



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.



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_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.



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