@@ -126,6 +126,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
126
126
"slice_push_back" => slice_push_back ( interner, arguments, location) ,
127
127
"slice_push_front" => slice_push_front ( interner, arguments, location) ,
128
128
"slice_remove" => slice_remove ( interner, arguments, location, call_stack) ,
129
+ "str_as_bytes" => str_as_bytes ( interner, arguments, location) ,
129
130
"struct_def_as_type" => struct_def_as_type ( interner, arguments, location) ,
130
131
"struct_def_fields" => struct_def_fields ( interner, arguments, location) ,
131
132
"struct_def_generics" => struct_def_generics ( interner, arguments, location) ,
@@ -242,6 +243,32 @@ fn slice_push_back(
242
243
Ok ( Value :: Slice ( values, typ) )
243
244
}
244
245
246
+ fn str_as_bytes (
247
+ interner : & NodeInterner ,
248
+ arguments : Vec < ( Value , Location ) > ,
249
+ location : Location ,
250
+ ) -> IResult < Value > {
251
+ let ( string, string_location) = check_one_argument ( arguments, location) ?;
252
+
253
+ match string {
254
+ Value :: String ( string) => {
255
+ let string_as_bytes = string. as_bytes ( ) ;
256
+ let bytes_vector: Vec < Value > = string_as_bytes. iter ( ) . cloned ( ) . map ( Value :: U8 ) . collect ( ) ;
257
+ let byte_array_type = Type :: Array (
258
+ Box :: new ( Type :: Constant ( string_as_bytes. len ( ) as u32 ) ) ,
259
+ Box :: new ( Type :: Integer ( Signedness :: Unsigned , IntegerBitSize :: Eight ) ) ,
260
+ ) ;
261
+ Ok ( Value :: Array ( bytes_vector. into ( ) , byte_array_type) )
262
+ }
263
+ value => {
264
+ let type_var = Box :: new ( interner. next_type_variable ( ) ) ;
265
+ let expected = Type :: Array ( type_var. clone ( ) , type_var) ;
266
+ let actual = value. get_type ( ) . into_owned ( ) ;
267
+ Err ( InterpreterError :: TypeMismatch { expected, actual, location : string_location } )
268
+ }
269
+ }
270
+ }
271
+
245
272
/// fn as_type(self) -> Type
246
273
fn struct_def_as_type (
247
274
interner : & NodeInterner ,
0 commit comments