From 9c34d48cdd003d8b88bec651a19a1cc0ec6b6472 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 6 Nov 2019 21:10:53 -0800 Subject: [PATCH] Finish draft of post 8 in compiler series --- 08/examples/works1.txt | 4 ++-- 08/examples/works3.txt | 1 + 08/examples/works4.txt | 16 ++++++++++++++++ 08/examples/works5.txt | 17 +++++++++++++++++ 08/main.cpp | 5 ++--- 5 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 08/examples/works4.txt create mode 100644 08/examples/works5.txt diff --git a/08/examples/works1.txt b/08/examples/works1.txt index bedb5d8..a2d647f 100644 --- a/08/examples/works1.txt +++ b/08/examples/works1.txt @@ -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 } diff --git a/08/examples/works3.txt b/08/examples/works3.txt index cfffd20..13514dd 100644 --- a/08/examples/works3.txt +++ b/08/examples/works3.txt @@ -5,3 +5,4 @@ defn length l = { Cons x xs -> { 1 + length xs } } } +defn main = { length (Cons 1 (Cons 2 (Cons 3 Nil))) } diff --git a/08/examples/works4.txt b/08/examples/works4.txt new file mode 100644 index 0000000..7674691 --- /dev/null +++ b/08/examples/works4.txt @@ -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)))) +} diff --git a/08/examples/works5.txt b/08/examples/works5.txt new file mode 100644 index 0000000..3a8aff5 --- /dev/null +++ b/08/examples/works5.txt @@ -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))) } diff --git a/08/main.cpp b/08/main.cpp index 47fdec4..3f50c32 100644 --- a/08/main.cpp +++ b/08/main.cpp @@ -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& 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"); }