mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
Interpolate strings by desugaring to Str.concat
We could definitely make this more efficent by allocating enough space for the final string and then copying the contents of each of the pieces into it one by one. We don't do that yet though!
This commit is contained in:
parent
2e15443c8c
commit
37a254cef3
5 changed files with 60 additions and 96 deletions
|
@ -586,12 +586,6 @@ pub enum Stmt<'a> {
|
|||
RuntimeError(&'a str),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum StrSegment<'a> {
|
||||
Interpolation(Expr<'a>),
|
||||
Plaintext(&'a str),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Literal<'a> {
|
||||
// Literals
|
||||
|
@ -1249,44 +1243,12 @@ pub fn with_hole<'a>(
|
|||
hole,
|
||||
),
|
||||
|
||||
Str(segments) => {
|
||||
use roc_can::expr::StrSegment::*;
|
||||
|
||||
let iter = &mut segments.into_iter().rev();
|
||||
let /* mut */ stmt = match iter.next() {
|
||||
Some(Plaintext(string)) => Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Str(arena.alloc(string))),
|
||||
Layout::Builtin(Builtin::Str),
|
||||
hole,
|
||||
),
|
||||
Some(Interpolation(loc_expr)) => {
|
||||
with_hole(env, loc_expr.value, procs, layout_cache, assigned, hole)
|
||||
}
|
||||
None => {
|
||||
// No segments? Empty string!
|
||||
return Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Str("")),
|
||||
Layout::Builtin(Builtin::Str),
|
||||
hole,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
while let Some(seg) = iter.next() {
|
||||
match seg {
|
||||
Plaintext(string) => {
|
||||
todo!("Str.concat plaintext str with previous: {:?}", string);
|
||||
}
|
||||
Interpolation(loc_expr) => {
|
||||
todo!("Str.concat interplation with previous: {:?}", loc_expr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
stmt
|
||||
}
|
||||
Str(string) => Stmt::Let(
|
||||
assigned,
|
||||
Expr::Literal(Literal::Str(arena.alloc(string))),
|
||||
Layout::Builtin(Builtin::Str),
|
||||
hole,
|
||||
),
|
||||
|
||||
Num(var, num) => match num_argument_to_int_or_float(env.subs, var) {
|
||||
IntOrFloat::IntType => Stmt::Let(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue