Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Quoted::tokens #5942

Merged
merged 7 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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<Value> {
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,
Expand Down
6 changes: 6 additions & 0 deletions docs/docs/noir/standard_library/meta/quoted.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions noir_stdlib/src/meta/quoted.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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<Expr> {}
// docs:end:as_expr

#[builtin(quoted_as_module)]
// docs:start:as_module
// docs:start:as_module
fn as_module(self) -> Option<Module> {}
// 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 {
Expand Down
Loading