Class: Liquor::Emitter

Inherits:
Object
  • Object
show all
Includes:
ASTTools
Defined in:
lib/liquor/emitter.rb

Constant Summary collapse

OPERATORS =
{
  :uminus => '-',  :not   => '!',
  :mul    => '*',  :div   => '/',  :mod => '%',
  :plus   => '+',  :minus => '-',
  :eq     => '==', :neq   => '!=',
  :lt     => '<',  :leq   => '<=',
  :gt     => '>',  :geq   => '>=',
  :and    => '&&', :or    => '||',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Emitter

Returns a new instance of Emitter



7
8
9
10
11
12
# File 'lib/liquor/emitter.rb', line 7

def initialize(context)
  @context  = context
  @compiler = context.compiler
  @buffer   = ""
  @current_capture_var = ""
end

Instance Attribute Details

#compilerObject (readonly)

Returns the value of attribute compiler



5
6
7
# File 'lib/liquor/emitter.rb', line 5

def compiler
  @compiler
end

#contextObject (readonly)

Returns the value of attribute context



5
6
7
# File 'lib/liquor/emitter.rb', line 5

def context
  @context
end

Instance Method Details

#boolean_binop(node) ⇒ Object



182
183
184
185
# File 'lib/liquor/emitter.rb', line 182

def boolean_binop(node)
  lhs, rhs = nvalue(node)
  "(#{convert_boolean(lhs)} #{OPERATORS[ntype(node)]} #{convert_boolean(rhs)})"
end

#boolean_unop(node) ⇒ Object



187
188
189
190
191
# File 'lib/liquor/emitter.rb', line 187

def boolean_unop(node)
  expr, = nvalue(node)
  # converts by itself
  "#{OPERATORS[ntype(node)]}(#{expr(expr)})"
end

#bufObject



18
19
20
# File 'lib/liquor/emitter.rb', line 18

def buf
  "_buf#{@current_capture_var}"
end

#call(node) ⇒ Object



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
148
149
150
151
152
153
154
# File 'lib/liquor/emitter.rb', line 112

def call(node)
  lhs, rhs = nvalue(node)
  name,    = nvalue(lhs)
  arg, kw  = nvalue(rhs)

  if !@context.function? name
    raise NameError.new("undefined function `#{name}'", nloc(lhs))
  end

  function = @context.compiler.function(name)
  if function.unnamed_arg && arg.nil?
    raise ArgumentError.new("unnamed argument is required, but none provided", nloc(rhs))
  elsif !function.unnamed_arg && !arg.nil?
    raise ArgumentError.new("unnamed argument is not accepted, but is provided", nloc(arg))
  else
    function.mandatory_named_args.each do |kwarg,|
      unless kw.include? kwarg
        raise ArgumentError.new("named argument `#{kwarg}' is required, but none provided", nloc(rhs))
      end
    end
    kw.each do |kwarg, kwval|
      if !function.mandatory_named_args.include?(kwarg) &&
         !function.optional_named_args.include?(kwarg)
        raise ArgumentError.new("named argument `#{kwarg}' is not accepted, but is provided", nloc(kwval))
      end
    end
  end

  if arg.nil?
    args = [ "nil" ]
  else
    args = [ expr(arg) ]
  end

  kw = kw.map do |kwarg, kwval|
    "#{kwarg.inspect} => #{expr(kwval)}"
  end
  args << "{ #{kw.join(", ")} }"

  args << nloc(node).inspect

  "@functions[#{name.inspect}].call(#{args.join(', ')})"
end

#captureObject



286
287
288
289
290
291
292
293
294
295
296
# File 'lib/liquor/emitter.rb', line 286

def capture
  previous_capture_var = @current_capture_var
  @current_capture_var = @buffer.lines.count.to_s # unique id
  out! %Q{#{buf} = ""\n}

  yield

  buf
ensure
  @current_capture_var = previous_capture_var
end

#cat!(string) ⇒ Object



298
299
300
# File 'lib/liquor/emitter.rb', line 298

def cat!(string)
  out! "#{buf} << (#{string})\n"
end

#check_external(node) ⇒ Object



227
228
229
# File 'lib/liquor/emitter.rb', line 227

def check_external(node)
  "Runtime.external!(#{expr(node)}, #{nloc(node).inspect})"
end

#check_integer(node) ⇒ Object



197
198
199
# File 'lib/liquor/emitter.rb', line 197

def check_integer(node)
  "Runtime.integer!(#{expr(node)}, #{nloc(node).inspect})"
end

#check_interp(node) ⇒ Object



231
232
233
# File 'lib/liquor/emitter.rb', line 231

def check_interp(node)
  "Runtime.interp!(#{expr(node)}, #{nloc(node).inspect})"
end

#check_string(node) ⇒ Object



206
207
208
# File 'lib/liquor/emitter.rb', line 206

def check_string(node)
  "Runtime.string!(#{expr(node)}, #{nloc(node).inspect})"
end

#check_tuple(node) ⇒ Object



215
216
217
# File 'lib/liquor/emitter.rb', line 215

def check_tuple(node)
  "Runtime.tuple!(#{expr(node)}, #{nloc(node).inspect})"
end

#compile_block(block) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/liquor/emitter.rb', line 258

def compile_block(block)
  block.each do |node|
    case ntype(node)
    when :plaintext
      cat! string(node)

    when :interp
      expr, = nvalue(node)
      cat! check_interp(expr)

    when :tag
      ident, args = nvalue(node)
      name, = nvalue(ident)

      unless @compiler.has_tag? name
        raise NameError.new("undefined tag `#{name}'", nloc(ident))
      end

      @compiler.tag(name).compile(self, node)

    else
      raise "unknown block-level node #{ntype(node)}"
    end
  end
rescue Liquor::Diagnostic => e
  @compiler.add_diagnostic e
end

#compile_toplevel(block) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/liquor/emitter.rb', line 235

def compile_toplevel(block)
  compile_block(block)

  [
    %!lambda { |_env={}, _storage={}|\n!,
    %|  _buf = ""\n|,
    ([
      %|  |,
      @context.externals.map do |extern|
        @context.access(extern)
      end.join(", "),
      %| = |,
      @context.externals.map do |extern|
        %Q|_env[#{extern.inspect}]|
      end.join(", "),
    ] if @context.externals.any?),
    %|\n|,
    flush!,
    %|  _buf\n|,
    %|}\n|
  ].join
end

#convert_boolean(node) ⇒ Object



193
194
195
# File 'lib/liquor/emitter.rb', line 193

def convert_boolean(node)
  "!!(#{expr(node)})"
end

#envObject



14
15
16
# File 'lib/liquor/emitter.rb', line 14

def env
  '_env'
end

#equality_binop(node) ⇒ Object



177
178
179
180
# File 'lib/liquor/emitter.rb', line 177

def equality_binop(node)
  lhs, rhs = nvalue(node)
  "(#{expr(lhs)} #{OPERATORS[ntype(node)]} #{expr(rhs)})"
end

#expr(node) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/liquor/emitter.rb', line 55

def expr(node)
  case ntype(node)
  when :ident
    ident(node)
  when :integer
    integer(node)
  when :string
    string(node)
  when :tuple
    tuple(node)
  when :plus
    plus(node)
  when :mul, :div, :mod, :minus,
       :lt, :leq, :gt, :geq
    integer_binop(node)
  when :uminus
    integer_unop(node)
  when :eq, :neq
    equality_binop(node)
  when :and, :or
    boolean_binop(node)
  when :not
    boolean_unop(node)
  when :index
    index(node)
  when :external
    external(node)
  when :call
    call(node)
  else
    raise "unknown node #{ntype node}"
  end
end

#external(node) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/liquor/emitter.rb', line 89

def external(node)
  target, method, args = nvalue(node)
  name, = nvalue(method)

  if args
    arg, kw = nvalue(args)

    if arg.nil?
      out_args = [ "nil" ]
    else
      out_args = [ expr(arg) ]
    end

    kw.each do |kwarg, kwval|
      out_args << "#{kwarg.inspect} => #{expr(kwval)}"
    end
  else
    out_args = []
  end

  "#{check_external(target)}.liquor_send(#{name.inspect}, [ #{out_args.join(", ")} ], #{nloc(method).inspect})"
end

#flush!Object



307
308
309
310
311
# File 'lib/liquor/emitter.rb', line 307

def flush!
  @buffer
ensure
  @buffer = ""
end

#ident(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/liquor/emitter.rb', line 26

def ident(node)
  name, = nvalue(node)

  case @context.type name
  when :builtin
    case name
    when 'null';  'nil'
    when 'true';  'true'
    when 'false'; 'false'
    end
  when :variable
    @context.access name, nloc(node)
  when :function
    raise NameError.new("using function `#{name}' as a variable", nloc(node))
  when :free
    raise NameError.new("identifier `#{name}' is not bound", nloc(node))
  end
end

#index(node) ⇒ Object



156
157
158
159
# File 'lib/liquor/emitter.rb', line 156

def index(node)
  lhs, rhs = nvalue(node)
  "#{check_tuple(lhs)}[#{check_integer(rhs)}]"
end

#integer(node) ⇒ Object



201
202
203
204
# File 'lib/liquor/emitter.rb', line 201

def integer(node)
  value, = nvalue(node)
  value
end

#integer_binop(node) ⇒ Object



167
168
169
170
# File 'lib/liquor/emitter.rb', line 167

def integer_binop(node)
  lhs, rhs = nvalue(node)
  "(#{check_integer(lhs)} #{OPERATORS[ntype(node)]} #{check_integer(rhs)})"
end

#integer_unop(node) ⇒ Object



172
173
174
175
# File 'lib/liquor/emitter.rb', line 172

def integer_unop(node)
  expr, = nvalue(node)
  "#{OPERATORS[ntype(node)]}(#{check_integer(expr)})"
end

#out!(string) ⇒ Object



302
303
304
305
# File 'lib/liquor/emitter.rb', line 302

def out!(string)
  string = string.gsub /(^|\n)/m, '\1' + ("  " * @context.nesting)
  @buffer.concat string
end

#plus(node) ⇒ Object



161
162
163
164
165
# File 'lib/liquor/emitter.rb', line 161

def plus(node)
  lhs, rhs = nvalue(node)
  "Runtime.add!(#{expr(lhs)}, #{nloc(lhs).inspect}," +
    " #{expr(rhs)}, #{nloc(rhs).inspect})"
end

#storageObject



22
23
24
# File 'lib/liquor/emitter.rb', line 22

def storage
  "_storage"
end

#string(node) ⇒ Object



210
211
212
213
# File 'lib/liquor/emitter.rb', line 210

def string(node)
  value, = nvalue(node)
  value.inspect
end

#tuple(node) ⇒ Object



219
220
221
222
223
224
225
# File 'lib/liquor/emitter.rb', line 219

def tuple(node)
  value, = nvalue(node)
  code = value.map do |elem|
    expr elem
  end.join(", ")
  "[ #{code} ]"
end