1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
extern crate regex;
use self::regex::Regex;
pub mod interpreter_error;
use interpreter::interpreter_error::*;
use errors::Error;
use std::io::Read;
use std::fs::File;
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum OptimizationLevel {
Debug,
Release,
SuperCompiler,
}
impl Default for OptimizationLevel {
fn default() -> Self { OptimizationLevel::Debug }
}
#[derive(Debug)]
pub struct Interpreter<'source> {
pub file_name : Option<&'source str>,
pub contents : String,
pub interactive: bool,
pub level : OptimizationLevel,
pub output : Option<File>,
}
impl<'source> Interpreter<'source> {
pub fn new_interactive() -> Interpreter<'source> {
Interpreter {
interactive: true,
file_name: None,
contents: String::from(""),
level: OptimizationLevel::Debug,
output: None,
}
}
pub fn new(file_name: &'source str, level: OptimizationLevel, out: Option<&'source str>) -> Option<Interpreter<'source>> {
let mut buf: String = String::new();
match File::open(file_name) {
Ok(mut file_handle) => {
match file_handle.read_to_string(&mut buf) {
Ok(_) => {},
Err(_) => {
InterpreterError {
file_name,
reason: "Could not read input file. (Was it valid UTF-8?)",
}.display();
return None;
}
};
},
Err(_) => {
InterpreterError {
file_name, reason: "Could not open input file. (Does it exist?)",
}.display();
return None;
},
};
let mut output_name: String = match out {
Some(n) => n,
None => file_name,
}.to_string();
let out_re = Regex::new(r"[[:word:]].class$").unwrap();
if !out_re.is_match(&output_name) {
output_name.push_str(".class");
};
let out_file: File;
match File::create(&output_name) {
Ok(f) => {out_file = f;},
Err(_) => {
InterpreterError {
file_name: &output_name,
reason: "Could not create or open output file.",
}.display();
return None;
},
};
Some(Interpreter {
contents: buf,
file_name: Some(file_name),
interactive: false,
level,
output: Some(out_file),
})
}
pub fn run(&self) -> i32 { unimplemented!() }
}