Skip to content

Commit 10b0714

Browse files
committed
All step0: add test, fix bugs, remove step0 eval.
1 parent 6457436 commit 10b0714

24 files changed

+68
-52
lines changed

bash/step0_repl.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ READ () {
55
}
66

77
EVAL () {
8-
r=
9-
eval "${1}"
8+
r="${1}"
109
}
1110

1211
PRINT () {

clojure/src/step0_repl.clj

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
;; read
66
(defn READ [& [strng]]
7-
(let [line (if strng strng (read-line))]
8-
strng))
7+
strng)
98

109
;; eval
1110
(defn EVAL [ast env]
12-
(eval (read-string ast)))
11+
ast)
1312

1413
;; print
15-
(defn PRINT [exp] exp)
14+
(defn PRINT [exp]
15+
exp)
1616

1717
;; repl
1818
(defn rep [strng] (PRINT (EVAL (READ strng), {})))

cs/step0_repl.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@ static string RE(string env, string str) {
2525
}
2626

2727
static void Main(string[] args) {
28-
string prompt = "user> ";
28+
if (args.Length > 0 && args[0] == "--raw") {
29+
Mal.readline.mode = Mal.readline.Mode.Raw;
30+
}
2931

32+
// repl loop
3033
while (true) {
3134
string line;
3235
try {
33-
line = Mal.readline.Readline(prompt);
36+
line = Mal.readline.Readline("user> ");
3437
if (line == null) { break; }
38+
if (line == "") { continue; }
3539
} catch (IOException e) {
3640
Console.WriteLine("IOException: " + e.Message);
3741
break;

js/step0_repl.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
if (typeof module !== 'undefined') {
22
var readline = require('./node_readline');
3+
var printer = require('./printer');
34
}
45

56
// read
@@ -9,7 +10,7 @@ function READ(str) {
910

1011
// eval
1112
function EVAL(ast, env) {
12-
return eval(ast);
13+
return ast;
1314
}
1415

1516
// print

lua/step0_repl.lua

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ function rep(str)
1818
return PRINT(EVAL(READ(str),""))
1919
end
2020

21+
if #arg > 0 and arg[1] == "--raw" then
22+
readline.raw = true
23+
end
24+
2125
while true do
2226
line = readline.readline("user> ")
2327
if not line then break end

miniMAL/miniMAL-core.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@
101101
["println", ["rep", "line"]],
102102
["cb"]]]],
103103
"opts", {"ignoreUndefined": true,
104-
"terminal": false},
105-
"opts", ["assoc!", "opts", ["`", "prompt"], "prompt"],
106-
"opts", ["assoc!", "opts", ["`", "eval"], "evl"]],
107-
[".", "r", ["`", "start"], "opts"]]]],
104+
"terminal": false}],
105+
["do",
106+
[".-", "opts", ["`", "prompt"], "prompt"],
107+
[".-", "opts", ["`", "eval"], "evl"],
108+
[".", "r", ["`", "start"], "opts"]]]]],
108109

109110
null
110111
]

miniMAL/step0_repl.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
["load-file", ["`", "miniMAL-core.json"]],
44

55
["def", "READ", ["fn", ["strng"],
6-
"strng"]],
6+
"strng"]],
77

88
["def", "EVAL", ["fn", ["ast", "env"],
9-
"ast"]],
9+
"ast"]],
1010

1111
["def", "PRINT", ["fn", ["exp"],
12-
"exp"]],
12+
"exp"]],
1313

1414
["def", "rep", ["fn", ["strng"],
15-
["PRINT", ["EVAL", ["READ", "strng"], null]]]],
15+
["PRINT", ["EVAL", ["READ", "strng"], null]]]],
1616

1717
["repl", ["`", "user> "], "rep"],
1818

perl/readline.pm

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ sub mal_readline {
5454
if ($rl_mode eq "terminal") {
5555
if (defined ($line = $_rl->readline($prompt))) {
5656
save_line($line);
57+
chomp $line;
5758
return $line;
5859
} else {
5960
return undef;
@@ -62,6 +63,7 @@ sub mal_readline {
6263
print "$prompt";
6364
if (defined ($line = readline(*STDIN))) {
6465
save_line($line);
66+
chomp($line);
6567
return $line;
6668
} else {
6769
return undef;

perl/step0_repl.pl

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use warnings FATAL => qw(all);
33
use File::Basename;
44
use lib dirname (__FILE__);
5-
use readline qw(mal_readline);
5+
use readline qw(mal_readline set_rl_mode);
66

77
# read
88
sub READ {
@@ -28,6 +28,9 @@ sub REP {
2828
return PRINT(EVAL(READ($str), {}));
2929
}
3030

31+
if (scalar(@ARGV) > 0 && $ARGV[0] eq "--raw") {
32+
set_rl_mode("raw");
33+
}
3134
while (1) {
3235
my $line = mal_readline("user> ");
3336
if (! defined $line) { last; }

php/step0_repl.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ function READ($str) {
99

1010
// eval
1111
function MAL_EVAL($ast, $env) {
12-
return eval($ast);
12+
return $ast;
1313
}
1414

1515
// print
1616
function MAL_PRINT($exp) {
17-
return var_export($exp, true) . "\n";
17+
return $exp;
1818
}
1919

2020
// repl
@@ -26,8 +26,8 @@ function rep($str) {
2626
do {
2727
$line = mal_readline("user> ");
2828
if ($line === NULL) { break; }
29-
if (!empty($line)) {
30-
print(rep($line));
29+
if ($line !== "") {
30+
print(rep($line) . "\n");
3131
}
3232
} while (true);
3333

php/step1_read_print.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function MAL_EVAL($ast, $env) {
1717

1818
// print
1919
function MAL_PRINT($exp) {
20-
return _pr_str($exp, True) . "\n";
20+
return _pr_str($exp, True);
2121
}
2222

2323
// repl
@@ -31,7 +31,7 @@ function rep($str) {
3131
$line = mal_readline("user> ");
3232
if ($line === NULL) { break; }
3333
if ($line !== "") {
34-
print(rep($line));
34+
print(rep($line) . "\n");
3535
}
3636
} catch (BlankException $e) {
3737
continue;

php/step2_eval.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function MAL_EVAL($ast, $env) {
4646

4747
// print
4848
function MAL_PRINT($exp) {
49-
return _pr_str($exp, True) . "\n";
49+
return _pr_str($exp, True);
5050
}
5151

5252
// repl
@@ -67,7 +67,7 @@ function rep($str) {
6767
$line = mal_readline("user> ");
6868
if ($line === NULL) { break; }
6969
if ($line !== "") {
70-
print(rep($line));
70+
print(rep($line) . "\n");
7171
}
7272
} catch (BlankException $e) {
7373
continue;

php/step3_env.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function MAL_EVAL($ast, $env) {
6363

6464
// print
6565
function MAL_PRINT($exp) {
66-
return _pr_str($exp, True) . "\n";
66+
return _pr_str($exp, True);
6767
}
6868

6969
// repl
@@ -84,7 +84,7 @@ function rep($str) {
8484
$line = mal_readline("user> ");
8585
if ($line === NULL) { break; }
8686
if ($line !== "") {
87-
print(rep($line));
87+
print(rep($line) . "\n");
8888
}
8989
} catch (BlankException $e) {
9090
continue;

php/step4_if_fn_do.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function MAL_EVAL($ast, $env) {
8181

8282
// print
8383
function MAL_PRINT($exp) {
84-
return _pr_str($exp, True) . "\n";
84+
return _pr_str($exp, True);
8585
}
8686

8787
// repl
@@ -105,7 +105,7 @@ function rep($str) {
105105
$line = mal_readline("user> ");
106106
if ($line === NULL) { break; }
107107
if ($line !== "") {
108-
print(rep($line));
108+
print(rep($line) . "\n");
109109
}
110110
} catch (BlankException $e) {
111111
continue;

php/step5_tco.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function MAL_EVAL($ast, $env) {
9393

9494
// print
9595
function MAL_PRINT($exp) {
96-
return _pr_str($exp, True) . "\n";
96+
return _pr_str($exp, True);
9797
}
9898

9999
// repl
@@ -117,7 +117,7 @@ function rep($str) {
117117
$line = mal_readline("user> ");
118118
if ($line === NULL) { break; }
119119
if ($line !== "") {
120-
print(rep($line));
120+
print(rep($line) . "\n");
121121
}
122122
} catch (BlankException $e) {
123123
continue;

php/step6_file.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function MAL_EVAL($ast, $env) {
9393

9494
// print
9595
function MAL_PRINT($exp) {
96-
return _pr_str($exp, True) . "\n";
96+
return _pr_str($exp, True);
9797
}
9898

9999
// repl
@@ -131,7 +131,7 @@ function rep($str) {
131131
$line = mal_readline("user> ");
132132
if ($line === NULL) { break; }
133133
if ($line !== "") {
134-
print(rep($line));
134+
print(rep($line) . "\n");
135135
}
136136
} catch (BlankException $e) {
137137
continue;

php/step7_quote.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function MAL_EVAL($ast, $env) {
117117

118118
// print
119119
function MAL_PRINT($exp) {
120-
return _pr_str($exp, True) . "\n";
120+
return _pr_str($exp, True);
121121
}
122122

123123
// repl
@@ -155,7 +155,7 @@ function rep($str) {
155155
$line = mal_readline("user> ");
156156
if ($line === NULL) { break; }
157157
if ($line !== "") {
158-
print(rep($line));
158+
print(rep($line) . "\n");
159159
}
160160
} catch (BlankException $e) {
161161
continue;

php/step8_macros.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ function MAL_EVAL($ast, $env) {
142142

143143
// print
144144
function MAL_PRINT($exp) {
145-
return _pr_str($exp, True) . "\n";
145+
return _pr_str($exp, True);
146146
}
147147

148148
// repl
@@ -182,7 +182,7 @@ function rep($str) {
182182
$line = mal_readline("user> ");
183183
if ($line === NULL) { break; }
184184
if ($line !== "") {
185-
print(rep($line));
185+
print(rep($line) . "\n");
186186
}
187187
} catch (BlankException $e) {
188188
continue;

php/step9_try.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function MAL_EVAL($ast, $env) {
160160

161161
// print
162162
function MAL_PRINT($exp) {
163-
return _pr_str($exp, True) . "\n";
163+
return _pr_str($exp, True);
164164
}
165165

166166
// repl
@@ -200,7 +200,7 @@ function rep($str) {
200200
$line = mal_readline("user> ");
201201
if ($line === NULL) { break; }
202202
if ($line !== "") {
203-
print(rep($line));
203+
print(rep($line) . "\n");
204204
}
205205
} catch (BlankException $e) {
206206
continue;

php/stepA_mal.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function MAL_EVAL($ast, $env) {
174174

175175
// print
176176
function MAL_PRINT($exp) {
177-
return _pr_str($exp, True) . "\n";
177+
return _pr_str($exp, True);
178178
}
179179

180180
// repl
@@ -216,7 +216,7 @@ function rep($str) {
216216
$line = mal_readline("user> ");
217217
if ($line === NULL) { break; }
218218
if ($line !== "") {
219-
print(rep($line));
219+
print(rep($line) . "\n");
220220
}
221221
} catch (BlankException $e) {
222222
continue;

python/step0_repl.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ def READ(str):
77

88
# eval
99
def EVAL(ast, env):
10-
# try it as an expression then a statement
11-
try:
12-
return eval(ast)
13-
except SyntaxError:
14-
exec(compile(ast, '', 'single'), globals())
15-
return None
10+
#print("EVAL %s" % printer._pr_str(ast))
11+
return ast
1612

1713
# print
1814
def PRINT(exp):

python/step1_read_print.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def EVAL(ast, env):
1212
#print("EVAL %s" % printer._pr_str(ast))
1313
return ast
1414

15+
# print
1516
def PRINT(exp):
1617
return printer._pr_str(exp)
1718

racket/step0_repl.rkt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env racket
22
#lang racket
33

4-
(require "types.rkt")
4+
(require "readline.rkt" "types.rkt")
55

66
;; read
77
(define (READ str)

0 commit comments

Comments
 (0)