mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 00:24:34 +00:00
Reading from stdin works
This commit is contained in:
parent
1c3cf5f675
commit
140fa5ffa9
6 changed files with 196 additions and 220 deletions
|
@ -2,9 +2,9 @@ extern crate roc;
|
|||
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use roc::expr::Expr::*;
|
||||
use roc::expr::Expr;
|
||||
use roc::eval::{eval, from_evaluated};
|
||||
use roc::eval::{Evaluated, eval, call};
|
||||
use roc::eval::Evaluated::*;
|
||||
use roc::parse;
|
||||
use std::io;
|
||||
|
||||
|
@ -16,49 +16,49 @@ fn main() -> std::io::Result<()> {
|
|||
|
||||
let expr = parse::parse_string(contents.as_str()).unwrap();
|
||||
|
||||
eval_task(expr)
|
||||
process_task(eval(expr))
|
||||
}
|
||||
|
||||
fn eval_task(expr: Expr) -> std::io::Result<()> {
|
||||
match from_evaluated(eval(expr)) {
|
||||
Error(problem) => {
|
||||
fn process_task(evaluated: Evaluated) -> std::io::Result<()> {
|
||||
match evaluated {
|
||||
EvalError(problem) => {
|
||||
println!("\n\u{001B}[4mruntime error\u{001B}[24m\n\n{:?}\n", problem);
|
||||
|
||||
Ok(())
|
||||
},
|
||||
ApplyVariant(name, Some(mut exprs)) => {
|
||||
ApplyVariant(name, Some(mut vals)) => {
|
||||
match name.as_str() {
|
||||
"Echo" => {
|
||||
let payload = exprs.pop().unwrap();
|
||||
let next_expr = exprs.pop().unwrap();
|
||||
let payload = vals.pop().unwrap();
|
||||
let callback = vals.pop().unwrap();
|
||||
|
||||
println!("{}", payload);
|
||||
|
||||
eval_task(Apply(Box::new(next_expr), vec![EmptyRecord]))
|
||||
process_task(call(callback, vec![Expr::EmptyRecord]))
|
||||
},
|
||||
"Read" => {
|
||||
let callback = vals.pop().unwrap();
|
||||
let mut input = String::new();
|
||||
|
||||
io::stdin().read_line(&mut input)?;
|
||||
|
||||
println!("[debug] You said: {}", input);
|
||||
|
||||
Ok(())
|
||||
process_task(call(callback, vec![Expr::Str(input)]))
|
||||
},
|
||||
_ => {
|
||||
display_expr(ApplyVariant(name, Some(exprs)));
|
||||
display_val(ApplyVariant(name, Some(vals)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
},
|
||||
output => {
|
||||
display_expr(output);
|
||||
display_val(output);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn display_expr(expr: Expr) {
|
||||
println!("\n\u{001B}[4mroc out\u{001B}[24m\n\n{}\n", expr);
|
||||
fn display_val(evaluated: Evaluated) {
|
||||
println!("\n\u{001B}[4mroc out\u{001B}[24m\n\n{}\n", evaluated);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue