1
+ // count number of tokens given as arguments.
1
2
// see: https://danielkeep.github.io/tlborm/book/blk-counting.html
2
3
macro_rules! replace_expr {
3
4
( $_t: tt $sub: expr) => {
@@ -44,9 +45,11 @@ macro_rules! arg_check {
44
45
( $vm: ident, $args: ident, required=[ $( ( $arg_name: ident, $arg_type: expr) ) ,* ] , optional=[ $( ( $optional_arg_name: ident, $optional_arg_type: expr) ) ,* ] ) => {
45
46
let mut arg_count = 0 ;
46
47
48
+ // use macro magic to compile-time count number of required and optional arguments
47
49
let minimum_arg_count = count_tts!( $( $arg_name) * ) ;
48
50
let maximum_arg_count = minimum_arg_count + count_tts!( $( $optional_arg_name) * ) ;
49
51
52
+ // verify that the number of given arguments is right
50
53
if $args. args. len( ) < minimum_arg_count || $args. args. len( ) > maximum_arg_count {
51
54
let expected_str = if minimum_arg_count == maximum_arg_count {
52
55
format!( "{}" , minimum_arg_count)
@@ -60,6 +63,9 @@ macro_rules! arg_check {
60
63
) ) ) ;
61
64
} ;
62
65
66
+ // for each required parameter:
67
+ // check if the type matches. If not, return with error
68
+ // assign the arg to a variable
63
69
$(
64
70
type_check!( $vm, $args, arg_count, $arg_name, $arg_type) ;
65
71
let $arg_name = & $args. args[ arg_count] ;
@@ -69,6 +75,9 @@ macro_rules! arg_check {
69
75
}
70
76
) *
71
77
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
72
81
$(
73
82
let $optional_arg_name = if arg_count < $args. args. len( ) {
74
83
type_check!( $vm, $args, arg_count, $optional_arg_name, $optional_arg_type) ;
0 commit comments