Add a self-made readline implementation
[jackhill/mal.git] / chuck / step1_read_print.ck
1 // @import readline.ck
2 // @import types/boxed/*.ck
3 // @import types/MalObject.ck
4 // @import types/mal/MalAtom.ck
5 // @import types/mal/MalError.ck
6 // @import types/mal/MalNil.ck
7 // @import types/mal/MalFalse.ck
8 // @import types/mal/MalTrue.ck
9 // @import types/mal/MalInt.ck
10 // @import types/mal/MalString.ck
11 // @import types/mal/MalSymbol.ck
12 // @import types/mal/MalKeyword.ck
13 // @import types/mal/MalList.ck
14 // @import types/mal/MalVector.ck
15 // @import types/mal/MalHashMap.ck
16 // @import util/*.ck
17 // @import reader.ck
18 // @import printer.ck
19
20 fun MalObject READ(string input)
21 {
22 return Reader.read_str(input);
23 }
24
25 fun MalObject EVAL(MalObject m)
26 {
27 return m;
28 }
29
30 fun string PRINT(MalObject m)
31 {
32 return Printer.pr_str(m, true);
33 }
34
35 fun string errorMessage(MalObject m)
36 {
37 (m$MalError).value() @=> MalObject value;
38
39 if( value.type == "string" )
40 {
41 return Printer.pr_str(value, false);
42 }
43 else
44 {
45 return "exception: " + Printer.pr_str(value, true);
46 }
47 }
48
49 fun string rep(string input)
50 {
51 READ(input) @=> MalObject m;
52
53 if( m.type == "error" )
54 {
55 return errorMessage(m);
56 }
57 else
58 {
59 return PRINT(EVAL(m));
60 }
61 }
62
63 fun void main()
64 {
65 int done;
66
67 while( !done )
68 {
69 Readline.readline("user> ") => string input;
70
71 if( input != null )
72 {
73 rep(input) => string output;
74
75 if( output == "empty input" )
76 {
77 // proceed immediately with prompt
78 }
79 else
80 {
81 Util.println(output);
82 }
83 }
84 else
85 {
86 true => done;
87 }
88 }
89 }
90
91 main();