perl: add conj
[jackhill/mal.git] / perl / step6_file.pl
CommitLineData
074cd748
JM
1use strict;
2use warnings FATAL => qw(all);
01c97316 3no if $] >= 5.018, warnings => "experimental::smartmatch";
f26bc011
JM
4use File::Basename;
5use lib dirname (__FILE__);
b8ee29b2 6use readline qw(mal_readline set_rl_mode);
074cd748
JM
7use feature qw(switch);
8use Data::Dumper;
9
89bd4de1 10use types qw($nil $true $false _list_Q);
074cd748
JM
11use reader;
12use printer;
13use env;
14use core qw($core_ns);
15
16# read
17sub READ {
18 my $str = shift;
19 return reader::read_str($str);
20}
21
22# eval
23sub eval_ast {
24 my($ast, $env) = @_;
25 given (ref $ast) {
26 when (/^Symbol/) {
b8ee29b2 27 $env->get($ast);
074cd748
JM
28 }
29 when (/^List/) {
89bd4de1 30 my @lst = map {EVAL($_, $env)} @{$ast->{val}};
074cd748
JM
31 return List->new(\@lst);
32 }
89bd4de1
JM
33 when (/^Vector/) {
34 my @lst = map {EVAL($_, $env)} @{$ast->{val}};
35 return Vector->new(\@lst);
36 }
37 when (/^HashMap/) {
38 my $new_hm = {};
c9de2e82 39 foreach my $k (keys( %{ $ast->{val} })) {
89bd4de1
JM
40 $new_hm->{$k} = EVAL($ast->get($k), $env);
41 }
42 return HashMap->new($new_hm);
43 }
074cd748
JM
44 default {
45 return $ast;
46 }
47 }
48}
49
50sub EVAL {
51 my($ast, $env) = @_;
52
53 while (1) {
54
55 #print "EVAL: " . printer::_pr_str($ast) . "\n";
89bd4de1 56 if (! _list_Q($ast)) {
074cd748
JM
57 return eval_ast($ast, $env);
58 }
59
60 # apply list
89bd4de1 61 my ($a0, $a1, $a2, $a3) = @{$ast->{val}};
074cd748
JM
62 given ((ref $a0) =~ /^Symbol/ ? $$a0 : $a0) {
63 when (/^def!$/) {
64 my $res = EVAL($a2, $env);
b8ee29b2 65 return $env->set($a1, $res);
074cd748
JM
66 }
67 when (/^let\*$/) {
68 my $let_env = Env->new($env);
89bd4de1 69 for(my $i=0; $i < scalar(@{$a1->{val}}); $i+=2) {
b8ee29b2 70 $let_env->set($a1->nth($i), EVAL($a1->nth($i+1), $let_env));
074cd748 71 }
89bd4de1
JM
72 $ast = $a2;
73 $env = $let_env;
6301e0b6 74 # Continue loop (TCO)
074cd748
JM
75 }
76 when (/^do$/) {
89bd4de1
JM
77 eval_ast($ast->slice(1, $#{$ast->{val}}-1), $env);
78 $ast = $ast->nth($#{$ast->{val}});
6301e0b6 79 # Continue loop (TCO)
074cd748
JM
80 }
81 when (/^if$/) {
82 my $cond = EVAL($a1, $env);
83 if ($cond eq $nil || $cond eq $false) {
84 $ast = $a3 ? $a3 : $nil;
85 } else {
86 $ast = $a2;
87 }
6301e0b6 88 # Continue loop (TCO)
074cd748
JM
89 }
90 when (/^fn\*$/) {
91 return Function->new(\&EVAL, $a2, $env, $a1);
92 }
93 default {
94 my $el = eval_ast($ast, $env);
89bd4de1 95 my $f = $el->nth(0);
074cd748
JM
96 if ((ref $f) =~ /^Function/) {
97 $ast = $f->{ast};
98 $env = $f->gen_env($el->rest());
6301e0b6 99 # Continue loop (TCO)
074cd748
JM
100 } else {
101 return &{ $f }($el->rest());
102 }
103 }
104 }
105
106 } # TCO while loop
107}
108
109# print
110sub PRINT {
111 my $exp = shift;
112 return printer::_pr_str($exp);
113}
114
115# repl
116my $repl_env = Env->new();
117sub REP {
118 my $str = shift;
119 return PRINT(EVAL(READ($str), $repl_env));
120}
121
122# core.pl: defined using perl
b8ee29b2
JM
123foreach my $n (%$core_ns) {
124 $repl_env->set(Symbol->new($n), $core_ns->{$n});
125}
126$repl_env->set(Symbol->new('eval'), sub { EVAL($_[0]->nth(0), $repl_env); });
074cd748 127my @_argv = map {String->new($_)} @ARGV[1..$#ARGV];
b8ee29b2 128$repl_env->set(Symbol->new('*ARGV*'), List->new(\@_argv));
074cd748
JM
129
130# core.mal: defined using the language itself
131REP("(def! not (fn* (a) (if a false true)))");
132REP("(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))");
133
b8ee29b2
JM
134if (scalar(@ARGV) > 0 && $ARGV[0] eq "--raw") {
135 set_rl_mode("raw");
136 shift @ARGV;
137}
89bd4de1 138if (scalar(@ARGV) > 0) {
074cd748
JM
139 REP("(load-file \"" . $ARGV[0] . "\")");
140 exit 0;
141}
142while (1) {
89bd4de1 143 my $line = mal_readline("user> ");
074cd748 144 if (! defined $line) { last; }
89bd4de1
JM
145 do {
146 local $@;
147 my $ret;
148 eval {
149 use autodie; # always "throw" errors
150 print(REP($line), "\n");
151 1;
152 } or do {
153 my $err = $@;
154 given (ref $err) {
155 when (/^BlankException/) {
156 # ignore and continue
157 }
158 default {
159 chomp $err;
160 print "Error: $err\n";
161 }
162 }
163 };
164 };
074cd748 165}