forked from wakasa-seesaa/scheme_in_ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extend_language.rb
196 lines (172 loc) · 3.82 KB
/
extend_language.rb
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
def null?(list)
list == []
end
$list_env = {
:nil => [],
:null => [:prim, lambda{|list| null?(list)}],
:cons => [:prim, lambda{|a, b| cons?(a, b)}],
:car => [:prim, lambda{|list| car?(list)}],
:cdr => [:prim, lambda{|list| cdr?(list)}],
:list => [:prim, lambda{|*list| list(*list)}],
}
$glocal_env = [$list_env, $primitive_fun_env, $boolean_env]
def cons(a, b)
if no list?(b)
raise "Sorry, we haven't implemented yet..."
else
[a] + b
end
end
def car(list)
list[0]
end
def cdr(list)
list[1..-1]
end
def list(*list)
list
end
def eval_define(exp, env)
if define_with_parameter?(exp)
var, val = define_with_parameter_var_val(exp)
else
var, val = define_var_val(exp)
end
var_ref = lookup_var_ref(var, env)
if var_ref != nil
var_ref[var] = _eval(val, env)
else
extend_env!([var], [_eval(val, env)], env)
end
nil
end
def extend_env!(parameters, args, env)
alist = parameters.zip(args)
alist.each { |k, v| h[k] = v}
env.unshift(h)
end
def define_with_parameter?(exp)
list?(exp[1])
end
def define_with_parameter_var_val
var = car(exp[1])
parameters, body = cdr(exp[1]), exp[2]
val = [:lambda, parameters, body]
[var, val]
end
def define_var_val(exp)
[exp[1], exp[2]]
end
def lookup_var_ref(var, env)
env.find{|alist| alist.key?(var)}
end
def define?(exp)
exp[0] == :define
end
def eval_cond(exp, env)
if_exp = cond_to_if(cdr(exp))
eval_if(if_exp, env)
end
def cond_to_if(cond_exp)
if cond_exp == []
''
else
e = car(cond_exp)
p, c = e[0], e[1]
if p == :else
p = :true
end
[:if, p, c, cond_to_if(cdr(cond_exp))]
end
end
def cond?(exp)
exp[0] == :cond
end
def parse(exp)
program = exp.strip().
gsub(/[a-zA-Z\+\-\*><=][0-9a-zA-Z\+\-=!*]*/, ':\\0').
gsub(/\s+/, ', ').
gsub(/\(/, '[').
gsub(/\)/, ']')
eval(program)
end
def eval_quote(exp, env)
car(cdr(exp))
end
def quote?(exp)
exp[0] == :quote
end
def special_form?(exp)
lambda?(exp) or
let?(exp) or
letrec?(exp) or
if?(exp) or
cond?(exp) or
define?(exp) or
quote?(exp)
end
def eval_special_form(exp, env)
if lambda?(exp)
eval_lambda(exp, env)
elsif let?(exp)
eval_let(exp, env)
elsif letrec?(exp)
eval_letrec(exp, env)
elsif if?(exp)
eval_if(exp, env)
elsif cond?(exp)
eval_cond(exp, env)
elsif define?(exp)
eval_define(exp, env)
elsif quote?(exp)
eval_quote(exp, env)
end
end
def repl
prompt = '>>> '
second_prompt = '> '
while true
print prompt
line = gets or return
while line.count('(') > line.count(')')
print second_prompt
next_line = gets or return
line += next_line
end
redo if line =~ /\A\s*\z/m
begin
val = _eval(parse(line), $global_env)
rescue Exception => e
puts e.to_s
redo
end
puts pp(val)
end
end
def closure?(exp)
exp[0] == :closure
end
def pp(exp)
if exp.is_a?(Symbol) or num?(exp)
exp.to_s
elsif exp == nil
'nil'
elsif exp.is_a?(Array) and closure?(exp)
parameter, body, env = exp[1], exp[2], exp[3]
"(closure #{pp(parameter)} #{pp(body)})"
elsif exp.is_a?(Hash)
if exp == $primitive_fun_env
'*primitive_fun_env*'
elsif exp == $boolean_env
'*boolean_env*'
elsif exp == $list_env
'*list_env*'
else
'{' + exp.map{|k, v| pp(k) + ':' + pp(v)}.join(', ') + '}'
end
elsif exp.is_a?(Array)
'(' + exp.map{|e| pp(e)}.join(', ') + ')'
else
exp.to_s
end
end