-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
75 lines (57 loc) · 1.74 KB
/
main.cpp
File metadata and controls
75 lines (57 loc) · 1.74 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
#include "CodeGenerator.h"
#include "CommandLine.h"
#include "Diagnostic.h"
#include "Parser.h"
#include "Tokenizer.h"
#include <cstring>
#include <iostream>
using namespace chibcpp;
// Command line options
static bool DumpTokens = false;
static bool DumpAST = false;
static std::string InputExpr;
static std::string OutputFile = "-";
static cl::opt_bool OptDumpTokens("dump-tokens", "Dump all tokens to stderr",
DumpTokens);
static cl::opt_bool OptDumpAST("dump-ast", "Dump the AST to stderr", DumpAST);
static cl::opt_string OptOutput("o", "Output file (default: stdout)",
OutputFile, "-");
static cl::opt_positional OptInput("expression", "Input expression to compile",
InputExpr);
int main(int Argc, char **Argv) {
// Parse command line options
if (!cl::ParseCommandLineOptions(
Argc, Argv, "chibcpp - A small C compiler inspired by chibcpp")) {
return 1;
}
const char *Input = InputExpr.c_str();
// Create diagnostic engine
DiagnosticEngine Diags(Input, "chibcpp");
// Create lexer
Lexer Lex(Input, Input + strlen(Input), Diags);
// Dump tokens if requested
if (DumpTokens) {
Lex.dumpTokens();
}
// Parse input into AST (lexer is called on-demand during parsing)
Parser P(Lex, Diags);
auto Ast = P.parse();
// Check for errors
if (Diags.hasErrorOccurred()) {
return 1;
}
// Dump AST if requested
if (DumpAST) {
std::cerr << "=== AST Dump ===\n";
Ast->dump();
std::cerr << "=== End AST Dump ===\n\n";
}
// Generate assembly code
CodeGenerator CG(Diags);
// Set output file
if (!CG.setOutputFile(OutputFile.c_str())) {
return 1;
}
CG.codegen(Ast.get());
return 0;
}