-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
86 lines (70 loc) · 1.92 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
/*************************************************
Author: hehez
Date: 4/23/17
Description:
**************************************************/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include "AstPrinter.h"
#include "CodeGen.h"
void fatal(const char *, const char *);
void error(const char *fmt, ...);
const char *pgm_name;
int errCnt = 0;
#ifdef YYDEBUG
extern int yydebug;
#endif
extern FILE *yyin;
extern int yylineno;
int yyparse (void);
// root of the abstract syntax tree for the expression
ClassDeclaration *expTree;
int errorNum;
ofstream jsm;
string fileName;
int main(int argc, const char **argv) {
pgm_name = argv[0];
#ifdef YYDEBUG
yydebug = 1;
#endif
if(argc > 1) {
yyin = fopen(argv[1], "r");
fileName = string(argv[1]);
if(yyin == NULL)
fatal("Cannot open input file: ", argv[1]);
}
// parse an expression and construct a syntax tree for it
// the root of the tree will be available as expTree
expTree = NULL;
//symTab = new SymTab();
errorNum = 0;
// yyparse();
int result = yyparse();
printf("Result of parsing: %d\n", result);
// now, visit the tree using the AstPrinter visitor
// but only if there were no errors
if(errCnt == 0 && expTree != NULL) {
expTree->accept(new AstPrinter (0));
if(errorNum == 0) {
expTree->accept(new CodeGen(0));
cout << "Generated: jsm.txt" << endl;
} else {
remove("jsm.txt");
}
}
return 0;
}
void error(const char *fmt, ...) {
va_list ap;
errCnt++;
va_start(ap, fmt);
fprintf(stderr, "Line %d: ", yylineno);
vfprintf(stderr, fmt, ap);
va_end(ap);
fflush(stderr);
}
void fatal(const char *msg1, const char *msg2) {
printf("%s%s\n", msg1, msg2);
exit(1);
}