2014-04-20 00:12:13 +04:00
|
|
|
package types;
|
|
|
|
use strict;
|
2014-04-22 05:08:18 +04:00
|
|
|
use warnings FATAL => qw(all);
|
2014-10-10 08:48:47 +04:00
|
|
|
no if $] >= 5.018, warnings => "experimental::smartmatch";
|
2014-04-21 08:45:58 +04:00
|
|
|
use feature qw(switch);
|
2014-04-20 00:12:13 +04:00
|
|
|
use Exporter 'import';
|
2014-04-24 06:46:57 +04:00
|
|
|
our @EXPORT_OK = qw(_sequential_Q _equal_Q _clone
|
2014-12-19 05:33:49 +03:00
|
|
|
$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);
|
2014-04-21 08:45:58 +04:00
|
|
|
|
|
|
|
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/) {
|
2015-11-19 00:39:46 +03:00
|
|
|
if (! (scalar(@{$a->{val}}) == scalar(@{$b->{val}}))) {
|
2014-04-21 08:45:58 +04:00
|
|
|
return 0;
|
|
|
|
}
|
2014-04-24 06:46:57 +04:00
|
|
|
for (my $i=0; $i<scalar(@{$a->{val}}); $i++) {
|
|
|
|
if (! _equal_Q($a->nth($i), $b->nth($i))) {
|
2014-04-21 08:45:58 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2014-04-24 06:46:57 +04:00
|
|
|
when (/^HashMap/) {
|
2015-11-19 06:44:58 +03:00
|
|
|
if (! (scalar(keys %{ $a->{val} }) == scalar(keys %{ $b->{val} }))) {
|
2015-11-19 00:39:46 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2015-11-19 06:44:58 +03:00
|
|
|
foreach my $k (keys %{ $a->{val} }) {
|
2015-11-19 00:39:46 +03:00
|
|
|
if (!_equal_Q($a->{val}->{$k}, $b->{val}->{$k})) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2014-04-24 06:46:57 +04:00
|
|
|
}
|
2014-04-21 08:45:58 +04:00
|
|
|
default {
|
|
|
|
return $$a eq $$b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-24 06:46:57 +04:00
|
|
|
sub _clone {
|
|
|
|
my ($obj) = @_;
|
|
|
|
given (ref $obj) {
|
2015-07-21 11:35:17 +03:00
|
|
|
when (/^CODE/) {
|
|
|
|
return FunctionRef->new( $obj );
|
2014-04-24 06:46:57 +04:00
|
|
|
}
|
|
|
|
default {
|
2015-07-21 11:35:17 +03:00
|
|
|
return bless {%{$obj}}, ref $obj;
|
2014-04-24 06:46:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Errors/Exceptions
|
|
|
|
|
|
|
|
{
|
|
|
|
package BlankException;
|
|
|
|
sub new { my $class = shift; bless String->new("Blank Line") => $class }
|
|
|
|
}
|
|
|
|
|
2014-04-21 08:45:58 +04:00
|
|
|
# Scalars
|
2014-04-20 00:12:13 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
|
2014-04-23 08:50:43 +04:00
|
|
|
sub _nil_Q { return $_[0] eq $nil }
|
|
|
|
sub _true_Q { return $_[0] eq $true }
|
|
|
|
sub _false_Q { return $_[0] eq $false }
|
|
|
|
|
|
|
|
|
2014-04-20 00:12:13 +04:00
|
|
|
{
|
|
|
|
package Integer;
|
2015-03-12 06:22:35 +03:00
|
|
|
sub new { my $class = shift; bless \do { my $x=$_[0] }, $class }
|
2014-04-20 00:12:13 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 06:50:52 +04:00
|
|
|
|
2014-04-20 00:12:13 +04:00
|
|
|
{
|
|
|
|
package Symbol;
|
2015-03-12 06:22:35 +03:00
|
|
|
sub new { my $class = shift; bless \do { my $x=$_[0] }, $class }
|
2014-04-20 00:12:13 +04:00
|
|
|
}
|
2014-04-22 06:47:36 +04:00
|
|
|
sub _symbol_Q { (ref $_[0]) =~ /^Symbol/ }
|
2014-04-21 06:50:52 +04:00
|
|
|
|
|
|
|
|
2014-12-19 05:33:49 +03:00
|
|
|
sub _keyword { return String->new(("\x{029e}".$_[0])); }
|
|
|
|
sub _keyword_Q { ((ref $_[0]) =~ /^String/) && ${$_[0]} =~ /^\x{029e}/; }
|
|
|
|
|
|
|
|
|
2014-04-20 00:12:13 +04:00
|
|
|
{
|
|
|
|
package String;
|
|
|
|
sub new { my $class = shift; bless \$_[0] => $class }
|
|
|
|
}
|
|
|
|
|
2014-04-21 06:50:52 +04:00
|
|
|
|
2014-04-21 08:45:58 +04:00
|
|
|
# Lists
|
|
|
|
|
2014-04-20 00:12:13 +04:00
|
|
|
{
|
|
|
|
package List;
|
2014-04-24 06:46:57 +04:00
|
|
|
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]]]); }
|
2014-04-20 00:12:13 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 08:45:58 +04:00
|
|
|
sub _list_Q { (ref $_[0]) =~ /^List/ }
|
|
|
|
|
2014-04-21 06:50:52 +04:00
|
|
|
|
2014-04-21 08:45:58 +04:00
|
|
|
# Vectors
|
2014-04-21 06:50:52 +04:00
|
|
|
|
2014-04-20 00:12:13 +04:00
|
|
|
{
|
|
|
|
package Vector;
|
2014-04-24 06:46:57 +04:00
|
|
|
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]]]); }
|
2014-04-20 00:12:13 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 08:45:58 +04:00
|
|
|
sub _vector_Q { (ref $_[0]) =~ /^Vector/ }
|
|
|
|
|
|
|
|
|
|
|
|
# Hash Maps
|
2014-04-21 06:50:52 +04:00
|
|
|
|
2014-04-20 00:12:13 +04:00
|
|
|
{
|
|
|
|
package HashMap;
|
2014-04-24 06:46:57 +04:00
|
|
|
sub new { my $class = shift; bless {'meta'=>$nil, 'val'=>$_[0]}, $class }
|
|
|
|
sub get { $_[0]->{val}->{$_[1]}; }
|
2014-04-20 00:12:13 +04:00
|
|
|
}
|
|
|
|
|
2014-04-23 08:50:43 +04:00
|
|
|
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/ }
|
|
|
|
|
2014-04-22 05:08:18 +04:00
|
|
|
|
|
|
|
# Functions
|
|
|
|
|
|
|
|
{
|
|
|
|
package Function;
|
|
|
|
sub new {
|
|
|
|
my $class = shift;
|
|
|
|
my ($eval, $ast, $env, $params) = @_;
|
2014-04-24 06:46:57 +04:00
|
|
|
bless {'meta'=>$nil,
|
|
|
|
'eval'=>$eval,
|
2014-04-22 05:08:18 +04:00
|
|
|
'ast'=>$ast,
|
|
|
|
'env'=>$env,
|
2014-04-22 06:47:36 +04:00
|
|
|
'params'=>$params,
|
|
|
|
'ismacro'=>0}, $class
|
2014-04-22 05:08:18 +04:00
|
|
|
}
|
|
|
|
sub gen_env {
|
2014-04-22 06:47:36 +04:00
|
|
|
my $self = $_[0];
|
|
|
|
return Env->new($self->{env}, $self->{params}, $_[1]);
|
2014-04-22 05:08:18 +04:00
|
|
|
}
|
|
|
|
sub apply {
|
2014-04-22 06:47:36 +04:00
|
|
|
my $self = $_[0];
|
|
|
|
return &{ $self->{eval} }($self->{ast}, gen_env($self, $_[1]));
|
2014-04-22 05:08:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-24 06:46:57 +04:00
|
|
|
|
2015-07-21 11:35:17 +03:00
|
|
|
# FunctionRef
|
|
|
|
|
|
|
|
{
|
|
|
|
package FunctionRef;
|
|
|
|
sub new {
|
|
|
|
my ($class, $code) = @_;
|
|
|
|
bless {'meta'=>$nil,
|
|
|
|
'code'=>$code}, $class
|
|
|
|
}
|
|
|
|
sub apply {
|
|
|
|
my $self = $_[0];
|
|
|
|
return &{ $self->{code} }($_[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-24 06:46:57 +04:00
|
|
|
# Atoms
|
|
|
|
|
|
|
|
{
|
|
|
|
package Atom;
|
|
|
|
sub new { my $class = shift; bless {'meta'=>$nil, 'val'=>$_[0]}, $class }
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _atom_Q { (ref $_[0]) =~ /^Atom/ }
|
|
|
|
|
2014-04-20 00:12:13 +04:00
|
|
|
1;
|