Finish draft of post 8 in compiler series

This commit is contained in:
Danila Fedorin 2019-11-06 21:10:53 -08:00
parent f7b5bb48d1
commit 9c34d48cdd
5 changed files with 38 additions and 5 deletions

View File

@ -1,2 +1,2 @@
defn main = { plus 320 6 }
defn plus x y = { x + y }
defn main = { sum 320 6 }
defn sum x y = { x + y }

View File

@ -5,3 +5,4 @@ defn length l = {
Cons x xs -> { 1 + length xs }
}
}
defn main = { length (Cons 1 (Cons 2 (Cons 3 Nil))) }

16
08/examples/works4.txt Normal file
View File

@ -0,0 +1,16 @@
data List = { Nil, Cons Int List }
defn add x y = { x + y }
defn mul x y = { x * y }
defn foldr f b l = {
case l of {
Nil -> { b }
Cons x xs -> { f x (foldr f b xs) }
}
}
defn main = {
foldr add 0 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil)))) +
foldr mul 1 (Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))))
}

17
08/examples/works5.txt Normal file
View File

@ -0,0 +1,17 @@
data List = { Nil, Cons Int List }
defn sumZip l m = {
case l of {
Nil -> { 0 }
Cons x xs -> {
case m of {
Nil -> { 0 }
Cons y ys -> { x + y + sumZip xs ys }
}
}
}
}
defn ones = { Cons 1 ones }
defn main = { sumZip ones (Cons 1 (Cons 2 (Cons 3 Nil))) }

View File

@ -108,12 +108,12 @@ void output_llvm(llvm_context& ctx, const std::string& filename) {
std::error_code ec;
llvm::raw_fd_ostream file(filename, ec, llvm::sys::fs::F_None);
if (ec) {
std::cerr << "Could not open output file: " << ec.message() << std::endl;
throw 0;
} else {
llvm::TargetMachine::CodeGenFileType type = llvm::TargetMachine::CGFT_ObjectFile;
llvm::legacy::PassManager pm;
if (targetMachine->addPassesToEmitFile(pm, file, NULL, type)) {
std::cerr << "Unable to emit target code" << std::endl;
throw 0;
} else {
pm.run(ctx.module);
file.close();
@ -136,7 +136,6 @@ void gen_llvm(const std::vector<definition_ptr>& prog) {
for(auto& definition : prog) {
definition->gen_llvm_second(ctx);
}
llvm::verifyModule(ctx.module);
ctx.module.print(llvm::outs(), nullptr);
output_llvm(ctx, "program.o");
}