Add Str.repeat builtin

This commit is contained in:
Kofi Gumbs 2021-09-29 17:32:42 -04:00
parent 0b02929a53
commit b32a42f05a
11 changed files with 109 additions and 14 deletions

View file

@ -15,7 +15,7 @@ use crate::llvm::build_list::{
};
use crate::llvm::build_str::{
empty_str, str_concat, str_count_graphemes, str_ends_with, str_from_float, str_from_int,
str_from_utf8, str_from_utf8_range, str_join_with, str_number_of_bytes, str_split,
str_from_utf8, str_from_utf8_range, str_join_with, str_number_of_bytes, str_repeat, str_split,
str_starts_with, str_starts_with_code_point, str_to_utf8,
};
use crate::llvm::compare::{generic_eq, generic_neq};
@ -4853,6 +4853,12 @@ fn run_low_level<'a, 'ctx, 'env>(
str_to_utf8(env, string.into_struct_value())
}
StrRepeat => {
// Str.repeat : Str, Nat -> Str
debug_assert_eq!(args.len(), 2);
str_repeat(env, scope, args[0], args[1])
}
StrSplit => {
// Str.split : Str, Str -> List Str
debug_assert_eq!(args.len(), 2);

View file

@ -12,6 +12,18 @@ use super::build::load_symbol;
pub static CHAR_LAYOUT: Layout = Layout::Builtin(Builtin::Int8);
/// Str.repeat : Str, Nat -> Str
pub fn str_repeat<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,
scope: &Scope<'a, 'ctx>,
str_symbol: Symbol,
count_symbol: Symbol,
) -> BasicValueEnum<'ctx> {
let str_c_abi = str_symbol_to_c_abi(env, scope, str_symbol);
let count = load_symbol(scope, &count_symbol);
call_bitcode_fn(env, &[str_c_abi.into(), count.into()], bitcode::STR_REPEAT)
}
/// Str.split : Str, Str -> List Str
pub fn str_split<'a, 'ctx, 'env>(
env: &Env<'a, 'ctx, 'env>,