-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.rb
87 lines (78 loc) · 1.49 KB
/
ast.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
MalNil = Struct.new('MalNil') do
def to_s() inspect end
def inspect()
'nil'
end
end
MalString = Struct.new('MalString', :val) do
def to_s()
val
end
def inspect()
val.dump
end
end
MalSym = Struct.new('MalSym', :val) do
def to_s() inspect end
def inspect()
val
end
end
MalKeyWord = Struct.new('MalKeyWord', :val) do
def to_s() inspect end
def inspect()
":#{val}"
end
end
MalList = Struct.new('MalList', :list) do
def to_s() inspect end
def inspect()
result = list
.map { |e| e.inspect }
.join(" ")
"(#{result})"
end
end
MalVec = Struct.new('MalVec', :vec) do
def to_s() inspect end
def inspect()
result = vec
.map { |e| e.inspect }
.join(" ")
"[#{result}]"
end
end
MalMap = Struct.new('MalMap', :_map) do
def to_s() inspect end
def inspect()
result = _map
.map { |k, v| "#{k.inspect} #{v.inspect}" }
.join(" ")
"{#{result}}"
end
end
MalLambda = Struct.new('MalLambda', :bindings, :exp, :outer, :is_macro)
class MalFn
attr_reader :fn, :arity, :vararg, :name
def initialize(fn, arity, options)
@fn = fn
@arity = arity
@vararg = options.fetch(:vararg, false)
@name = options.fetch(:name, nil)
end
def to_s() inspect() end
def inspect()
if @name != nil
@name
elsif @fn.class == Proc
"#<builtin>"
else
if @fn.is_macro
"#<macro>"
else
"#<lambda>"
end
end
end
end
MalAtom = Struct.new('MalAtom', :inner)