-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
125 lines (113 loc) · 4.12 KB
/
main.rs
File metadata and controls
125 lines (113 loc) · 4.12 KB
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
121
122
123
124
125
extern crate wisdom;
extern crate clap;
extern crate rustyline;
use clap::{Arg, App};
use std::io::{Write, BufReader};
use wisdom::interpreter::{Interpreter, SlowInterpreter};
use wisdom::ast::Value;
use wisdom::interpreter::error::{Error};
use std::io::{self, BufRead};
use std::fs::File;
use wisdom::common::{Position, WisdomError};
use rustyline::Editor;
fn do_write(msg: &str) {
std::io::stdout().write(msg.as_bytes()).unwrap();
std::io::stdout().flush().unwrap();
}
fn get_line(filename: &str, line: usize) -> io::Result<String> {
let file = File::open(filename)?;
let reader = BufReader::new(file);
reader.lines().nth(line).unwrap_or(Err(io::ErrorKind::NotFound.into()))
}
fn handle_err_with_line(desc: String, pos: Position, line: String) {
do_write(format!("{}\n\n ", desc).as_str());
do_write(line.as_str());
do_write("\n ");
do_write(" ".repeat(pos.column - 1).as_str());
do_write("^");
do_write("\n ");
do_write("-".repeat(pos.column - 1).as_str());
do_write("|\n");
}
fn handle(err: Error, filename: &str) {
let position = err.position();
if let Ok(line) = get_line(filename, position.line - 1) {
do_write(format!("{}:{}:{}\n", filename, position.line, position.column).as_str());
handle_err_with_line(format!("{}", err), position, line);
} else {
panic!("Error occurred when handling an error. Damn.")
}
}
// TODO: support reading from file
fn main() {
let mut interp = SlowInterpreter::new();
let mut rl = Editor::<()>::new();
let args = App::new("WELP")
.version("0.1")
.author("Giles Hutton")
.arg(
Arg::with_name("file")
.help("run a given wisdom file")
.takes_value(true)
)
.arg(
Arg::with_name("eval")
.short("e")
.help("run a given expression")
.takes_value(true)
).get_matches();
match args.value_of("file") {
Some(filename) => {
match interp.eval_file(filename) {
Err(e) => {
do_write(format!("failed to run {}\n", filename).as_str());
handle(e, filename);
}
_ => {}
}
}
None => {
match args.value_of("eval") {
Some(script) => {
match interp.eval_script(script) {
Ok(v) => do_write(format!("{}\n", v).as_str()),
Err(e) => do_write(format!("{}\n", e).as_str()),
}
}
None => {
use rustyline::error::ReadlineError::*;
do_write("Wisdom REPL (WELP) v1.0\n");
loop {
let input = rl.readline(">>> ");
match input {
Ok(line) => {
if line == "\n" || line.is_empty() {
continue;
}
rl.add_history_entry(line.clone());
match interp.eval_line(line.as_str()) {
Ok(v) => {
if v != Value::None {
do_write(format!("{}\n", v).as_str())
}
}
Err(e) => {
do_write(format!("{}\n", e).as_str())
}
}
}
Err(Interrupted) | Err(Eof) => {
do_write("Exiting.\n");
break;
}
Err(err) => {
do_write(format!("Input error: {}\n", err).as_str());
break;
}
}
}
}
}
}
}
}