-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandler.cpp
More file actions
79 lines (71 loc) · 1.93 KB
/
Handler.cpp
File metadata and controls
79 lines (71 loc) · 1.93 KB
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
//===----------------------------------------------------------------------===//
// Top-Level parsing and JIT Driver
//===----------------------------------------------------------------------===//
#include "Handler.h"
#include "Parser.h"
#include "AST.h"
LLVMContext TheContext;
std::unique_ptr<Module> TheModule;
std::map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos;
Handler::Handler() {
parser = new Parser();
}
void Handler::InitializeModuleAndPassManager() {
// Open a new module.
TheModule = llvm::make_unique<Module>("my cool jit", TheContext);
}
void Handler::HandleDefinition() {
if (auto FnAST = parser->ParseDefinition()) {
if (auto *FnIR = FnAST->codegen()) {
fprintf(stderr, "Read function definition:");
FnIR->print(errs());
fprintf(stderr, "\n");
}
} else {
// Skip token for error recovery.
parser->getNextToken();
}
}
void Handler::HandleExtern() {
if (auto ProtoAST = parser->ParseExtern()) {
if (auto *FnIR = ProtoAST->codegen()) {
fprintf(stderr, "Read extern: ");
FnIR->print(errs());
fprintf(stderr, "\n");
FunctionProtos[ProtoAST->getName()] = std::move(ProtoAST);
}
} else {
// Skip token for error recovery.
parser->getNextToken();
}
}
void Handler::HandleTopLevelExpression() {
// Evaluate a top-level expression into an anonymous function.
if (auto FnAST = parser->ParseTopLevelExpr()) {
FnAST->codegen();
} else {
// Skip token for error recovery.
parser->getNextToken();
}
}
/// top ::= definition | external | expression | ';'
void Handler::MainLoop() {
while (true) {
switch (parser->getCurTok()) {
case tok_eof:
return;
case ';': // ignore top-level semicolons.
parser->getNextToken();
break;
case tok_def:
HandleDefinition();
break;
case tok_extern:
HandleExtern();
break;
default:
HandleTopLevelExpression();
break;
}
}
}