From 3e231b47158e63ad037dce4e95240f26791b6aa8 Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 18 Jan 2017 01:56:42 -0800 Subject: [PATCH] Use native rust to write source file --- src/compilation.rs | 21 ++++++++++----------- src/main.rs | 2 +- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/compilation.rs b/src/compilation.rs index 944cc8e..29dd773 100644 --- a/src/compilation.rs +++ b/src/compilation.rs @@ -1,6 +1,8 @@ extern crate llvm_sys; use std::collections::HashMap; +use std::fs::File; +use std::io::Write; use self::llvm_sys::prelude::*; use self::llvm_sys::{LLVMIntPredicate, LLVMRealPredicate}; @@ -20,7 +22,12 @@ pub fn compilation_sequence(ast: AST, sourcefile: &str) { _ => panic!("Bad filename {}", sourcefile), }; - compile_ast(ast, ll_filename, false); + let llvm_code = compile_ast(ast); + println!("Compilation process finished for {}", ll_filename); + File::create(ll_filename) + .and_then(|mut f| f.write_all(llvm_code.as_bytes())) + .expect("Error writing file"); + let llc_output = Command::new("llc") .arg("-filetype=obj") .arg(ll_filename) @@ -58,7 +65,7 @@ struct CompilationData { current_function: Option, } -pub fn compile_ast(ast: AST, filename: &str, return_string: bool) -> Option { +pub fn compile_ast(ast: AST) -> String { println!("Compiling!"); let names: VariableMap = HashMap::new(); @@ -86,20 +93,12 @@ pub fn compile_ast(ast: AST, filename: &str, return_string: bool) -> Option Repl<'a> { } if self.show_llvm_ir { - let s = compile_ast(ast, "«repl llvm»", true).unwrap(); + let s = compile_ast(ast); output.push_str(&s); } else { // for now only handle last output