mirror of
https://github.com/roc-lang/roc.git
synced 2025-08-02 03:12:20 +00:00
str splits
This commit is contained in:
parent
a3d5e7bee5
commit
93fb1c73e6
3 changed files with 54 additions and 17 deletions
|
@ -1003,6 +1003,30 @@ impl<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_num_sub_checked(
|
||||||
|
&mut self,
|
||||||
|
dst: &Symbol,
|
||||||
|
src1: &Symbol,
|
||||||
|
src2: &Symbol,
|
||||||
|
num_layout: &InLayout<'a>,
|
||||||
|
return_layout: &InLayout<'a>,
|
||||||
|
) {
|
||||||
|
let function_name = match self.interner().get(*num_layout) {
|
||||||
|
Layout::Builtin(Builtin::Int(width)) => &bitcode::NUM_SUB_CHECKED_INT[width],
|
||||||
|
Layout::Builtin(Builtin::Float(width)) => &bitcode::NUM_SUB_CHECKED_FLOAT[width],
|
||||||
|
Layout::Builtin(Builtin::Decimal) => bitcode::DEC_SUB_WITH_OVERFLOW,
|
||||||
|
x => internal_error!("NumSubChecked is not defined for {:?}", x),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.build_fn_call(
|
||||||
|
dst,
|
||||||
|
function_name.to_string(),
|
||||||
|
&[*src1, *src2],
|
||||||
|
&[*num_layout, *num_layout],
|
||||||
|
return_layout,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
fn build_num_mul(&mut self, dst: &Symbol, src1: &Symbol, src2: &Symbol, layout: &InLayout<'a>) {
|
fn build_num_mul(&mut self, dst: &Symbol, src1: &Symbol, src2: &Symbol, layout: &InLayout<'a>) {
|
||||||
use Builtin::Int;
|
use Builtin::Int;
|
||||||
|
|
||||||
|
|
|
@ -447,6 +447,9 @@ trait Backend<'a> {
|
||||||
LowLevel::NumAddChecked => {
|
LowLevel::NumAddChecked => {
|
||||||
self.build_num_add_checked(sym, &args[0], &args[1], &arg_layouts[0], ret_layout)
|
self.build_num_add_checked(sym, &args[0], &args[1], &arg_layouts[0], ret_layout)
|
||||||
}
|
}
|
||||||
|
LowLevel::NumSubChecked => {
|
||||||
|
self.build_num_sub_checked(sym, &args[0], &args[1], &arg_layouts[0], ret_layout)
|
||||||
|
}
|
||||||
LowLevel::NumAcos => self.build_fn_call(
|
LowLevel::NumAcos => self.build_fn_call(
|
||||||
sym,
|
sym,
|
||||||
bitcode::NUM_ACOS[FloatWidth::F64].to_string(),
|
bitcode::NUM_ACOS[FloatWidth::F64].to_string(),
|
||||||
|
@ -868,6 +871,13 @@ trait Backend<'a> {
|
||||||
arg_layouts,
|
arg_layouts,
|
||||||
ret_layout,
|
ret_layout,
|
||||||
),
|
),
|
||||||
|
LowLevel::StrSubstringUnsafe => self.build_fn_call(
|
||||||
|
sym,
|
||||||
|
bitcode::STR_SUBSTRING_UNSAFE.to_string(),
|
||||||
|
args,
|
||||||
|
arg_layouts,
|
||||||
|
ret_layout,
|
||||||
|
),
|
||||||
LowLevel::StrToUtf8 => self.build_fn_call(
|
LowLevel::StrToUtf8 => self.build_fn_call(
|
||||||
sym,
|
sym,
|
||||||
bitcode::STR_TO_UTF8.to_string(),
|
bitcode::STR_TO_UTF8.to_string(),
|
||||||
|
@ -1043,13 +1053,6 @@ trait Backend<'a> {
|
||||||
self.load_literal_symbols(args);
|
self.load_literal_symbols(args);
|
||||||
self.build_fn_call(sym, fn_name, args, arg_layouts, ret_layout)
|
self.build_fn_call(sym, fn_name, args, arg_layouts, ret_layout)
|
||||||
}
|
}
|
||||||
Symbol::NUM_ADD_CHECKED => {
|
|
||||||
let layout_id = LayoutIds::default().get(func_sym, ret_layout);
|
|
||||||
let fn_name = self.symbol_to_string(func_sym, layout_id);
|
|
||||||
// Now that the arguments are needed, load them if they are literals.
|
|
||||||
self.load_literal_symbols(args);
|
|
||||||
self.build_fn_call(sym, fn_name, args, arg_layouts, ret_layout)
|
|
||||||
}
|
|
||||||
Symbol::BOOL_TRUE => {
|
Symbol::BOOL_TRUE => {
|
||||||
let bool_layout = Layout::BOOL;
|
let bool_layout = Layout::BOOL;
|
||||||
self.load_literal(&Symbol::DEV_TMP, &bool_layout, &Literal::Bool(true));
|
self.load_literal(&Symbol::DEV_TMP, &bool_layout, &Literal::Bool(true));
|
||||||
|
@ -1110,6 +1113,16 @@ trait Backend<'a> {
|
||||||
return_layout: &InLayout<'a>,
|
return_layout: &InLayout<'a>,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/// build_num_sub_checked stores the sum of src1 and src2 into dst.
|
||||||
|
fn build_num_sub_checked(
|
||||||
|
&mut self,
|
||||||
|
dst: &Symbol,
|
||||||
|
src1: &Symbol,
|
||||||
|
src2: &Symbol,
|
||||||
|
num_layout: &InLayout<'a>,
|
||||||
|
return_layout: &InLayout<'a>,
|
||||||
|
);
|
||||||
|
|
||||||
/// build_num_mul stores `src1 * src2` into dst.
|
/// build_num_mul stores `src1 * src2` into dst.
|
||||||
fn build_num_mul(&mut self, dst: &Symbol, src1: &Symbol, src2: &Symbol, layout: &InLayout<'a>);
|
fn build_num_mul(&mut self, dst: &Symbol, src1: &Symbol, src2: &Symbol, layout: &InLayout<'a>);
|
||||||
|
|
||||||
|
|
|
@ -1697,7 +1697,7 @@ fn to_scalar_4_byte() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_split_first_one_char() {
|
fn str_split_first_one_char() {
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
@ -1713,7 +1713,7 @@ fn str_split_first_one_char() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_split_first_multiple_chars() {
|
fn str_split_first_multiple_chars() {
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
@ -1727,7 +1727,7 @@ fn str_split_first_multiple_chars() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_split_first_entire_input() {
|
fn str_split_first_entire_input() {
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
@ -1741,7 +1741,7 @@ fn str_split_first_entire_input() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_split_first_not_found() {
|
fn str_split_first_not_found() {
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
@ -1755,7 +1755,7 @@ fn str_split_first_not_found() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_split_last_one_char() {
|
fn str_split_last_one_char() {
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
@ -1769,7 +1769,7 @@ fn str_split_last_one_char() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_split_last_multiple_chars() {
|
fn str_split_last_multiple_chars() {
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
@ -1783,7 +1783,7 @@ fn str_split_last_multiple_chars() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_split_last_entire_input() {
|
fn str_split_last_entire_input() {
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
indoc!(
|
indoc!(
|
||||||
|
@ -1797,12 +1797,12 @@ fn str_split_last_entire_input() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_split_last_not_found() {
|
fn str_split_last_not_found() {
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
indoc!(
|
indoc!(
|
||||||
r#"
|
r#"
|
||||||
Str.splitFirst "foo" "bar"
|
Str.splitLast "foo" "bar"
|
||||||
"#
|
"#
|
||||||
),
|
),
|
||||||
RocResult::err(()),
|
RocResult::err(()),
|
||||||
|
@ -1831,7 +1831,7 @@ fn str_split_overlapping_substring_2() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(any(feature = "gen-llvm"))]
|
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
|
||||||
fn str_walk_utf8_with_index() {
|
fn str_walk_utf8_with_index() {
|
||||||
#[cfg(not(feature = "gen-llvm-wasm"))]
|
#[cfg(not(feature = "gen-llvm-wasm"))]
|
||||||
assert_evals_to!(
|
assert_evals_to!(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue