Progress on test.roc

This commit is contained in:
Richard Feldman 2019-06-13 19:03:03 -04:00
parent abf3bf9df7
commit 1c3cf5f675
3 changed files with 40 additions and 23 deletions

View file

@ -16,34 +16,47 @@ fn main() -> std::io::Result<()> {
let expr = parse::parse_string(contents.as_str()).unwrap();
eval_task(expr)
}
fn eval_task(expr: Expr) -> std::io::Result<()> {
match from_evaluated(eval(expr)) {
Error(problem) => {
println!("\n\u{001B}[4mruntime error\u{001B}[24m\n\n{:?}\n", problem)
println!("\n\u{001B}[4mruntime error\u{001B}[24m\n\n{:?}\n", problem);
Ok(())
},
ApplyVariant(name, Some(exprs)) => {
ApplyVariant(name, Some(mut exprs)) => {
match name.as_str() {
"Echo" => {
let payload = exprs.first().unwrap().clone();
let payload = exprs.pop().unwrap();
let next_expr = exprs.pop().unwrap();
println!("{}", payload);
eval_task(Apply(Box::new(next_expr), vec![EmptyRecord]))
},
"Read" => {
let mut input = String::new();
io::stdin().read_line(&mut input)?;
println!("[debug] You said: {}", input);
Ok(())
},
_ => {
display_expr(ApplyVariant(name, Some(exprs)));
Ok(())
}
}
},
output => {
display_expr(output);
}
};
Ok(())
Ok(())
}
}
}
fn display_expr(expr: Expr) {

View file

@ -7,11 +7,11 @@ fail = (val) ->
echo = (str) ->
Echo str, succeed, fail
Echo fail, succeed, str
readInput =
Read succeed, fail
Read fail, succeed
map = (convert, task) ->
@ -28,28 +28,30 @@ after = (task, cont) ->
case task
when Success val then cont val
when Failure val then Failure val
when Echo str, prevCont, onFailure then
Echo str,
when Echo onFailure, prevCont, str then
Echo
(ioErr -> after (onFailure ioErr), cont),
({} -> after (prevCont {}), cont),
(ioErr -> after (onFailure ioErr), cont)
when Read prevCont, onFailure then
str
when Read onFailure, prevCont then
Read
(str -> after (prevCont str), cont),
(ioErr -> after (onFailure ioErr), cont)
(ioErr -> after (onFailure ioErr), cont),
(str -> after (prevCont str), cont)
fallback = (task, onFailure) ->
case task
when Success val then Success val
when Failure val then onFailure val
when Echo str, cont, prevOnFailure then
Echo str
when Echo prevOnFailure, cont, str then
Echo
(ioErr -> fallback (prevOnFailure ioErr), onFailure),
({} -> fallback (cont {}), onFailure),
(ioErr -> fallback (prevOnFailure ioErr), onFailure)
when Read cont, prevOnFailure then
str
when Read prevOnFailure, cont then
Read
(str -> fallback (cont str), onFailure),
(ioErr -> fallback (prevOnFailure ioErr), onFailure)
(ioErr -> fallback (prevOnFailure ioErr), onFailure),
(str -> fallback (cont str), onFailure)
demo =

View file

@ -176,8 +176,8 @@ pub fn scoped_eval(expr: Expr, vars: &Scope) -> Evaluated {
}
#[inline(always)]
fn eval_apply(expr: Evaluated, args: Vec<Expr>, vars: &Scope) -> Evaluated {
match expr {
fn eval_apply(evaluated: Evaluated, args: Vec<Expr>, vars: &Scope) -> Evaluated {
match evaluated {
Evaluated(Closure(arg_patterns, body)) => {
let evaluated_args =
args.into_iter()
@ -189,7 +189,9 @@ fn eval_apply(expr: Evaluated, args: Vec<Expr>, vars: &Scope) -> Evaluated {
Err(prob) => problem(prob)
}
},
_ => problem(TypeMismatch("Tried to call a non-function.".to_string()))
Evaluated(expr) => {
problem(TypeMismatch(format!("Tried to call a non-function: {}", expr)))
}
}
}