~tim/scheme-vm

7a1473623f9718f5f69600f363d0c086f4ae5b09 — Tim Morgan 2 years ago b2181e2
Support all character names in the spec
2 files changed, 26 insertions(+), 11 deletions(-)

M compiler.rb
M spec/compiler_spec.rb
M compiler.rb => compiler.rb +9 -2
@@ 178,8 178,15 @@ class Compiler

  def compile_character(name, options)
    code = {
      'space'   => ' ',
      'newline' => "\n"
      '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,

M spec/compiler_spec.rb => spec/compiler_spec.rb +17 -9
@@ 105,20 105,28 @@ describe Compiler do
          #\\c
          #\\space
          #\\newline
          #\\alarm
          #\\backspace
          #\\delete
          #\\escape
          #\\null
          #\\return
          #\\tab
        END
      end

      it 'compiles into vm instructions' do
        expect(d(@result)).to eq([
          'VM::PUSH_NUM', 99,
          'VM::TO_CHAR',
          'VM::POP',
          'VM::PUSH_NUM', 32,
          'VM::TO_CHAR',
          'VM::POP',
          'VM::PUSH_NUM', 10,
          'VM::TO_CHAR',
          'VM::POP',
          'VM::PUSH_NUM', 99,  'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 32,  'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 10,  'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 7,   'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 8,   'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 127, 'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 27,  'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 0,   'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 13,  'VM::TO_CHAR', 'VM::POP',
          'VM::PUSH_NUM', 9,   'VM::TO_CHAR', 'VM::POP',
          'VM::HALT'
        ])
      end