Skip to content

Commit 6ee4326

Browse files
committed
Cleanup pl0.cc
1 parent dcdfd9f commit 6ee4326

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

pl0/pl0.cc

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// pl0.cc - PL/0 interpreter (https://en.wikipedia.org/wiki/PL/0)
2+
// pl0.cc - PL/0 language (https://en.wikipedia.org/wiki/PL/0)
33
//
44
// Copyright (c) 2015 Yuji Hirose. All rights reserved.
55
// MIT License
@@ -79,7 +79,6 @@ struct SymbolScope;
7979

8080
struct Annotation {
8181
shared_ptr<SymbolScope> scope;
82-
shared_ptr<vector<string>> freeVariables;
8382
};
8483

8584
typedef AstBase<Annotation> AstPL0;
@@ -538,20 +537,10 @@ struct LLVM {
538537
void exec() {
539538
unique_ptr<ExecutionEngine> ee(EngineBuilder(std::move(module_)).create());
540539
std::vector<GenericValue> noargs;
541-
auto fn = ee->FindFunctionNamed("__main__");
540+
auto fn = ee->FindFunctionNamed("main");
542541
auto ret = ee->runFunction(fn, noargs);
543542
}
544543

545-
static void dump(const shared_ptr<AstPL0> ast) {
546-
LLVM compiler(ast);
547-
compiler.dump();
548-
}
549-
550-
static void exec(const shared_ptr<AstPL0> ast) {
551-
LLVM compiler(ast);
552-
compiler.exec();
553-
}
554-
555544
private:
556545
LLVMContext context_;
557546
IRBuilder<> builder_;
@@ -631,7 +620,7 @@ struct LLVM {
631620

632621
void compile_program(const shared_ptr<AstPL0> ast) {
633622
auto fn = cast<Function>(module_->getOrInsertFunction(
634-
"__main__", builder_.getVoidTy(), nullptr));
623+
"main", builder_.getVoidTy(), nullptr));
635624
{
636625
auto BB = BasicBlock::Create(context_, "entry", fn);
637626
builder_.SetInsertPoint(BB);
@@ -901,14 +890,31 @@ struct LLVM {
901890
*/
902891
int main(int argc, const char** argv) {
903892
if (argc < 2) {
904-
cout << "usage: pl0 PATH [--ast]" << endl;
893+
cout << "usage: pl0 PATH [--ast] [--llvm] [--jit]" << endl;
905894
return 1;
906895
}
907896

908-
// Read a source file into memory
897+
// Parser commandline parameters
909898
auto path = argv[1];
910-
vector<char> source;
899+
bool opt_jit = false;
900+
bool opt_ast = false;
901+
bool opt_llvm = false;
902+
{
903+
auto argi = 2;
904+
while (argi < argc) {
905+
if (string("--ast") == argv[argi]) {
906+
opt_ast = true;
907+
} else if (string("--jit") == argv[argi]) {
908+
opt_jit = true;
909+
} else if (string("--llvm") == argv[argi]) {
910+
opt_llvm = true;
911+
}
912+
argi++;
913+
}
914+
}
911915

916+
// Read a source file into memory
917+
vector<char> source;
912918
ifstream ifs(path, ios::in | ios::binary);
913919
if (ifs.fail()) {
914920
cerr << "can't open the source file." << endl;
@@ -930,23 +936,6 @@ int main(int argc, const char** argv) {
930936
// Parse the source and make an AST
931937
shared_ptr<AstPL0> ast;
932938
if (parser.parse_n(source.data(), source.size(), ast, path)) {
933-
bool opt_jit = false;
934-
bool opt_ast = false;
935-
bool opt_llvm = false;
936-
{
937-
auto argi = 2;
938-
while (argi < argc) {
939-
if (string("--ast") == argv[argi]) {
940-
opt_ast = true;
941-
} else if (string("--jit") == argv[argi]) {
942-
opt_jit = true;
943-
} else if (string("--llvm") == argv[argi]) {
944-
opt_llvm = true;
945-
}
946-
argi++;
947-
}
948-
}
949-
950939
try {
951940
SymbolTable::build_on_ast(ast);
952941

@@ -955,11 +944,13 @@ int main(int argc, const char** argv) {
955944
}
956945

957946
if (opt_llvm || opt_jit) {
947+
LLVM compiler(ast);
948+
958949
if (opt_llvm) {
959-
LLVM::dump(ast);
950+
compiler.dump();
960951
}
961952
if (opt_jit) {
962-
LLVM::exec(ast);
953+
compiler.exec();
963954
}
964955
} else {
965956
Interpreter::exec(ast);

0 commit comments

Comments
 (0)