Skip to content

Commit 5e69648

Browse files
committed
refactor compiler
1 parent dbf82c8 commit 5e69648

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ macro_rules! init_compiler {
110110
($file_path:expr) => {{
111111
let (program, file_name) = parse_program($file_path);
112112
let context = Compiler::new_master_context();
113-
let mut compiler = Compiler::new(context, program, $file_path, file_name);
113+
let compiler = Compiler::new(context, program, $file_path, file_name);
114114

115115
#[cfg(debug_assertions)]
116116
compiler.set_debug_info(true);

compiler/src/output.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::Compiler;
22
use gccjit_sys::*;
3-
use std::{ffi::CString, process::exit};
3+
use std::{
4+
ffi::CString,
5+
fs::{self, File},
6+
process::exit,
7+
};
48
use utils::compiler_error;
59

610
impl Compiler {
@@ -46,7 +50,7 @@ impl Compiler {
4650
)
4751
};
4852
}
49-
53+
5054
pub fn make_dynamic_library(&self, file_path: String) {
5155
unsafe {
5256
let file_path = CString::new(file_path).unwrap();
@@ -58,14 +62,24 @@ impl Compiler {
5862
};
5963
}
6064

65+
pub fn create_dump_file(&self, file_path: String) {
66+
if !fs::exists(file_path.clone()).unwrap() {
67+
if let Err(err) = File::create(file_path.clone()) {
68+
compiler_error!(format!("Failed to dump source code at '{}': {}", file_path, err))
69+
}
70+
}
71+
}
72+
6173
pub fn make_dump_ir(&self, file_path: String) {
74+
self.create_dump_file(file_path.clone());
6275
unsafe {
6376
let file_path = CString::new(file_path).unwrap();
6477
gcc_jit_context_dump_to_file(self.context, file_path.as_ptr(), self.cbool(true))
6578
};
6679
}
67-
80+
6881
pub fn make_dump_asm(&self, file_path: String) {
82+
self.create_dump_file(file_path.clone());
6983
unsafe {
7084
let file_path = CString::new(file_path).unwrap();
7185
gcc_jit_context_compile_to_file(

examples/main.cyr

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import std::math;
21
import std::io;
32

3+
fn sqrt(value: double): double {
4+
return value;
5+
}
6+
47
pub fn main() {
58
std::io::printf("Hello World\n");
69

0 commit comments

Comments
 (0)