Skip to content

Commit

Permalink
Add array! macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbens-starkware committed Jun 28, 2023
1 parent de9b26d commit 4f76824
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions crates/cairo-lang-plugins/src/plugins/inline_macro_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use cairo_lang_syntax::node::db::SyntaxGroup;
use cairo_lang_syntax::node::kind::SyntaxKind;
use cairo_lang_syntax::node::{SyntaxNode, TypedSyntaxNode};

use super::inline_macros::array::ArrayMacro;
use super::inline_macros::consteval_int::ConstevalIntMacro;

/// The result of expanding an inline macro.
Expand Down Expand Up @@ -39,6 +40,7 @@ pub trait InlineMacro {
/// Returns the inline macro plugin for the given macro name, or None if no such plugin exists.
fn get_inline_macro_plugin(macro_name: &str) -> Option<Box<dyn InlineMacro>> {
match macro_name {
"array" => Some(Box::new(ArrayMacro)),
"consteval_int" => Some(Box::new(ConstevalIntMacro)),
_ => None,
}
Expand Down
41 changes: 41 additions & 0 deletions crates/cairo-lang-plugins/src/plugins/inline_macros/array.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use cairo_lang_syntax::node::TypedSyntaxNode;

use crate::plugins::InlineMacro;

pub struct ArrayMacro;
impl InlineMacro for ArrayMacro {
fn append_macro_code(
&self,
macro_expander_data: &mut crate::plugins::InlineMacroExpanderData,
db: &dyn cairo_lang_syntax::node::db::SyntaxGroup,
macro_arguments: &cairo_lang_syntax::node::ast::ExprList,
) {
let args = macro_arguments.elements(db);
let mut expanded_code = "{
let mut __array_builder_macro_result__ = ArrayTrait::new();"
.to_string();
for arg in args {
expanded_code.push_str(&format!(
"\n __array_builder_macro_result__.append({});",
arg.as_syntax_node().get_text(db)
));
}
expanded_code.push_str(
"\n __array_builder_macro_result__
}",
);
macro_expander_data.result_code.push_str(&expanded_code);
macro_expander_data.code_changed = true;
}

fn is_bracket_type_allowed(
&self,
db: &dyn cairo_lang_syntax::node::db::SyntaxGroup,
macro_ast: &cairo_lang_syntax::node::ast::ExprInlineMacro,
) -> bool {
matches!(
macro_ast.arguments(db),
cairo_lang_syntax::node::ast::WrappedExprList::BracketedExprList(_)
)
}
}
1 change: 1 addition & 0 deletions crates/cairo-lang-plugins/src/plugins/inline_macros/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod array;
pub mod consteval_int;
47 changes: 47 additions & 0 deletions crates/cairo-lang-plugins/src/test_data/inline_macros
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,50 @@ error: Unknown inline macro: foo
--> dummy_file.cairo:2:13
let x = foo!(0);
^*****^

//! > ==========================================================================

//! > Test array macro

//! > test_runner_name
test_expand_plugin

//! > cairo_code
fn foo() {
let arr = array![1,2,3];
}

//! > generated_cairo_code
fn foo() {
let arr = {
let mut __array_builder_macro_result__ = ArrayTrait::new();
__array_builder_macro_result__.append(1);
__array_builder_macro_result__.append(2);
__array_builder_macro_result__.append(3);
__array_builder_macro_result__
};
}

//! > expected_diagnostics

//! > ==========================================================================

//! > Test array macro empty

//! > test_runner_name
test_expand_plugin

//! > cairo_code
fn foo() {
let arr = array![];
}

//! > generated_cairo_code
fn foo() {
let arr = {
let mut __array_builder_macro_result__ = ArrayTrait::new();
__array_builder_macro_result__
};
}

//! > expected_diagnostics

0 comments on commit 4f76824

Please sign in to comment.