This repository was archived by the owner on Sep 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
176 lines (174 loc) · 6.55 KB
/
main.cpp
File metadata and controls
176 lines (174 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
#ifndef NOMINMAX
#define NOMINMAX
#endif // !NOMINMAX
#define TokenType Win32TokenType
#include <windows.h>
#undef TokenType
#include <conio.h>
#include "error.h"
#include "tokenizer.h"
#include "value.h"
#include "parser.h"
#include "eval_env.h"
#include "forms.h"
#include "render.h"
using namespace std::literals;
int main(int argc, char** argv) {
SetConsoleOutputCP(CP_UTF8);
std::istream* ist{&std::cin};
std::ifstream ifs;
//check args and open file
if (argc == 3) {
if ((argv[1] == "--input"s) || (argv[1]== "-i"s)) {
ifs.open(argv[2], std::ios::in);
if (!ifs.is_open()) {
std::cerr << "Error: Can't open the file '" << argv[2]
<< "'." << std::endl;
std::exit(1);
}
ist = &ifs;
} else {
std::cerr << "Error: Input Syntax Error." << std::endl;
std::exit(1);
}
}
else if(argc != 1) {
std::cerr << "Input Syntax Error." << std::endl;
std::exit(1);
}
//main loop
std::stack<bool> checkBracket;
std::string expr;
auto env = EvalEnv::createGlobal();
Render render{env};
std::string input;//only for filemode
if (argc == 1) std::cout << ">>> ";
while (true) {
try {
//repl mode
if (argc == 1){
char ch = _getch();
if (ch == 13) {//end of line
render.render(render.getInput(), 0);
std::cout << '\n';
expr = expr + render.getInput() + " ";
for (auto& c : render.getInput()) {
if (c == '(')
checkBracket.push(true);
else if (c == ')')
if (checkBracket.empty())
throw LispError("Mismatched Parentheses.");//fix
else
checkBracket.pop();
}
//evaluate expression
if (checkBracket.empty()) {
auto tokens = Tokenizer::tokenize(expr);
expr = "";
Parser parser(std::move(tokens));
auto value = parser.parse();
auto result = env->eval(std::move(value));
std::cout << result->toString() << std::endl << ">>> ";
} else {
std::cout << "...\t";
}
render.init();
} else if (ch == '\b') {//backspace
if (render.getTailLen() < render.getInput().length()) {
auto input0 = render.getInput();
int len0 = input0.length() - render.getTailLen() - 1;
render.render(input0.substr(0, len0) +
input0.substr(len0 + 1, render.getTailLen()),
render.getTailLen());
}
} else if (int(ch) == 26) {// ctrl z
std::exit(0);
} else if (int(ch) == -32) {//arrow 75 left 77 right 72 up 80 down
ch = _getch();
int tailLen0 = render.getTailLen();
auto input0 = render.getInput();
if (int(ch) == 75 && tailLen0 < input0.length())
render.render(input0, tailLen0 + 1);
if (int(ch) == 77 && tailLen0 > 0)
render.render(input0, tailLen0 - 1);
if (int(ch) == 72 && render.historyPos > 0) {
--render.historyPos;
render.render(render.getHistory(), 0);
}
if (int(ch) == 80 &&
render.historyPos < render.getHistoryLen() - 1) {
++render.historyPos;
render.render(render.getHistory(), 0);
}
} else {
auto input0 = render.getInput();
int tailLen0 = render.getTailLen();
int len0 = input0.length() - tailLen0;
render.render(input0.substr(0, len0) +
std::string{ch} +
input0.substr(len0,tailLen0),
tailLen0);
}
}
//file mode
else {
std::string line;
if (ist->eof()) {
if (!checkBracket.empty()) {
throw LispError("Mismatched Parentheses.");
} else
std::exit(0);
}
std::getline(*ist, line);
input += line;
if (!input.empty()) {
for (auto& c : line) {
if (c == '(')
checkBracket.push(true);
else if (c == ')')
if (checkBracket.empty())
throw LispError("Mismatched Parentheses.");
else
checkBracket.pop();
}
if (checkBracket.empty()) {
auto tokens = Tokenizer::tokenize(input);
input = "";
Parser parser(std::move(tokens));
auto value = parser.parse();
auto result = env->eval(std::move(value));
} else {
input += " ";
}
}
}
} catch (std::runtime_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
if (argc != 1)
std::exit(1);
else {
render.init();
std::cout << ">>> ";
}
}
}
}
//#include "rjsj_test.hpp"
//struct TestCtx {
// std::shared_ptr<EvalEnv> env = EvalEnv::createGlobal();
// std::string eval(std::string input) {
// auto tokens = Tokenizer::tokenize(input);
// Parser parser(std::move(tokens));
// auto value = parser.parse();
// auto result = env->eval(std::move(value));
// return result->toString();
// }
//};
//int main() {
// RJSJ_TEST(TestCtx, Lv2, Lv3, Lv4, Lv5, Lv5Extra, Lv6, Lv7, Lv7Lib, Sicp);
// // [...]
//}