~tim/scheme-vm

ref: c5110102ce2df2d78a0c87f293edf20f5a3984e7 scheme-vm/compiler.rb -rw-r--r-- 7.5 KiB
c5110102Tim Morgan Add more char functions 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
43
44
45
46
47
48
49
50
51
52
53
54
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
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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
require_relative 'vm'
require_relative 'compiler/macro'
require_relative 'compiler/optimizer'
require_relative 'compiler/libraries'
require_relative 'compiler/lib/scheme/base'
require_relative 'compiler/lib/scheme/process_context'
require_relative 'compiler/lib/scheme/write'
require 'pp'
require 'pry'

class Compiler
  ROOT_PATH = VM::ROOT_PATH
  LOAD_PATH = [File.join(ROOT_PATH, 'lib'), File.join(ROOT_PATH, 'spec')].freeze

  include Compiler::Libraries
  include Compiler::Lib::Scheme::Base
  include Compiler::Lib::Scheme::ProcessContext
  include Compiler::Lib::Scheme::Write

  def initialize(code = nil, filename:, arguments: {}, load_path: LOAD_PATH)
    @variables = {}
    @filename = filename
    @arguments = arguments
    @load_path = load_path
    @syntax = {}              # macro transformers
    @locals = {}              # top-level locals (globals)
    @libs = {}                # loaded libraries
    @mangled_identifiers = {} # used for macro hygiene
    @source = {}              # store source code for each file compiled
    @source[filename] = code
    @sexps = []
    @sexps += Parser.new(code, filename: filename).parse if code
  end

  attr_reader :variables, :arguments, :syntax, :source, :libs
  attr_accessor :filename

  def compile(code = nil)
    if code
      @source[@filename] = code
      @sexps = Parser.new(code, filename: filename).parse
    end
    compile_sexps(@sexps, options: { syntax: @syntax, locals: @locals }) + [VM::HALT]
  end

  def mangle_identifier(name)
    @mangled_identifiers[name] ||= 0
    version = @mangled_identifiers[name] += 1
    name.mangle(version)
  end

  def built_in_function?(name)
    !built_in_function_name(name).nil?
  end

  def built_in_function_name(name)
    underscored_name = 'do_' + name.gsub(/([a-z])-/, '\1_').gsub(/^--/, '')
    underscored_name if respond_to?(underscored_name, :include_private)
  end

  private

  def compile_sexps(sexps, options:)
    optimize(
      sexps
        .map { |s| compile_sexp(s, options) }
        .flatten
        .compact
    )
  end

  def compile_sexps_use_last(sexps, options:)
    return [] if sexps.empty?
    sexps[0..-2].map { |s| compile_sexp_discard(s, options) } +
      [compile_sexp_use(sexps.last, options)]
  end

  def optimize(instructions)
    Optimizer.new(instructions).optimize
  end

  def compile_sexp(sexp, options = { use: false, locals: {} })
    sexp = sexp.to_ruby if sexp.is_a?(VM::Pair)
    return compile_literal(sexp, options) unless sexp.is_a?(Array)
    return [] if sexp.empty? && !options[:quote]
    dispatch(sexp, options)
  end

  def compile_sexp_use(sexp, options = { locals: {} })
    compile_sexp(sexp, options.merge(use: true))
  end

  def compile_sexp_discard(sexp, options = { locals: {} })
    compile_sexp(sexp, options.merge(use: false))
  end

  def dispatch(sexp, options)
    (name, *args) = sexp
    if options[:quote] || options[:quasiquote]
      compile_quoted_sexp(sexp, options)
    elsif name.is_a?(Array) || name.is_a?(VM::Pair)
      call(sexp, options)
    elsif name == 'include'
      do_include(args, name.filename, options)
    elsif name == 'import'
      do_import(args, name.filename, options)
    elsif (built_in_name = built_in_function_name(name))
      send(built_in_name, args, options)
    elsif (macro = options[:syntax][name])
      compile_macro_sexp(sexp, macro, options)
    else
      call(sexp, options)
    end
  end

  def compile_quoted_sexp(sexp, options)
    (name, *_args) = sexp
    if name =~ /unquote(\-splicing)?/ && options[:quasiquote]
      compile_quasiquoted_sexp(sexp, options)
    elsif sexp.size == 3 && sexp[1] == '.'
      compile_pair(sexp, options)
    else
      base_list(sexp, options)
    end
  end

  def compile_quasiquoted_sexp(sexp, options)
    (name, *args) = sexp
    expr = compile_sexp(args.first, options.merge(quasiquote: false))
    if name == 'unquote-splicing'
      raise 'can only use unquote-splicing with a list' if expr.compact.last != VM::PUSH_LIST
      ['splice', expr.first]
    else
      expr
    end
  end

  def compile_literal(literal, options = { locals: {} })
    case literal.to_s.strip
    when /\A-?[0-9]+\z/
      compile_number(literal, options)
    when '#t', '#true', '#f', '#false'
      compile_boolean(literal, options)
    when /\A#\\(.+)\z/
      compile_character($1, options)
    when /\A"(.*)"\z/
      compile_string($1, options)
    when ''
      []
    else
      compile_atom(literal, options)
    end
  end

  def compile_atom(name, options)
    if options[:quote] || options[:quasiquote]
      [
        VM::PUSH_ATOM,
        name,
        pop_maybe(options)
      ]
    else
      [
        push_var(name, options),
        pop_maybe(options)
      ]
    end
  end

  def compile_boolean(name, options)
    case name
    when '#t', '#true'
      [VM::PUSH_TRUE, pop_maybe(options)]
    when '#f', '#false'
      [VM::PUSH_FALSE, pop_maybe(options)]
    end
  end

  def compile_character(name, options)
    code = {
      'space'     => ' ',
      'newline'   => "\n",
      'alarm'     => "\a",
      'backspace' => "\b",
      'delete'    => "\x7F",
      'escape'    => "\x1B",
      'null'      => "\x00",
      'return'    => "\r",
      'tab'       => "\t"
    }.fetch(name, name[0]).ord
    [
      VM::PUSH_NUM, code,
      VM::TO_CHAR,
      pop_maybe(options)
    ]
  end

  def compile_string(string, options)
    [
      VM::PUSH_STR,
      string,
      pop_maybe(options)
    ]
  end

  def compile_number(number, options)
    [
      VM::PUSH_NUM,
      number,
      pop_maybe(options)
    ]
  end

  def compile_macro_sexp(sexp, macro, options)
    if (method_name = macro[:native_transformer])
      send(method_name, sexp[1..-1], options)
    else
      sexp = Macro.new(macro, self).expand(sexp)
      locals = Hash[macro[:locals].zip([true] * macro[:locals].size)]
      compile_sexp(
        sexp,
        options.merge(
          syntax: options[:syntax].merge(macro[:syntax] || {}),
          locals: options[:locals].merge(locals)
        )
      )
    end
  end

  def unmangled_name(name)
    return name unless (match = name.match(/\A#([^:]+):(.+)\z/))
    match[2]
  end

  def do_debug(_args, _options)
    [VM::DEBUG]
  end

  def do_debug_compile(_args, options)
    binding.pry # rubocop:disable Lint/Debugger
    []
  end

  def call((lambda, *args), options)
    function = compile_sexp_use(lambda, options)
    [
      args.map { |arg| compile_sexp_use(arg, options) },
      args.any? ? [VM::PUSH_NUM, args.size, VM::SET_ARGS] : nil,
      function,
      VM::CALL
    ]
  end

  def compile_pair((car, _, cdr), options)
    [
      compile_sexp_use(car, options),
      compile_sexp_use(cdr, options),
      VM::CONS,
      pop_maybe(options)
    ]
  end

  def push_var(name, options)
    raise VM::VariableUndefined, name unless options[:locals][unmangled_name(name)]
    [
      VM::PUSH_VAR,
      name
    ]
  end

  def push_arg(name, _options = {})
    [
      VM::PUSH_ARG,
      VM::NAME_ARG, name
    ]
  end

  def push_all_args(name, _options = {})
    [
      VM::PUSH_ARGS,
      VM::NAME_ARG, name
    ]
  end

  def pop_maybe(options)
    return VM::POP unless options[:use]
  end

  def parse_file(filename, relative_to: nil)
    path = if filename.start_with?('.') && relative_to
             File.join(File.dirname(relative_to), filename)
           else
             @load_path.map { |p| File.join(p, filename) }.detect { |p| File.exist?(p) }
           end
    raise "File #{filename} not found in load path #{@load_path.join(';')}" unless path
    code = File.read(path)
    @source[filename] = code
    Parser.new(code, filename: filename).parse
  end
end