-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterpret.cpp
106 lines (92 loc) · 2.49 KB
/
Interpret.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
#include <iostream>
#include "Eval.h"
#include "TypeCheck.h"
#include "Stella/Absyn.H"
#include "Stella/Printer.H"
#include "Stella/Absyn.H"
#include "Stella/Parser.H"
#include "Stella/ParserError.H"
#include "Stella/Printer.H"
void usage() {
printf("usage: Call with one of the following argument combinations:\n");
printf("\t(no arguments)\tTypecheck stdin.\n");
printf("\tPROGRAM\t\tParse content of file as Stella program, then call main with input parsed from stdin.\n");
}
enum MODE { TYPECHECK, INTERPRET } ;
int main(int argc, char ** argv)
{
FILE *input;
int quiet = 0;
char *filename = NULL;
MODE mode = TYPECHECK;
if (argc > 1) {
if (strcmp(argv[1], "typecheck") == 0) {
if (argc > 2) {
filename = argv[2];
} else {
usage();
exit(1);
}
} else if (strcmp(argv[1], "interpret") == 0) {
mode = INTERPRET;
if (argc > 2) {
filename = argv[2];
} else {
usage();
exit(1);
}
} else {
mode = INTERPRET;
filename = argv[1];
}
}
if (filename) {
input = fopen(filename, "r");
if (!input) {
printf("cannot read file: %s\n", filename);
exit(1);
}
} else {
input = stdin;
}
Stella::Program *prog = nullptr;
try {
prog = Stella::pProgram(input);
std::cout << "parsing succeeded!\n" ;
if (prog) { std::cout << "prog is not null\n" ;}
}
catch (Stella::parse_error &ex) {
std::cerr << "Parse error on line " << ex.getLine() << "\n";
}
if (filename)
fclose(input);
if (prog) {
Stella::typecheckProgram(prog);
}
if (mode == INTERPRET) {
int intInput;
std::cin >> intInput;
Stella::Expr *inputExpr = new Stella::ConstInt(0);
for (int i = 0; i < intInput; i++) {
inputExpr = new Stella::Succ(inputExpr);
}
// Stella::AProgram *inputWrapper = nullptr;
// Stella::Expr *inputExpr = nullptr;
// try {
// inputWrapper = dynamic_cast<Stella::AProgram *>(
// Stella::pProgram(("language core; fn input(x : Nat) -> Nat { return " + rawInput + " ; }").c_str())
// );
//
// inputExpr = dynamic_cast<Stella::DeclFun *>(inputWrapper->listdecl_[0][0])->expr_;
// }
// catch (Stella::parse_error &ex) {
// std::cerr << "Parse error on line " << ex.getLine() << "\n";
// }
if (inputExpr) {
auto *result = Stella::evalMainWith(prog, inputExpr);
auto printTree = Stella::PrintAbsyn();
std::cout << printTree.print(result) << std::endl;
}
}
return 0;
}