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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
package types;
use strict;
use warnings FATAL => qw(all);
no if $] >= 5.018, warnings => "experimental::smartmatch";
use feature qw(switch);
use Exporter 'import';
our @EXPORT_OK = qw(_sequential_Q _equal_Q _clone
$nil $true $false _nil_Q _true_Q _false_Q
_symbol _symbol_Q _keyword _keyword_Q _list_Q _vector_Q
_hash_map _hash_map_Q _assoc_BANG _dissoc_BANG _atom_Q);
use Data::Dumper;
# General functions
sub _sequential_Q {
return _list_Q($_[0]) || _vector_Q($_[0])
}
sub _equal_Q {
my ($a, $b) = @_;
my ($ota, $otb) = (ref $a, ref $b);
if (!(($ota eq $otb) || (_sequential_Q($a) && _sequential_Q($b)))) {
return 0;
}
given (ref $a) {
when (/^Symbol/) {
return $$a eq $$b;
}
when (/^List/ || /^Vector/) {
if (! scalar(@{$a->{val}}) == scalar(@{$b->{val}})) {
return 0;
}
for (my $i=0; $i<scalar(@{$a->{val}}); $i++) {
if (! _equal_Q($a->nth($i), $b->nth($i))) {
return 0;
}
}
return 1;
}
when (/^HashMap/) {
die "TODO: Hash map comparison\n";
}
default {
return $$a eq $$b;
}
}
return 0;
}
sub _clone {
my ($obj) = @_;
given (ref $obj) {
when (/^List/) {
return List->new( [ @{$obj->{val}} ] );
}
when (/^Vector/) {
return Vector->new( [ @{$obj->{val}} ] );
}
when (/^HashMap/) {
return Vector->new( { %{$obj->{val}} } );
}
when (/^Function/) {
return Function->new_from_hash( { %{$obj} } );
}
default {
die "Clone of non-collection\n";
}
}
}
# Errors/Exceptions
{
package BlankException;
sub new { my $class = shift; bless String->new("Blank Line") => $class }
}
# Scalars
{
package Nil;
sub new { my $class = shift; my $s = 'nil'; bless \$s => $class }
}
{
package True;
sub new { my $class = shift; my $s = 'true'; bless \$s => $class }
}
{
package False;
sub new { my $class = shift; my $s = 'false'; bless \$s => $class }
}
our $nil = Nil->new();
our $true = True->new();
our $false = False->new();
sub _nil_Q { return $_[0] eq $nil }
sub _true_Q { return $_[0] eq $true }
sub _false_Q { return $_[0] eq $false }
{
package Integer;
sub new { my $class = shift; bless \$_[0] => $class }
}
{
package Symbol;
sub new { my $class = shift; bless \$_[0] => $class }
}
sub _symbol_Q { (ref $_[0]) =~ /^Symbol/ }
sub _keyword { return String->new(("\x{029e}".$_[0])); }
sub _keyword_Q { ((ref $_[0]) =~ /^String/) && ${$_[0]} =~ /^\x{029e}/; }
{
package String;
sub new { my $class = shift; bless \$_[0] => $class }
}
# Lists
{
package List;
sub new { my $class = shift; bless {'meta'=>$nil, 'val'=>$_[0]}, $class }
sub nth { $_[0]->{val}->[$_[1]]; }
#sub _val { $_[0]->{val}->[$_[1]]->{val}; } # return value of nth item
sub rest { my @arr = @{$_[0]->{val}}; List->new([@arr[1..$#arr]]); }
sub slice { my @arr = @{$_[0]->{val}}; List->new([@arr[$_[1]..$_[2]]]); }
}
sub _list_Q { (ref $_[0]) =~ /^List/ }
# Vectors
{
package Vector;
sub new { my $class = shift; bless {'meta'=>$nil, 'val'=>$_[0]}, $class }
sub nth { $_[0]->{val}->[$_[1]]; }
#sub _val { $_[0]->{val}->[$_[1]]->{val}; } # return value of nth item
sub rest { my @arr = @{$_[0]->{val}}; List->new([@arr[1..$#arr]]); }
sub slice { my @arr = @{$_[0]->{val}}; List->new([@arr[$_[1]..$_[2]]]); }
}
sub _vector_Q { (ref $_[0]) =~ /^Vector/ }
# Hash Maps
{
package HashMap;
sub new { my $class = shift; bless {'meta'=>$nil, 'val'=>$_[0]}, $class }
sub get { $_[0]->{val}->{$_[1]}; }
}
sub _hash_map {
my $hsh = {};
return _assoc_BANG($hsh, @_);
}
sub _assoc_BANG {
my $hsh = shift;
my @lst = @_;
for(my $i=0; $i<scalar(@lst); $i+=2) {
my $str = $lst[$i];
$hsh->{$$str} = $lst[$i+1];
}
return HashMap->new($hsh);
}
sub _dissoc_BANG {
my $hsh = shift;
my @lst = @_;
for(my $i=0; $i<scalar(@lst); $i++) {
my $str = $lst[$i];
delete $hsh->{$$str};
}
return HashMap->new($hsh);
}
sub _hash_map_Q { (ref $_[0]) =~ /^HashMap/ }
# Functions
{
package Function;
sub new {
my $class = shift;
my ($eval, $ast, $env, $params) = @_;
bless {'meta'=>$nil,
'eval'=>$eval,
'ast'=>$ast,
'env'=>$env,
'params'=>$params,
'ismacro'=>0}, $class
}
sub new_from_hash { my $class = shift; bless $_[0], $class }
sub gen_env {
my $self = $_[0];
return Env->new($self->{env}, $self->{params}, $_[1]);
}
sub apply {
my $self = $_[0];
return &{ $self->{eval} }($self->{ast}, gen_env($self, $_[1]));
}
}
# Atoms
{
package Atom;
sub new { my $class = shift; bless {'meta'=>$nil, 'val'=>$_[0]}, $class }
}
sub _atom_Q { (ref $_[0]) =~ /^Atom/ }
1;
|