From b1a67cd43862acfa3db3e8259e91c996ba20c6a9 Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 5 Sep 2024 13:22:50 +0000 Subject: [PATCH 1/6] added fmtstr.quoted --- .../src/hir/comptime/interpreter/builtin.rs | 23 +++++++++++++++++++ docs/docs/noir/standard_library/fmtstr.md | 8 ++++++- noir_stdlib/src/meta/format_string.nr | 5 ++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index d2c9e4ffc0c..8405294bd80 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -97,6 +97,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "expr_resolve" => expr_resolve(self, arguments, location), "is_unconstrained" => Ok(Value::Bool(true)), "fmtstr_quoted_contents" => fmtstr_quoted_contents(interner, arguments, location), + "fmtstr_quoted" => fmtstr_quoted(interner, arguments, location), "function_def_body" => function_def_body(interner, arguments, location), "function_def_has_named_attribute" => { function_def_has_named_attribute(interner, arguments, location) @@ -1595,6 +1596,28 @@ fn fmtstr_quoted_contents( Ok(Value::Quoted(Rc::new(tokens))) } +// fn quoted(self) -> Quoted +fn fmtstr_quoted( + interner: &NodeInterner, + arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult { + let self_argument = check_one_argument(arguments, location)?; + let (string, _) = get_format_string(interner, self_argument)?; + let (tokens, _) = Lexer::lex(&string); + let str = tokens + .0 + .into_iter() + .filter(|token| !matches!(token.clone().into_token(), Token::EOF)) + .map(|token| token.into_token().display(interner).to_string()) + .collect::>() + .join(""); + + let token = Token::Str(str); + + Ok(Value::Quoted(Rc::new(vec![token]))) +} + // fn body(self) -> Expr fn function_def_body( interner: &NodeInterner, diff --git a/docs/docs/noir/standard_library/fmtstr.md b/docs/docs/noir/standard_library/fmtstr.md index 293793e23ff..e491862abe7 100644 --- a/docs/docs/noir/standard_library/fmtstr.md +++ b/docs/docs/noir/standard_library/fmtstr.md @@ -10,4 +10,10 @@ title: fmtstr #include_code quoted_contents noir_stdlib/src/meta/format_string.nr rust -Returns the format string contents (that is, without the leading and trailing double quotes) as a `Quoted` value. \ No newline at end of file +Returns the format string contents (that is, without the leading and trailing double quotes) as a `Quoted` value. + +### quoted + +#include_code quoted noir_stdlib/src/meta/format_string.nr rust + +Returns the format string (including the leading and trailing double quotes) as a `Quoted` value. \ No newline at end of file diff --git a/noir_stdlib/src/meta/format_string.nr b/noir_stdlib/src/meta/format_string.nr index 44b69719efe..f57efc24837 100644 --- a/noir_stdlib/src/meta/format_string.nr +++ b/noir_stdlib/src/meta/format_string.nr @@ -3,4 +3,9 @@ impl fmtstr { // docs:start:quoted_contents fn quoted_contents(self) -> Quoted {} // docs:end:quoted_contents + + #[builtin(fmtstr_quoted)] + // docs:start:quoted + fn quoted(self) -> Quoted {} + // docs:end:quoted } From 751ff9b52d31a3b524f74b4e0bfe6d16c9d8a0ad Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 5 Sep 2024 16:07:45 +0000 Subject: [PATCH 2/6] changed approach --- .../src/hir/comptime/interpreter/builtin.rs | 12 ++++++++++++ docs/docs/noir/standard_library/fmtstr.md | 8 +------- noir_stdlib/src/meta/format_string.nr | 5 ----- noir_stdlib/src/meta/quoted.nr | 13 +++++++++---- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 20136207aeb..8ac7c1549bd 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -124,6 +124,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), @@ -536,6 +537,17 @@ fn quoted_as_type( Ok(Value::Type(typ)) } +// fn as_expr(quoted: Quoted) -> Option +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/fmtstr.md b/docs/docs/noir/standard_library/fmtstr.md index e491862abe7..293793e23ff 100644 --- a/docs/docs/noir/standard_library/fmtstr.md +++ b/docs/docs/noir/standard_library/fmtstr.md @@ -10,10 +10,4 @@ title: fmtstr #include_code quoted_contents noir_stdlib/src/meta/format_string.nr rust -Returns the format string contents (that is, without the leading and trailing double quotes) as a `Quoted` value. - -### quoted - -#include_code quoted noir_stdlib/src/meta/format_string.nr rust - -Returns the format string (including the leading and trailing double quotes) as a `Quoted` value. \ No newline at end of file +Returns the format string contents (that is, without the leading and trailing double quotes) as a `Quoted` value. \ No newline at end of file diff --git a/noir_stdlib/src/meta/format_string.nr b/noir_stdlib/src/meta/format_string.nr index f57efc24837..44b69719efe 100644 --- a/noir_stdlib/src/meta/format_string.nr +++ b/noir_stdlib/src/meta/format_string.nr @@ -3,9 +3,4 @@ impl fmtstr { // docs:start:quoted_contents fn quoted_contents(self) -> Quoted {} // docs:end:quoted_contents - - #[builtin(fmtstr_quoted)] - // docs:start:quoted - fn quoted(self) -> Quoted {} - // docs:end:quoted } 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 { From ade7c01fe40f72fddf87ff45f832d61b8c029dea Mon Sep 17 00:00:00 2001 From: thunkar Date: Thu, 5 Sep 2024 16:15:42 +0000 Subject: [PATCH 3/6] docs --- docs/docs/noir/standard_library/meta/quoted.md | 6 ++++++ 1 file changed, 6 insertions(+) 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 From 1fab076446b913b11ac6b00de6dbdbeea187b345 Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 5 Sep 2024 11:24:45 -0500 Subject: [PATCH 4/6] Update compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs --- compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 8ac7c1549bd..8062c4d2712 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -97,7 +97,6 @@ impl<'local, 'context> Interpreter<'local, 'context> { "expr_resolve" => expr_resolve(self, arguments, location), "is_unconstrained" => Ok(Value::Bool(true)), "fmtstr_quoted_contents" => fmtstr_quoted_contents(interner, arguments, location), - "fmtstr_quoted" => fmtstr_quoted(interner, arguments, location), "function_def_body" => function_def_body(interner, arguments, location), "function_def_has_named_attribute" => { function_def_has_named_attribute(interner, arguments, location) From 0b73e07b0eab0a96378321177d7a9035bbf9257a Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 5 Sep 2024 11:24:51 -0500 Subject: [PATCH 5/6] Update compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs --- .../src/hir/comptime/interpreter/builtin.rs | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 8062c4d2712..cd68dcf81a3 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -1666,28 +1666,6 @@ fn fmtstr_quoted_contents( Ok(Value::Quoted(Rc::new(tokens))) } -// fn quoted(self) -> Quoted -fn fmtstr_quoted( - interner: &NodeInterner, - arguments: Vec<(Value, Location)>, - location: Location, -) -> IResult { - let self_argument = check_one_argument(arguments, location)?; - let (string, _) = get_format_string(interner, self_argument)?; - let (tokens, _) = Lexer::lex(&string); - let str = tokens - .0 - .into_iter() - .filter(|token| !matches!(token.clone().into_token(), Token::EOF)) - .map(|token| token.into_token().display(interner).to_string()) - .collect::>() - .join(""); - - let token = Token::Str(str); - - Ok(Value::Quoted(Rc::new(vec![token]))) -} - // fn body(self) -> Expr fn function_def_body( interner: &NodeInterner, From 08b7c344ee474f4aeefe3d2eae01087d170c45dc Mon Sep 17 00:00:00 2001 From: jfecher Date: Thu, 5 Sep 2024 11:25:55 -0500 Subject: [PATCH 6/6] Update compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs --- compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index cd68dcf81a3..5db7999a6f2 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -536,7 +536,7 @@ fn quoted_as_type( Ok(Value::Type(typ)) } -// fn as_expr(quoted: Quoted) -> Option +// 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)?;