Hardcode Read and Echo

This commit is contained in:
Richard Feldman 2019-05-29 21:10:04 -04:00
parent 6c0e34f5fc
commit 88f8325fb3
2 changed files with 53 additions and 8 deletions

View File

@ -2,8 +2,12 @@ extern crate roc;
use std::fs::File;
use std::io::prelude::*;
use roc::expr::Expr::*;
use roc::expr::Expr;
use roc::eval::eval;
use roc::eval::from_evaluated;
use roc::parse;
use std::io;
fn main() -> std::io::Result<()> {
let mut file = File::open("test.roc")?;
@ -13,7 +17,34 @@ fn main() -> std::io::Result<()> {
let expr = parse::parse_string(contents.as_str()).unwrap();
println!("\n\u{001B}[4mroc out\u{001B}[24m\n\n{}\n", eval(expr).to_string());
match from_evaluated(eval(expr)) {
Error(problem) => {
println!("\n\u{001B}[4mruntime error\u{001B}[24m\n\n{:?}\n", problem)
},
ApplyVariant(name, payload) => {
match name.as_str() {
"Echo" => {
println!("{}", payload.unwrap().first().unwrap());
},
"Read" => {
let mut input = String::new();
io::stdin().read_line(&mut input)?;
println!("[debug] You said: {}", input);
},
_ => {
display_expr(ApplyVariant(name, payload));
}
}
},
output => {
display_expr(output);
}
};
Ok(())
}
fn display_expr(expr: Expr) {
println!("\n\u{001B}[4mroc out\u{001B}[24m\n\n{}\n", expr);
}

View File

@ -35,12 +35,12 @@ impl fmt::Display for Expr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
// PRIMITIVES
Int(num) => write!(f, "{} : Int", *num),
Int(num) => write!(f, "{}", *num),
Frac(numerator, denominator) => {
if *denominator == 10 {
write!(f, "{} : Frac", (*numerator as f64 / 10.0))
write!(f, "{}", (*numerator as f64 / 10.0))
} else {
write!(f, "{}/{} : Frac", numerator, denominator)
write!(f, "{}/{}", numerator, denominator)
}
},
Str(string) => {
@ -52,12 +52,26 @@ impl fmt::Display for Expr {
.replace("\n", "\\n")
.replace("\r", "\\r");
write!(f, "\"{}\" : String", escaped_str)
write!(f, "\"{}\"", escaped_str)
},
Char(ch) => write!(f, "'{}' : Char", *ch),
Bool(true) => write!(f, "True : Bool"),
Bool(false) => write!(f, "False : Bool"),
Char(ch) => write!(f, "'{}'", *ch),
Bool(true) => write!(f, "True"),
Bool(false) => write!(f, "False"),
Closure(args, _) => write!(f, "<{}-argument function>", args.len()),
ApplyVariant(name, opt_exprs) => {
match opt_exprs {
None => write!(f, "{}", name),
Some(exprs) => {
let contents =
exprs.into_iter()
.map(|expr| format!(" {}", expr))
.collect::<Vec<_>>()
.join(",");
write!(f, "{}{}", name, contents)
}
}
},
// ERRORS
Error(Problem::UnrecognizedVarName(name)) => write!(f, "NAMING ERROR: Unrecognized var name `{}`", name),