-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
89 lines (79 loc) · 2.71 KB
/
main.rb
File metadata and controls
89 lines (79 loc) · 2.71 KB
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
require_relative "./parsing/main.rb"
require_relative "./types.rb"
require_relative "./env.rb"
require_relative "./procedure.rb"
require_relative "./eval.rb"
class Main
def call
@global_env = standard_env
@evaluator = Eval.new(@global_env)
repl()
end
private
attr_reader :global_env, :evaluator
def repl(prompt='falklisp> ')
loop do
print prompt
input = gets.chomp
begin
parsed_input = parse(input)
val = evaluator.eval(parsed_input, global_env)
puts lispstr(val) if val || val == false
rescue SyntaxError => e
puts "Syntax Error: #{e.message}"
# rescue => e
# puts "Error: #{e.message}"
end
end
end
def lispstr(exp)
if exp.is_a?(Array)
'(' + exp.map { |e| lispstr(e) }.join(' ') + ')'
else
exp.to_s
end
end
def parse(program)
Parsing.new(program).call
end
def standard_env
env = Env.new
env.update({
'+' => lambda { |x, y| x + y },
'-' => lambda { |x, y| x - y },
'*' => lambda { |x, y| x * y },
'/' => lambda { |x, y| x / y.to_f },
'>' => lambda { |x, y| x > y },
'<' => lambda { |x, y| x < y },
'>=' => lambda { |x, y| x >= y },
'<=' => lambda { |x, y| x <= y },
'=' => lambda { |x, y| x == y },
'abs' => lambda { |x| x.abs },
'append' => lambda { |x, y| x + y },
'apply' => lambda { |proc, args| proc.call(*args) },
'begin' => lambda { |*x| x[-1] },
'car' => lambda { |x| x[0] },
'cdr' => lambda { |x| x[1..-1] },
'cons' => lambda { |x, y| [x] + y },
'eq?' => lambda { |x, y| x.equal?(y) },
'equal?' => lambda { |x, y| x == y },
'length' => lambda { |x| x.length },
'list' => lambda { |*x| x },
'list?' => lambda { |x| x.is_a?(Array) },
'map' => lambda { |proc, lst| lst.map(&proc) },
'max' => lambda { |*x| x.max },
'min' => lambda { |*x| x.min },
'not' => lambda { |x| !x },
'null?' => lambda { |x| x == [] },
'number?' => lambda { |x| x.is_a?(Numeric) },
'procedure?' => lambda { |x| x.respond_to?(:call) },
'round' => lambda { |x| x.round },
'symbol?' => lambda { |x| x.is_a?(Symbol) },
'filter' => lambda { |proc, lst| lst.select(&proc) },
'reduce' => lambda { |proc, init, lst| lst.reduce(init, &proc) }
})
env
end
end
p = Main.new
p.call