Skip to content

Commit e016b68

Browse files
committed
Document arg_check! macro a bit
1 parent fcea845 commit e016b68

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

vm/src/macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// count number of tokens given as arguments.
12
// see: https://danielkeep.github.io/tlborm/book/blk-counting.html
23
macro_rules! replace_expr {
34
($_t:tt $sub:expr) => {
@@ -44,9 +45,11 @@ macro_rules! arg_check {
4445
( $vm: ident, $args:ident, required=[$( ($arg_name:ident, $arg_type:expr) ),*], optional=[$( ($optional_arg_name:ident, $optional_arg_type:expr) ),*] ) => {
4546
let mut arg_count = 0;
4647

48+
// use macro magic to compile-time count number of required and optional arguments
4749
let minimum_arg_count = count_tts!($($arg_name)*);
4850
let maximum_arg_count = minimum_arg_count + count_tts!($($optional_arg_name)*);
4951

52+
// verify that the number of given arguments is right
5053
if $args.args.len() < minimum_arg_count || $args.args.len() > maximum_arg_count {
5154
let expected_str = if minimum_arg_count == maximum_arg_count {
5255
format!("{}", minimum_arg_count)
@@ -60,6 +63,9 @@ macro_rules! arg_check {
6063
)));
6164
};
6265

66+
// for each required parameter:
67+
// check if the type matches. If not, return with error
68+
// assign the arg to a variable
6369
$(
6470
type_check!($vm, $args, arg_count, $arg_name, $arg_type);
6571
let $arg_name = &$args.args[arg_count];
@@ -69,6 +75,9 @@ macro_rules! arg_check {
6975
}
7076
)*
7177

78+
// for each optional parameter, if there are enough positional arguments:
79+
// check if the type matches. If not, return with error
80+
// assign the arg to a variable
7281
$(
7382
let $optional_arg_name = if arg_count < $args.args.len() {
7483
type_check!($vm, $args, arg_count, $optional_arg_name, $optional_arg_type);

0 commit comments

Comments
 (0)