-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.rs
74 lines (63 loc) · 1.5 KB
/
compiler.rs
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
use std::env;
use std::fs;
use std::io::{self, Write};
use std::process;
use crate::ukiyoCompiler::Ukiyo;
fn main() {
let args: Vec<String> = env::args().collect();
match args.len() {
1 => run_prompt(),
2 => run_file(&args[1]),
_ => {
println!("Usage: ukiyoCompiler [script]");
process::exit(64);
}
}
}
fn run_file(path: &String) {
let contents = fs::read_to_string(path)
.expect("Something went wrong reading the file");
run(&contents);
if had_error() {
process::exit(65);
}
}
fn run_prompt() {
let stdin = io::stdin();
loop {
print!("> ");
io::stdout().flush().unwrap();
let mut line = String::new();
stdin.read_line(&mut line).unwrap();
if line.is_empty() { break; }
run(&line);
}
}
fn run(source: &String) {
let tokens = scan_tokens(source);
for token in tokens {
println!("{:?}", token);
}
}
fn scan_tokens(source: &String) -> Vec<Token> {
// TODO: Implement the Scanner logic here
vec![]
}
fn had_error() -> bool {
// TODO: Add your error handling logic here
false
}
#[derive(Debug)]
struct Token {
// TODO: Define the Token structure here
}
static mut HAD_ERROR: bool = false;
fn error(line: i32, message: &str) {
report(line, "", message);
}
fn report(line: i32, location: &str, message: &str) {
eprintln!("[line {}] Error{}: {}", line, location, message);
unsafe {
HAD_ERROR = true;
}
}