aboutsummaryrefslogtreecommitdiff
path: root/main.cpp
blob: 28bd27d8c52845db00452334627b6c6f0bcd2e44 (plain) (blame)
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
#include "model.h"
#include "builtin.h"
#include "parser.h"
#include "eval.h"
#include "exc.h"
#include <cstdio>

int main(int argc, char **argv) {
    //freopen("in.scm", "r", stdin);
    Tokenizor *tk = new Tokenizor();
    ASTGenerator *ast = new ASTGenerator();
    Evaluator *eval = new Evaluator();

    bool interactive = false;
    bool preload = false;
    char *fname;

    int rcnt = 0;
    for (int i = 1; i < argc; i++)
        if (strcmp(argv[i], "-i") == 0)
            interactive = true;
        else if (strcmp(argv[i], "-l") == 0 && i < argc - 1)
        {
            preload = true;
            fname = argv[i + 1];
        }

    if (preload)
    {
        FILE *f = fopen(fname, "r");
        if (!f)
        {
            printf("Can not open file: %s\n", fname);
            return 0;
        }
        tk->set_stream(f);
        while (1)
        {
            try
            {
                Pair *tree = ast->absorb(tk);
                if (!tree) break;
                eval->run_expr(tree)->ext_repr().c_str();
            }
            catch (GeneralError &e)
            {
                fprintf(stderr, "An error occured: %s\n", e.get_msg().c_str());
            }
        }
        interactive = true;
    }
    tk->set_stream(stdin); 
    while (1)
    {
        if (interactive)
        fprintf(stderr, "Sonsi> ");
        try
        {
            Pair *tree = ast->absorb(tk);
            if (!tree) break;
            string output = eval->run_expr(tree)->ext_repr();
            if (interactive)
                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());
        }
    }
}