mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 08:34:33 +00:00
Hardcode Read and Echo
This commit is contained in:
parent
6c0e34f5fc
commit
88f8325fb3
2 changed files with 53 additions and 8 deletions
|
@ -2,8 +2,12 @@ extern crate roc;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
|
use roc::expr::Expr::*;
|
||||||
|
use roc::expr::Expr;
|
||||||
use roc::eval::eval;
|
use roc::eval::eval;
|
||||||
|
use roc::eval::from_evaluated;
|
||||||
use roc::parse;
|
use roc::parse;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
let mut file = File::open("test.roc")?;
|
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();
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn display_expr(expr: Expr) {
|
||||||
|
println!("\n\u{001B}[4mroc out\u{001B}[24m\n\n{}\n", expr);
|
||||||
|
}
|
||||||
|
|
28
src/expr.rs
28
src/expr.rs
|
@ -35,12 +35,12 @@ impl fmt::Display for Expr {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
// PRIMITIVES
|
// PRIMITIVES
|
||||||
Int(num) => write!(f, "{} : Int", *num),
|
Int(num) => write!(f, "{}", *num),
|
||||||
Frac(numerator, denominator) => {
|
Frac(numerator, denominator) => {
|
||||||
if *denominator == 10 {
|
if *denominator == 10 {
|
||||||
write!(f, "{} : Frac", (*numerator as f64 / 10.0))
|
write!(f, "{}", (*numerator as f64 / 10.0))
|
||||||
} else {
|
} else {
|
||||||
write!(f, "{}/{} : Frac", numerator, denominator)
|
write!(f, "{}/{}", numerator, denominator)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Str(string) => {
|
Str(string) => {
|
||||||
|
@ -52,12 +52,26 @@ impl fmt::Display for Expr {
|
||||||
.replace("\n", "\\n")
|
.replace("\n", "\\n")
|
||||||
.replace("\r", "\\r");
|
.replace("\r", "\\r");
|
||||||
|
|
||||||
write!(f, "\"{}\" : String", escaped_str)
|
write!(f, "\"{}\"", escaped_str)
|
||||||
},
|
},
|
||||||
Char(ch) => write!(f, "'{}' : Char", *ch),
|
Char(ch) => write!(f, "'{}'", *ch),
|
||||||
Bool(true) => write!(f, "True : Bool"),
|
Bool(true) => write!(f, "True"),
|
||||||
Bool(false) => write!(f, "False : Bool"),
|
Bool(false) => write!(f, "False"),
|
||||||
Closure(args, _) => write!(f, "<{}-argument function>", args.len()),
|
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
|
// ERRORS
|
||||||
Error(Problem::UnrecognizedVarName(name)) => write!(f, "NAMING ERROR: Unrecognized var name `{}`", name),
|
Error(Problem::UnrecognizedVarName(name)) => write!(f, "NAMING ERROR: Unrecognized var name `{}`", name),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue