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.

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 50

def compile_assignment(identifier)
  shift :equal
  value    = compile_vexpression

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

  code :set, variable, value
end

#compile_binop(left) ⇒ Object



37
38
39
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 37

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

#compile_elseObject



146
147
148
149
150
151
152
153
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 146

def compile_else
  shift :else
  shift :lbrack

  body = collect_compiles(:expression, :rbrack)

  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
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 10

def compile_expression
  expect :if, :unless, :class, :module, :_ => :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:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 70

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

  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  => action { |i| components << i },
      :_           => 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_unlessObject



135
136
137
138
139
140
141
142
143
144
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 135

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

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

#compile_unopObject



41
42
43
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 41

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

#compile_vexpressionICR::Code

Compiles an expression that returns a value.

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/liquidscript/compiler/icr/expressions.rb', line 17

def compile_vexpression
  out = expect :number,     :identifier,
               :istring,    :lparen,
               :sstring,    :operator,
               :keyword,    :unop,
               :newline,
               :istring_begin,
               :lbrack   => :object,
               :lbrace   => :array,
               :arrow    => :function

  if peek? :binop
    compile_binop(out)
  elsif peek? :prop
    compile_property(out)
  else
    out
  end
end