Module: Liquidscript::Compiler::ICR::Literals

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

Instance Method Summary collapse

Instance Method Details

#_compile_for_in(ident) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/liquidscript/compiler/icr/literals.rb', line 39

def _compile_for_in(ident)
  content = shift :identifier
  unless content.value == "in"
    raise CompileError, "Expected `in', got #{content.value}"
  end

  obj = shift :identifier
  shift :rparen
  shift :lbrace

  set ident
  body = collect_compiles(:expression, :rbrace)
  code :for_in, ident, ref(obj), body
end

#_compile_for_seg(first) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/liquidscript/compiler/icr/literals.rb', line 54

def _compile_for_seg(first)
  first = value_expect(first)
  shift :comma
  second = compile_vexpression
  shift :comma
  third = compile_vexpression
  shift :rparen
  shift :lbrace

  body = collect_compiles(:expression, :rbrace)
  code :for_seg, first, second, third, body
end

#compile_actionObject



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

def compile_action
  code :action, shift(:action)
end

#compile_arrayObject



151
152
153
154
155
156
157
158
# File 'lib/liquidscript/compiler/icr/literals.rb', line 151

def compile_array
  shift :lbrack

  parts = collect_compiles(:vexpression, :rbrack,
    :comma => action.shift)

  code :array, parts
end

#compile_forObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/liquidscript/compiler/icr/literals.rb', line 24

def compile_for
  shift :for
  shift :lparen
  if peek?(:identifier)
    ident = shift :identifier
    if peek?(:identifier)
      _compile_for_in(ident)
    else
      _compile_for_seg(compile_identifier(ident))
    end
  else
    compile_for_seg compile_vexpression
  end
end

#compile_functionObject



167
168
169
# File 'lib/liquidscript/compiler/icr/literals.rb', line 167

def compile_function
  compile_function_with_parameters([])
end

#compile_function_with_parameters(parameters) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/liquidscript/compiler/icr/literals.rb', line 178

def compile_function_with_parameters(parameters)
  shift :arrow

  expressions = Liquidscript::ICR::Set.new
  expressions.context = Liquidscript::ICR::Context.new
  expressions.context.parent = top.context
  expressions[:arguments] = parameters
  @set << expressions

  parameters.each do |parameter|
    set(parameter).parameter!
  end


  expression = action do
    expressions << compile_expression
  end

  unless peek?(:lbrace)
    expression.call
  else
    shift :lbrace
    loop do
      expect :rbrace => action.end_loop,
             :_      => expression
    end
  end

  code :function, @set.pop
end

#compile_heredocObject



86
87
88
89
90
91
92
# File 'lib/liquidscript/compiler/icr/literals.rb', line 86

def compile_heredoc
  h = shift(:heredoc, :iheredoc)

  top[:heredocs][top[:herenum]].body = [h]
  top[:herenum] += 1
  nil
end

#compile_hrefObject



77
78
79
80
81
82
83
84
# File 'lib/liquidscript/compiler/icr/literals.rb', line 77

def compile_href
  ref = shift :heredoc_ref, :iheredoc_ref
  heredoc = Heredoc.new(ref.value, ref.type == :iheredoc_ref)
  top[:heredocs] ||= []
  top[:herenum]  ||= 0
  top[:heredocs] << heredoc
  code :href, heredoc
end

#compile_identifier(identifier) ⇒ Object



67
68
69
70
71
# File 'lib/liquidscript/compiler/icr/literals.rb', line 67

def compile_identifier(identifier)
  ref(identifier) unless peek?(:equal)

  identifier
end

#compile_iheredoc_beginObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/liquidscript/compiler/icr/literals.rb', line 94

def compile_iheredoc_begin
  start = shift :iheredoc_begin
  contents = [start]

  loop do
    contents << compile_vexpression
    contents << shift(:iheredoc)
    peek?(:istring_begin)
  end

  top[:heredocs][top[:herenum]].body = contents
  top[:herenum] += 1
  nil
end

#compile_istringObject



123
124
125
# File 'lib/liquidscript/compiler/icr/literals.rb', line 123

def compile_istring
  code :istring, shift(:istring).value
end

#compile_istring_beginObject



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/liquidscript/compiler/icr/literals.rb', line 109

def compile_istring_begin
  start = shift :istring_begin
  contents = [start]

  loop do
    contents << compile_vexpression
    contents << shift(:istring)
    peek?(:istring_begin)
  end


  code :interop, *contents
end

#compile_keywordObject



135
136
137
# File 'lib/liquidscript/compiler/icr/literals.rb', line 135

def compile_keyword
  code :keyword, shift(:keyword)
end

#compile_newlineObject



171
172
173
174
175
176
# File 'lib/liquidscript/compiler/icr/literals.rb', line 171

def compile_newline
  if peek?(:newline)
    pop
    code :newline
  end
end

#compile_numberObject



6
7
8
# File 'lib/liquidscript/compiler/icr/literals.rb', line 6

def compile_number
  code :number, pop.value
end

#compile_objectObject



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/liquidscript/compiler/icr/literals.rb', line 139

def compile_object
  shift :lbrace

  objects = collect_compiles :rbrace,
    :comma => action.shift,
    :newline => action.shift do
    [compile_object_key, compile_vexpression]
  end

  code :object, objects
end

#compile_object_keyObject



160
161
162
163
164
165
# File 'lib/liquidscript/compiler/icr/literals.rb', line 160

def compile_object_key
  key = shift :identifier, :istring
  shift :colon

  key
end

#compile_operatorObject



131
132
133
# File 'lib/liquidscript/compiler/icr/literals.rb', line 131

def compile_operator
  code :operator, shift(:operator), compile_vexpression
end

#compile_regexObject



73
74
75
# File 'lib/liquidscript/compiler/icr/literals.rb', line 73

def compile_regex
  code :regex, shift(:regex)
end

#compile_sstringObject



127
128
129
# File 'lib/liquidscript/compiler/icr/literals.rb', line 127

def compile_sstring
  code :sstring, shift(:sstring).value[1..-1]
end

#compile_whileObject



14
15
16
17
18
19
20
21
22
# File 'lib/liquidscript/compiler/icr/literals.rb', line 14

def compile_while
  shift :while
  shift :lparen
  conditional = compile_vexpression
  shift :rparen
  shift :lbrace
  body = collect_compiles(:expression, :rbrace)
  code :while, conditional, body
end