Fix eval-ing functions in repl

This commit is contained in:
Richard Feldman 2022-10-30 14:35:40 -04:00
parent eebf973f11
commit d7fd72c905
No known key found for this signature in database
GPG key ID: F1F21AA5B1D9E43B
3 changed files with 34 additions and 19 deletions

View file

@ -58,15 +58,7 @@ pub fn gen_and_eval_llvm<'a>(
let (_, main_fn_layout) = match loaded.procedures.keys().find(|(s, _)| *s == main_fn_symbol) {
Some(layout) => *layout,
None => {
return (
Some(ReplOutput {
expr: "<function>".to_string(),
expr_type: expr_type_str,
}),
problems,
);
}
None => unreachable!(),
};
let interns = loaded.interns.clone();
@ -87,8 +79,15 @@ pub fn gen_and_eval_llvm<'a>(
layout_interner.into_global().fork(),
target_info,
);
let expr_str = format_answer(&arena, expr).to_string();
(Some(format_answer(&arena, expr, expr_type_str)), problems)
(
Some(ReplOutput {
expr: expr_str,
expr_type: expr_type_str,
}),
problems,
)
}
struct CliApp {

View file

@ -13,7 +13,7 @@ use roc_mono::layout::{
self, union_sorted_tags_pub, Builtin, Layout, LayoutCache, LayoutInterner, UnionLayout,
UnionVariant, WrappedVariant,
};
use roc_parse::ast::{AssignedField, Collection, Expr, StrLiteral};
use roc_parse::ast::{AssignedField, Collection, Expr, Pattern, StrLiteral};
use roc_region::all::{Loc, Region};
use roc_std::RocDec;
use roc_target::TargetInfo;
@ -67,7 +67,20 @@ pub fn jit_to_ast<'a, A: ReplApp<'a>>(
// it's `main` and can be executed.
jit_to_ast_help(&mut env, app, main_fn_name, &result, var)
}
_ => Expr::MalformedClosure,
ProcLayout { arguments, .. } => {
// This is a user-supplied function; create a fake Expr for it.
let mut arg_patterns =
bumpalo::collections::Vec::with_capacity_in(arguments.len(), arena);
// Put in an underscore for each of the args, just to get the arity right.
for _ in 0..arguments.len() {
arg_patterns.push(Loc::at_zero(Pattern::Underscore("_")));
}
let body_expr = Loc::at_zero(Expr::Record(Collection::empty()));
Expr::Closure(arg_patterns.into_bump_slice(), arena.alloc(body_expr))
}
}
}

View file

@ -11,23 +11,26 @@ use roc_region::all::LineInfo;
use roc_reporting::report::{can_problem, type_problem, RocDocAllocator};
use roc_target::TargetInfo;
#[derive(Debug)]
pub struct ReplOutput {
pub expr: String,
pub expr_type: String,
}
pub fn format_answer(arena: &Bump, answer: Expr<'_>, expr_type: String) -> ReplOutput {
let mut expr = roc_fmt::Buf::new_in(arena);
pub fn format_answer<'a>(arena: &'a Bump, answer: Expr<'_>) -> &'a str {
match answer {
Expr::Closure(_, _) | Expr::MalformedClosure => "<function>",
_ => {
let mut expr = roc_fmt::Buf::new_in(arena);
answer.format_with_options(&mut expr, Parens::NotNeeded, Newlines::Yes, 0);
answer.format_with_options(&mut expr, Parens::NotNeeded, Newlines::Yes, 0);
ReplOutput {
expr: expr.into_bump_str().to_string(),
expr_type,
expr.into_bump_str()
}
}
}
#[derive(Default)]
#[derive(Default, Debug)]
pub struct Problems {
pub errors: Vec<String>,
pub warnings: Vec<String>,