-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathmain.cpp
215 lines (191 loc) · 6.8 KB
/
main.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <limits> // for std::numeric_limits for std::cin.ignore()
#include <vector>
#include "shaka_scheme/system/lexer/rules/init.hpp"
#include "shaka_scheme/runtime/stdproc/numbers_arithmetic.hpp"
#include "shaka_scheme/runtime/stdproc/equivalence_predicates.hpp"
#include "shaka_scheme/system/exceptions/BaseException.hpp"
#include "shaka_scheme/system/exceptions/TypeException.hpp"
#include "shaka_scheme/system/vm/HeapVirtualMachine.hpp"
#include "shaka_scheme/system/vm/compiler/Compiler.hpp"
#include "shaka_scheme/system/vm/strings.hpp"
#include "shaka_scheme/system/lexer/rules/rule_token.hpp"
#include "shaka_scheme/system/parser/parser_definitions.hpp"
#include "shaka_scheme/system/parser/syntax_rules/macro_engine.hpp"
#include "shaka_scheme/system/gc/GC.hpp"
#include "shaka_scheme/system/gc/init_gc.hpp"
#include "shaka_scheme/system/base/DataPair.hpp"
#include "shaka_scheme/system/gc/mark_procedures.hpp"
int main() {
using namespace shaka;
// Given: the lexer rules are initialized
shaka::lexer::rules::init_lexer_rules();
shaka::gc::GC garbage_collector;
shaka::gc::init_create_node(garbage_collector);
std::cout << "Welcome to Shaka Scheme!" << std::endl;
bool done = false;
//shaka::Token token(shaka::Token::Type::END_OF_FILE, "\0");
//shaka::Tokenizer input(std::cin);
//while (!done) {
// std::cout << "> " << std::flush;
// try {
// token = input.get();
// std::cout << token << std::endl;
// if (token.get_type() == shaka::Token::Type::DIRECTIVE) {
// if (token.get_string() == std::string("quit")) {
// done = true;
// }
// }
// } catch (shaka::BaseException e) {
// std::cerr << e.what() << std::endl;
// std::cin.clear();
// std::cin.ignore(std::numeric_limits<int>::max());
// }
//}
std::string buf;
shaka::EnvPtr top_level = std::make_shared<shaka::Environment>(nullptr);
{
using namespace shaka;
top_level->set_value(Symbol("define"),
create_node(PrimitiveFormMarker("define")));
top_level->set_value(
Symbol("set!"),
create_node(PrimitiveFormMarker("set!")));
top_level->set_value(Symbol("lambda"),
create_node(PrimitiveFormMarker("lambda")));
top_level->set_value(
Symbol("quote"),
create_node(PrimitiveFormMarker("quote")));
top_level->set_value(Symbol("define-syntax"),
create_node(PrimitiveFormMarker("define-syntax")));
top_level->set_value(Symbol("let-syntax"),
create_node(PrimitiveFormMarker("let-syntax")));
top_level->set_value(Symbol("syntax-rules"),
create_node(PrimitiveFormMarker("syntax-rules")));
}
shaka::Closure string_append(
top_level,
nullptr,
std::vector<shaka::Symbol>(0),
std::make_shared<shaka::Callable>(shaka::string_append),
nullptr,
true);
top_level->set_value(
shaka::Symbol("string-append"),
create_node(string_append));
shaka::Closure add_numbers(
top_level,
nullptr,
std::vector<shaka::Symbol>(0),
std::make_shared<shaka::Callable>(shaka::stdproc::add),
nullptr,
true);
top_level->set_value(
shaka::Symbol("+"),
create_node(add_numbers));
shaka::Closure sub_numbers(
top_level,
nullptr,
std::vector<shaka::Symbol>(0),
std::make_shared<shaka::Callable>(shaka::stdproc::sub),
nullptr,
true);
top_level->set_value(
shaka::Symbol("-"),
create_node(sub_numbers));
shaka::Closure mul_numbers(
top_level,
nullptr,
std::vector<shaka::Symbol>(0),
std::make_shared<shaka::Callable>(shaka::stdproc::mul),
nullptr,
true);
top_level->set_value(
shaka::Symbol("*"),
create_node(mul_numbers));
shaka::Closure div_numbers(
top_level,
nullptr,
std::vector<shaka::Symbol>(0),
std::make_shared<shaka::Callable>(shaka::stdproc::div),
nullptr,
true);
top_level->set_value(
shaka::Symbol("/"),
create_node(div_numbers));
shaka::Closure display_datum(
top_level,
nullptr,
std::vector<shaka::Symbol>(0),
std::make_shared<shaka::Callable>(shaka::stdproc::display),
nullptr,
true);
top_level->set_value(
shaka::Symbol("display"),
create_node(display_datum));
shaka::Closure eqv_closure(shaka::stdproc::eqv, false);
top_level->set_value(
shaka::Symbol("eqv?"),
create_node(eqv_closure));
shaka::Closure eq_closure(shaka::stdproc::eq, false);
top_level->set_value(
shaka::Symbol("eq?"),
create_node(eq_closure));
shaka::Closure equal_closure(shaka::stdproc::equal, false);
top_level->set_value(
shaka::Symbol("equal?"),
create_node(equal_closure));
shaka::ValueRib vr;
shaka::HeapVirtualMachine hvm(nullptr, nullptr, top_level, vr, nullptr);
shaka::Compiler compiler;
shaka::Symbol halt("halt");
auto halt_instruction = shaka::core::list(create_node(halt));
while (!done) {
try {
std::cout << "> " << std::flush;
getline(std::cin, buf);
shaka::parser::ParserInput input(buf);
auto result = shaka::parser::parse_datum(input);
//std::cout << "parsed datum" << std::endl;
if (result.is_lexer_error()) {
std::cout << "LexerError: " << result << std::endl;
input.tokens.clear();
input.lex.input.clear();
break;
} else if (result.is_parser_error()) {
std::cout << "ParserError: " << result << std::endl;
continue;
} else if (result.is_incomplete()) {
std::cout << "Incomplete: " << result << std::endl;
continue;
}
shaka::Expression expr = result.it;
shaka::macro::MacroContext macro_context(hvm);
shaka::macro::run_macro_expansion(expr, macro_context);
//std::cout << "macro expanded datum" << std::endl;
//std::cout << *expr << std::endl;
shaka::Expression compiled = compiler.compile(expr, halt_instruction);
//std::cout << "compiled datum" << std::endl;
//std::cout << *compiled << std::endl;
hvm.set_expression(compiled);
do {
hvm.evaluate_assembly_instruction();
} while (shaka::core::car(hvm.get_expression())->get<shaka::Symbol>() !=
shaka::Symbol("halt"));
std::cout << *hvm.get_accumulator() << std::endl;
shaka::gc::mark(hvm);
mark_node(halt_instruction);
garbage_collector.sweep();
} catch (shaka::InvalidInputException e) {
std::cerr << "InvalidInputException: " << e.what() << std::endl;
} catch (shaka::TypeException e) {
std::cerr << "TypeException: " << e.what() << std::endl;
}
catch (std::runtime_error e) {
std::cerr << "RuntimeError: " << e.what() << std::endl;
break;
}
}
std::cout << "Exiting..." << std::endl;
return 0;
}