Merge pull request #256 from vvakame/impl-ts
[jackhill/mal.git] / cpp / step1_read_print.cpp
CommitLineData
8bd091e3
ST
1#include "MAL.h"
2
3#include "ReadLine.h"
4#include "Types.h"
5
6#include <iostream>
7#include <memory>
8
9malValuePtr READ(const String& input);
10String PRINT(malValuePtr ast);
11
12static ReadLine s_readLine("~/.mal-history");
13
179e8eaf
ST
14static String rep(const String& input);
15static malValuePtr EVAL(malValuePtr ast);
16
8bd091e3
ST
17int main(int argc, char* argv[])
18{
19 String prompt = "user> ";
20 String input;
21 while (s_readLine.get(prompt, input)) {
22 String out;
23 try {
24 out = rep(input);
25 }
26 catch (malEmptyInputException&) {
27 continue; // no output
28 }
29 catch (String& s) {
30 out = s;
31 };
32 std::cout << out << "\n";
33 }
34 return 0;
35}
36
179e8eaf 37static String rep(const String& input)
8bd091e3
ST
38{
39 return PRINT(EVAL(READ(input)));
40}
41
42malValuePtr READ(const String& input)
43{
44 return readStr(input);
45}
46
179e8eaf 47static malValuePtr EVAL(malValuePtr ast)
8bd091e3
ST
48{
49 return ast;
50}
51
52String PRINT(malValuePtr ast)
53{
54 return ast->print(true);
55}
179e8eaf
ST
56
57// These have been added after step 1 to keep the linker happy.
dc9b184b 58malValuePtr EVAL(malValuePtr ast, malEnvPtr)
179e8eaf
ST
59{
60 return ast;
61}
62
494c1608 63malValuePtr APPLY(malValuePtr ast, malValueIter, malValueIter)
179e8eaf
ST
64{
65 return ast;
66}