Skip to content

Commit

Permalink
Merge 380b3bd into fc5bb02
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Sep 12, 2024
2 parents fc5bb02 + 380b3bd commit 009497b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
13 changes: 13 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl<'local, 'context> Interpreter<'local, 'context> {
"expr_is_continue" => expr_is_continue(interner, arguments, location),
"expr_resolve" => expr_resolve(self, arguments, location),
"is_unconstrained" => Ok(Value::Bool(true)),
"fmtstr_quoted" => fmtstr_quoted(interner, arguments, location),
"fmtstr_quoted_contents" => fmtstr_quoted_contents(interner, arguments, location),
"fresh_type_variable" => fresh_type_variable(interner),
"function_def_add_attribute" => function_def_add_attribute(self, arguments, location),
Expand Down Expand Up @@ -1897,6 +1898,18 @@ fn unwrap_expr_value(interner: &NodeInterner, mut expr_value: ExprValue) -> Expr
expr_value
}

// fn quoted(self) -> Quoted
fn fmtstr_quoted(
interner: &NodeInterner,
arguments: Vec<(Value, Location)>,
location: Location,
) -> IResult<Value> {
let self_argument = check_one_argument(arguments, location)?;
let (string, _) = get_format_string(interner, self_argument)?;
let token = Token::Str((*string).clone());
Ok(Value::Quoted(Rc::new(vec![token])))
}

// fn quoted_contents(self) -> Quoted
fn fmtstr_quoted_contents(
interner: &NodeInterner,
Expand Down
6 changes: 3 additions & 3 deletions compiler/noirc_frontend/src/lexer/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ impl fmt::Display for Token {
Token::Ident(ref s) => write!(f, "{s}"),
Token::Int(n) => write!(f, "{}", n.to_u128()),
Token::Bool(b) => write!(f, "{b}"),
Token::Str(ref b) => write!(f, "{b}"),
Token::FmtStr(ref b) => write!(f, "f{b}"),
Token::Str(ref b) => write!(f, "{b:?}"),
Token::FmtStr(ref b) => write!(f, "f{b:?}"),
Token::RawStr(ref b, hashes) => {
let h: String = std::iter::once('#').cycle().take(hashes as usize).collect();
write!(f, "r{h}\"{b}\"{h}")
write!(f, "r{h}{b:?}{h}")
}
Token::Keyword(k) => write!(f, "{k}"),
Token::Attribute(ref a) => write!(f, "{a}"),
Expand Down
6 changes: 6 additions & 0 deletions docs/docs/noir/standard_library/fmtstr.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ title: fmtstr

## Methods

### quoted

#include_code quoted noir_stdlib/src/meta/format_string.nr rust

Returns the string literal as a `Quoted` value (this includes the leading and trailing double quotes).

### quoted_contents

#include_code quoted_contents noir_stdlib/src/meta/format_string.nr rust
Expand Down
5 changes: 5 additions & 0 deletions noir_stdlib/src/meta/format_string.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
impl <let N: u32, T> fmtstr<N, T> {
#[builtin(fmtstr_quoted)]
// docs:start:quoted
comptime fn quoted(self) -> Quoted {}
// docs:end:quoted

#[builtin(fmtstr_quoted_contents)]
// docs:start:quoted_contents
comptime fn quoted_contents(self) -> Quoted {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ fn main() {

// Mainly test fmtstr::quoted_contents
call!(glue(quote { hello }, quote { world }));

comptime
{
let x = "world\n\t\"";
let s = f"Hello {x}".quoted();
let code = quote { check_hello_world($s) };
std::meta::unquote!(code);
}
}

comptime fn glue(x: Quoted, y: Quoted) -> Quoted {
Expand All @@ -26,4 +34,7 @@ fn hello_world() {}
comptime fn call(x: Quoted) -> Quoted {
quote { $x() }
}
fn check_hello_world<let N: u32>(s: str<N>) {
assert_eq(s.as_bytes().len(), "Hello world\n\t\"".as_bytes().len());
}

0 comments on commit 009497b

Please sign in to comment.