Add plumbing for displaying RocDec in the repl.

This commit is contained in:
Derek Gustafson 2022-03-04 17:32:09 -05:00
parent 874ce855bc
commit dc92de7781
No known key found for this signature in database
GPG key ID: 8B8B15EF3CC8410B
6 changed files with 61 additions and 18 deletions

View file

@ -13,6 +13,7 @@ use roc_mono::layout::{
};
use roc_parse::ast::{AssignedField, Collection, Expr, StrLiteral};
use roc_region::all::{Loc, Region};
use roc_std::RocDec;
use roc_target::TargetInfo;
use roc_types::subs::{Content, FlatType, GetSubsSlice, RecordFields, Subs, UnionTags, Variable};
@ -277,6 +278,14 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
layout: &Layout<'a>,
content: &'a Content,
) -> Result<Expr<'a>, ToAstProblem> {
macro_rules! helper {
($ty:ty) => {
app.call_function(main_fn_name, |_, num: $ty| {
num_to_ast(env, number_literal_to_ast(env.arena, num), content)
})
};
}
let (newtype_containers, content) = unroll_newtypes(env, content);
let content = unroll_aliases(env, content);
let result = match layout {
@ -287,14 +296,6 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
Layout::Builtin(Builtin::Int(int_width)) => {
use IntWidth::*;
macro_rules! helper {
($ty:ty) => {
app.call_function(main_fn_name, |_, num: $ty| {
num_to_ast(env, number_literal_to_ast(env.arena, num), content)
})
};
}
let result = match int_width {
U8 | I8 => {
// NOTE: `helper!` does not handle 8-bit numbers yet
@ -317,14 +318,6 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
Layout::Builtin(Builtin::Float(float_width)) => {
use FloatWidth::*;
macro_rules! helper {
($ty:ty) => {
app.call_function(main_fn_name, |_, num: $ty| {
num_to_ast(env, number_literal_to_ast(env.arena, num), content)
})
};
}
let result = match float_width {
F32 => helper!(f32),
F64 => helper!(f64),
@ -333,6 +326,9 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
Ok(result)
}
Layout::Builtin(Builtin::Decimal) => {
Ok(helper!(RocDec))
}
Layout::Builtin(Builtin::Str) => {
let size = layout.stack_size(env.target_info) as usize;
Ok(
@ -1246,5 +1242,9 @@ fn num_to_ast<'a>(env: &Env<'a, '_>, num_expr: Expr<'a>, content: &Content) -> E
/// This is centralized in case we want to format it differently later,
/// e.g. adding underscores for large numbers
fn number_literal_to_ast<T: std::fmt::Display>(arena: &Bump, num: T) -> Expr<'_> {
Expr::Num(arena.alloc(format!("{}", num)))
use std::fmt::Write;
let mut string = bumpalo::collections::String::with_capacity_in(64, arena);
write!(string, "{}", num).unwrap();
Expr::Num(string.into_bump_str())
}