Skip to content

Commit 49b8502

Browse files
committed
Chnages after recording 2
1 parent b6e9e3f commit 49b8502

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2018"
77
[dependencies]
88
colored = "2.0"
99
rustyline = "6.2.0"
10+
rand = "0.7.3"

src/constants.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,3 @@ Commands:
2525
2626
by Kianenigma.
2727
"#;
28-
29-
pub const TEMP_FILE: &'static str = "temp.c";

src/program/mod.rs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use std::{
2-
error::Error,
32
fs::File,
43
io::Write,
54
process::{Command, Stdio},
65
};
76

8-
use crate::constants::TEMP_FILE;
9-
107
use crate::config::Config;
118

129
pub enum StatementType {
@@ -41,6 +38,7 @@ pub struct Program {
4138
pub functions: Vec<String>,
4239
pub last_push: StatementType,
4340
pub argv: String,
41+
pub ident: u32,
4442
}
4543

4644
impl Default for Program {
@@ -52,17 +50,40 @@ impl Default for Program {
5250
defines: Default::default(),
5351
argv: Default::default(),
5452
last_push: Default::default(),
53+
ident: rand::random(),
5554
}
5655
}
5756
}
5857

58+
impl Drop for Program {
59+
fn drop(&mut self) {
60+
let _ = std::fs::remove_file(self.executable_file_name());
61+
let _ = std::fs::remove_file(self.source_file_name());
62+
}
63+
}
64+
5965
impl Program {
6066
pub fn new() -> Self {
67+
let ident = rand::random::<u32>();
6168
Self {
62-
..Default::default()
69+
includes: Default::default(),
70+
functions: Default::default(),
71+
statements: Default::default(),
72+
defines: Default::default(),
73+
argv: Default::default(),
74+
last_push: Default::default(),
75+
ident,
6376
}
6477
}
6578

79+
pub fn source_file_name(&self) -> String {
80+
format!("{}.c", self.ident.to_string())
81+
}
82+
83+
pub fn executable_file_name(&self) -> String {
84+
format!("./{}.out", self.ident.to_string())
85+
}
86+
6687
pub fn push(&mut self, stmt: &str, stmt_type: StatementType) {
6788
match stmt_type {
6889
StatementType::Def => self.defines.push(String::from(stmt)),
@@ -152,23 +173,22 @@ int main(int argc, char **argv) {{
152173
let source = self.generate_source_code(false);
153174

154175
// create temp file
155-
let mut temp_source_file = match File::create(TEMP_FILE) {
156-
Err(why) => panic!("Could not create temp file [{}]", why.description()),
157-
Ok(file) => file,
158-
};
176+
let mut temp_source_file =
177+
File::create(self.source_file_name()).map_err(|err| err.to_string())?;
159178

160179
// write source to a temp file
161-
match temp_source_file.write_all(source.as_bytes()) {
162-
Err(why) => panic!("Could not write to temp file: [{}]", why.description()),
163-
Ok(_) => (),
164-
}
180+
let _ = temp_source_file
181+
.write_all(source.as_bytes())
182+
.map_err(|err| err.to_string())?;
165183

166184
// spawn a compiler
167185
let cc = config.cc.clone();
168-
let compile_handle = match Command::new(cc).arg(TEMP_FILE).output() {
169-
Err(why) => panic!("Failed spawn compiler: {}", why.description()),
170-
Ok(handle) => handle,
171-
};
186+
let compile_handle = Command::new(cc)
187+
.arg(self.source_file_name())
188+
.arg("-o")
189+
.arg(self.executable_file_name())
190+
.output()
191+
.map_err(|e| e.to_string())?;
172192

173193
let compile_stderr = String::from_utf8_lossy(&compile_handle.stderr);
174194
if compile_stderr.len() > 0 && compile_stderr.contains("error:") {
@@ -177,21 +197,14 @@ int main(int argc, char **argv) {{
177197

178198
// execute the binary
179199
let args: Vec<&str> = self.argv.split_whitespace().collect();
180-
let child = match Command::new(String::from("./a.out"))
200+
let child = Command::new(self.executable_file_name())
181201
.args(args)
182202
.stdout(Stdio::piped())
183203
.stderr(Stdio::piped())
184204
.spawn()
185-
{
186-
Ok(child) => child,
187-
Err(why) => panic!("Failed to Execute: {}", why.description()),
188-
};
205+
.map_err(|e| e.to_string())?;
189206

190-
let handle = match child.wait_with_output() {
191-
Ok(handle) => handle,
192-
Err(why) => panic!("Failed to Execute: {}", why.description()),
193-
};
194-
Ok(handle)
207+
child.wait_with_output().map_err(|e| e.to_string())
195208
}
196209
}
197210

@@ -249,10 +262,11 @@ mod tests {
249262
fn argv() {
250263
let (mut p, c) = create_dummy_program();
251264
p.push(
252-
r#"for (int i = 0; i < argc; i++) {printf("argv[%d] = %s\n", i, argv[i]);}"#,
265+
r#"for (int i = 0; i < argc; i++) { printf("argv[%d] = %s\n", i, argv[i]); }"#,
253266
StatementType::Stmt,
254267
);
255268
let handle = p.run(&c);
269+
256270
assert!(String::from_utf8_lossy(&handle.unwrap().stdout).contains("argv[0]"));
257271
}
258272

0 commit comments

Comments
 (0)