Class: Lisp

Inherits:
Object show all
Defined in:
lib/missinglisp.rb

Instance Method Summary collapse

Instance Method Details

#_parse(tokens) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/missinglisp.rb', line 59

def _parse(tokens)
  return if tokens.empty?

  token = tokens.shift

  case token
  when :L
    list = []

    while tokens.first != :J
      raise 'unexpected end of input' if tokens.empty?

      list << _parse(tokens)
    end
    tokens.shift

    list
  when :J
    raise 'unexpected \'J\''
  else
    token
  end
end

#eval(exp, env) ⇒ Object



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
# File 'lib/missinglisp.rb', line 25

def eval(exp, env)
  return exp if exp.is_a? Numeric
  return env[exp] if exp.is_a? Symbol

  # list
  # Special forms
  if exp[0] == :quote
    return exp[1..]
  elsif exp[0] == :if
    _, test, e1, e2 = exp
    test = self.eval(test, env)
    return self.eval(e1, env) if test.is_a?(Array) && !test.empty?
    return self.eval(e1, env) unless test.is_a?(Array) || !test

    return self.eval(e2, env)
  elsif exp[0] == :define
    _, var, e = exp
    env[var] = self.eval(e, env)
    return []
  elsif exp[0] == :lambda
    _, params, e = exp
    return ->(*args) { self.eval(e, env.merge(Hash[params.zip(args)])) }
  end

  args = exp[1..].map { |c| self.eval(c, env) }
  env[exp.first]
    .call(*args)
end

#initial_envObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/missinglisp.rb', line 2

def initial_env
  {
    car: ->(*ls) { ls[0][0] },
    cdr: ->(*ls) { ls[0].drop 1 },
    cons: ->(e, cell, *) { [e] + cell },
    list: ->(*ls) { ls },
    add: ->(*ls) { ls.sum },
    sub: ->(hd, *tl) { tl.reduce(hd) { |sum, v| sum - v } },
    mult: ->(*ls) { ls.reduce(:*) },
    div: ->(hd, *tl) { tl.reduce(hd) { |sum, v| sum / v } },

    eq: ->(a, b, *) { a == b },
    lt: ->(a, b, *) { a < b },
    leq: ->(a, b, *) { a <= b },
    gt: ->(a, b, *) { a > b },
    geq: ->(a, b, *) { a >= b },

    p: ->(*ls) { p ls },

    nil: []
  }
end

#parse(program) ⇒ Object



54
55
56
57
# File 'lib/missinglisp.rb', line 54

def parse(program)
  tokens = tokenize program
  _parse tokens
end

#tokenize(s) ⇒ Object



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
# File 'lib/missinglisp.rb', line 83

def tokenize(s)
  res = []
  cur = ''
  is_reading_digit = false
  s.each_char do |c|
    case c
    when '_'
      if cur != ''
        if is_reading_digit
          res.push cur.to_i
        else
          res.push cur.to_sym
        end

        cur = ''
        is_reading_digit = false
      end
    when 'L'
      if cur != ''
        if is_reading_digit
          res.push cur.to_i
          cur = ''
          is_reading_digit = false
          res.push :L
          next
        end

        cur += 'L' and next
      end

      res.push :L
    when 'J'
      if cur != ''
        if is_reading_digit
          res.push cur.to_i
          cur = ''
          is_reading_digit = false
          res.push :J
          next
        end

        cur += 'J' and next
      end

      res.push :J
    when '0'..'9'
      cur += c and next if cur != ''

      is_reading_digit = true
      cur = c
    else
      if is_reading_digit
        res.push cur.to_i
        cur = ''
        is_reading_digit = false
      end

      cur += c
    end
  end

  res.push cur.to_sym if cur != ''

  res
end