diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 65c9c3f018d..5db7999a6f2 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -123,6 +123,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "quoted_as_trait_constraint" => quoted_as_trait_constraint(self, arguments, location), "quoted_as_type" => quoted_as_type(self, arguments, location), "quoted_eq" => quoted_eq(arguments, location), + "quoted_tokens" => quoted_tokens(arguments, location), "slice_insert" => slice_insert(interner, arguments, location), "slice_pop_back" => slice_pop_back(interner, arguments, location, call_stack), "slice_pop_front" => slice_pop_front(interner, arguments, location, call_stack), @@ -535,6 +536,17 @@ fn quoted_as_type( Ok(Value::Type(typ)) } +// fn tokens(quoted: Quoted) -> [Quoted] +fn quoted_tokens(arguments: Vec<(Value, Location)>, location: Location) -> IResult { + let argument = check_one_argument(arguments, location)?; + let value = get_quoted(argument)?; + + Ok(Value::Slice( + value.iter().map(|token| Value::Quoted(Rc::new(vec![token.clone()]))).collect(), + Type::Slice(Box::new(Type::Quoted(QuotedType::Quoted))), + )) +} + fn to_le_radix( arguments: Vec<(Value, Location)>, return_type: Type, diff --git a/docs/docs/noir/standard_library/meta/quoted.md b/docs/docs/noir/standard_library/meta/quoted.md index bf79f2e5d9f..ac075d96648 100644 --- a/docs/docs/noir/standard_library/meta/quoted.md +++ b/docs/docs/noir/standard_library/meta/quoted.md @@ -50,6 +50,12 @@ stream doesn't parse to a type or if the type isn't a valid type in scope. #include_code implements_example test_programs/compile_success_empty/comptime_type/src/main.nr rust +### tokens + +#include_code tokens noir_stdlib/src/meta/quoted.nr rust + +Returns a slice of the individual tokens that form this token stream. + ## Trait Implementations ```rust diff --git a/noir_stdlib/src/meta/quoted.nr b/noir_stdlib/src/meta/quoted.nr index 9fd1e9026ba..3fab43359c1 100644 --- a/noir_stdlib/src/meta/quoted.nr +++ b/noir_stdlib/src/meta/quoted.nr @@ -3,24 +3,29 @@ use crate::option::Option; impl Quoted { #[builtin(quoted_as_expr)] -// docs:start:as_expr + // docs:start:as_expr fn as_expr(self) -> Option {} // docs:end:as_expr #[builtin(quoted_as_module)] -// docs:start:as_module + // docs:start:as_module fn as_module(self) -> Option {} // docs:end:as_module #[builtin(quoted_as_trait_constraint)] -// docs:start:as_trait_constraint + // docs:start:as_trait_constraint fn as_trait_constraint(self) -> TraitConstraint {} // docs:end:as_trait_constraint #[builtin(quoted_as_type)] -// docs:start:as_type + // docs:start:as_type fn as_type(self) -> Type {} // docs:end:as_type + + #[builtin(quoted_tokens)] + // docs:start:tokens + fn tokens(self) -> [Quoted] {} + // docs:end:tokens } impl Eq for Quoted {