aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp32
1 files changed, 23 insertions, 9 deletions
diff --git a/main.cpp b/main.cpp
index 78515a6..15a877e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -3,13 +3,15 @@
#include "parser.h"
#include "eval.h"
#include "exc.h"
+#include "gc.h"
#include <cstdio>
#include <cstdlib>
-Tokenizor *tk = new Tokenizor();
-ASTGenerator *ast = new ASTGenerator();
-Evaluator *eval = new Evaluator();
+GarbageCollector gc;
+Tokenizor tk;
+ASTGenerator ast;
+Evaluator eval;
void load_file(const char *fname) {
FILE *f = fopen(fname, "r");
@@ -18,19 +20,21 @@ void load_file(const char *fname) {
printf("Can not open file: %s\n", fname);
exit(0);
}
- tk->set_stream(f);
+ tk.set_stream(f);
while (1)
{
try
{
- Pair *tree = ast->absorb(tk);
+ Pair *tree = ast.absorb(&tk);
if (!tree) break;
- eval->run_expr(tree);
+ EvalObj *ret = eval.run_expr(tree);
+ gc.expose(ret);
}
catch (GeneralError &e)
{
fprintf(stderr, "An error occured: %s\n", e.get_msg().c_str());
}
+ gc.force();
}
}
@@ -46,8 +50,15 @@ void print_help(const char *cmd) {
exit(0);
}
+EmptyList *empty_list = new EmptyList();
+UnspecObj *unspec_obj = new UnspecObj();
+
int main(int argc, char **argv) {
+ //freopen("in.scm", "r", stdin);
+ gc.attach(empty_list);
+ gc.attach(unspec_obj);
+
for (int i = 1; i < argc; i++)
{
if (*argv[i] == '-') // parsing options
@@ -79,20 +90,23 @@ int main(int argc, char **argv) {
}
int rcnt = 0;
- tk->set_stream(stdin); // interactive mode
+ tk.set_stream(stdin); // interactive mode
while (1)
{
fprintf(stderr, "Sonsi> ");
try
{
- Pair *tree = ast->absorb(tk);
+ Pair *tree = ast.absorb(&tk);
if (!tree) break;
- string output = eval->run_expr(tree)->ext_repr();
+ EvalObj *ret = eval.run_expr(tree);
+ string output = ret->ext_repr();
+ gc.expose(ret);
fprintf(stderr, "Ret> $%d = %s\n", rcnt++, output.c_str());
}
catch (GeneralError &e)
{
fprintf(stderr, "An error occured: %s\n", e.get_msg().c_str());
}
+ gc.force();
}
}