rustfmt cli/

This commit is contained in:
Richard Feldman 2019-09-18 19:00:01 -04:00
parent b0338d06dc
commit 08f9eb883d

View file

@ -1,13 +1,13 @@
extern crate roc; extern crate roc;
use std::fs::File;
use std::io::prelude::*;
use roc::expr::Expr;
use roc::eval::{Evaluated, eval, call};
use roc::eval::Evaluated::*; use roc::eval::Evaluated::*;
use roc::eval::{call, eval, Evaluated};
use roc::expr::Expr;
use roc::parse; use roc::parse;
use roc::region::{Region, Located}; use roc::region::{Located, Region};
use std::fs::File;
use std::io; use std::io;
use std::io::prelude::*;
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
let argv = std::env::args().into_iter().collect::<Vec<String>>(); let argv = std::env::args().into_iter().collect::<Vec<String>>();
@ -22,7 +22,7 @@ fn main() -> std::io::Result<()> {
let expr = parse::parse_string(contents.as_str()).unwrap(); let expr = parse::parse_string(contents.as_str()).unwrap();
process_task(eval(expr)) process_task(eval(expr))
}, }
None => { None => {
println!("Usage: roc FILENAME.roc"); println!("Usage: roc FILENAME.roc");
@ -34,11 +34,14 @@ fn main() -> std::io::Result<()> {
fn process_task(evaluated: Evaluated) -> std::io::Result<()> { fn process_task(evaluated: Evaluated) -> std::io::Result<()> {
match evaluated { match evaluated {
EvalError(region, problem) => { EvalError(region, problem) => {
println!("\n\u{001B}[4mruntime error\u{001B}[24m\n\n{} at {}\n", println!(
format!("{}", problem), format!("line {}, column {}", region.start_line, region.start_col)); "\n\u{001B}[4mruntime error\u{001B}[24m\n\n{} at {}\n",
format!("{}", problem),
format!("line {}, column {}", region.start_line, region.start_col)
);
Ok(()) Ok(())
}, }
ApplyVariant(name, Some(mut vals)) => { ApplyVariant(name, Some(mut vals)) => {
match name.as_str() { match name.as_str() {
"Echo" => { "Echo" => {
@ -51,9 +54,13 @@ fn process_task(evaluated: Evaluated) -> std::io::Result<()> {
format!("{}", err), format!("{}", err),
format!("line {}, column {}", region.start_line, region.start_col) format!("line {}, column {}", region.start_line, region.start_col)
); );
}, }
Some(val) => { panic!("TYPE MISMATCH in Echo: {}", format!("{}", val)); }, Some(val) => {
None => { panic!("TYPE MISMATCH in Echo: None"); } panic!("TYPE MISMATCH in Echo: {}", format!("{}", val));
}
None => {
panic!("TYPE MISMATCH in Echo: None");
}
}; };
// Print the string to the console, since that's what Echo does! // Print the string to the console, since that's what Echo does!
@ -62,14 +69,17 @@ fn process_task(evaluated: Evaluated) -> std::io::Result<()> {
// Continue with the callback. // Continue with the callback.
let callback = vals.pop().unwrap(); let callback = vals.pop().unwrap();
process_task( process_task(call(
call( Region {
Region { start_line: 0, start_col: 0, end_line: 0, end_col: 0 }, start_line: 0,
callback, start_col: 0,
vec![with_zero_loc(Expr::EmptyRecord)] end_line: 0,
) end_col: 0,
) },
}, callback,
vec![with_zero_loc(Expr::EmptyRecord)],
))
}
"Read" => { "Read" => {
// Read a line from from stdin, since that's what Read does! // Read a line from from stdin, since that's what Read does!
let mut input = String::new(); let mut input = String::new();
@ -79,18 +89,21 @@ fn process_task(evaluated: Evaluated) -> std::io::Result<()> {
// Continue with the callback. // Continue with the callback.
let callback = vals.pop().unwrap(); let callback = vals.pop().unwrap();
process_task( process_task(call(
call( Region {
Region { start_line: 0, start_col: 0, end_line: 0, end_col: 0 }, start_line: 0,
callback, start_col: 0,
vec![with_zero_loc(Expr::Str(input.trim().to_string()))] end_line: 0,
) end_col: 0,
) },
}, callback,
vec![with_zero_loc(Expr::Str(input.trim().to_string()))],
))
}
"Success" => { "Success" => {
// We finished all our tasks. Great! No need to print anything. // We finished all our tasks. Great! No need to print anything.
Ok(()) Ok(())
}, }
_ => { _ => {
// We don't recognize this variant, so display it and exit. // We don't recognize this variant, so display it and exit.
display_val(ApplyVariant(name, Some(vals))); display_val(ApplyVariant(name, Some(vals)));
@ -98,7 +111,7 @@ fn process_task(evaluated: Evaluated) -> std::io::Result<()> {
Ok(()) Ok(())
} }
} }
}, }
output => { output => {
// We don't recognize this value, so display it and exit. // We don't recognize this value, so display it and exit.
display_val(output); display_val(output);
@ -109,16 +122,18 @@ fn process_task(evaluated: Evaluated) -> std::io::Result<()> {
} }
fn with_zero_loc<T>(val: T) -> Located<T> { fn with_zero_loc<T>(val: T) -> Located<T> {
Located::new(val, Region { Located::new(
start_line: 0, val,
start_col: 0, Region {
start_line: 0,
start_col: 0,
end_line: 0, end_line: 0,
end_col: 0, end_col: 0,
}) },
)
} }
fn display_val(evaluated: Evaluated) { fn display_val(evaluated: Evaluated) {
println!("\n\u{001B}[4mroc out\u{001B}[24m\n\n{}\n", evaluated); println!("\n\u{001B}[4mroc out\u{001B}[24m\n\n{}\n", evaluated);
} }