@@ -537,28 +537,43 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
537
537
.eq, .ne {
538
538
// TODO Implement `$if method.args.len == 1`
539
539
if cond.left is ast.SelectorExpr
540
- && (g.comptime_for_field_var.len > 0 || g.comptime_for_method.len > 0 )
541
- && cond.right is ast.StringLiteral {
542
- selector := cond.left as ast.SelectorExpr
543
- if selector.expr is ast.Ident && selector.field_name == 'name' {
544
- if g.comptime_for_method_var.len > 0
545
- && (selector.expr as ast.Ident ).name == g.comptime_for_method_var {
546
- is_equal := g.comptime_for_method == cond.right.val
547
- if is_equal {
548
- g.write ('1' )
549
- } else {
550
- g.write ('0' )
540
+ && (g.comptime_for_field_var.len > 0 || g.comptime_for_method.len > 0 ) {
541
+ if cond.right is ast.StringLiteral {
542
+ selector := cond.left as ast.SelectorExpr
543
+ if selector.expr is ast.Ident && selector.field_name == 'name' {
544
+ if g.comptime_for_method_var.len > 0
545
+ && (selector.expr as ast.Ident ).name == g.comptime_for_method_var {
546
+ is_equal := g.comptime_for_method == cond.right.val
547
+ if is_equal {
548
+ g.write ('1' )
549
+ } else {
550
+ g.write ('0' )
551
+ }
552
+ return is_equal, true
553
+ } else if g.comptime_for_field_var.len > 0
554
+ && (selector.expr as ast.Ident ).name == g.comptime_for_field_var {
555
+ is_equal := g.comptime_for_field_value.name == cond.right.val
556
+ if is_equal {
557
+ g.write ('1' )
558
+ } else {
559
+ g.write ('0' )
560
+ }
561
+ return is_equal, true
562
+ }
563
+ }
564
+ } else if cond.right is ast.IntegerLiteral {
565
+ if g.is_comptime_selector_field_name (cond.left, 'indirections' ) {
566
+ is_true := match cond.op {
567
+ .eq { g.comptime_for_field_type.nr_muls () == cond.right.val.i64 () }
568
+ .ne { g.comptime_for_field_type.nr_muls () != cond.right.val.i64 () }
569
+ else { false }
551
570
}
552
- return is_equal, true
553
- } else if g.comptime_for_field_var.len > 0
554
- && (selector.expr as ast.Ident ).name == g.comptime_for_field_var {
555
- is_equal := g.comptime_for_field_value.name == cond.right.val
556
- if is_equal {
571
+ if is_true {
557
572
g.write ('1' )
558
573
} else {
559
574
g.write ('0' )
560
575
}
561
- return is_equal , true
576
+ return is_true , true
562
577
}
563
578
}
564
579
}
@@ -611,6 +626,30 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
611
626
return true , true
612
627
}
613
628
}
629
+ .gt, .lt, .ge, .le {
630
+ if cond.left is ast.SelectorExpr && cond.right is ast.IntegerLiteral {
631
+ if g.is_comptime_selector_field_name (cond.left as ast.SelectorExpr ,
632
+ 'indirections' )
633
+ {
634
+ is_true := match cond.op {
635
+ .gt { g.comptime_for_field_type.nr_muls () > cond.right.val.i64 () }
636
+ .lt { g.comptime_for_field_type.nr_muls () < cond.right.val.i64 () }
637
+ .ge { g.comptime_for_field_type.nr_muls () > = cond.right.val.i64 () }
638
+ .le { g.comptime_for_field_type.nr_muls () < = cond.right.val.i64 () }
639
+ else { false }
640
+ }
641
+ if is_true {
642
+ g.write ('1' )
643
+ } else {
644
+ g.write ('0' )
645
+ }
646
+ return is_true, true
647
+ } else {
648
+ return true , false
649
+ }
650
+ }
651
+ return true , false
652
+ }
614
653
else {
615
654
return true , false
616
655
}
@@ -679,6 +718,13 @@ fn (mut g Gen) pop_existing_comptime_values() {
679
718
g.comptime_var_type_map = old.comptime_var_type_map.clone ()
680
719
}
681
720
721
+ // is_comptime_selector_field_name checks if the SelectorExpr is related to $for variable accessing specific field name provided by `field_name`
722
+ [inline ]
723
+ fn (mut g Gen) is_comptime_selector_field_name (node ast.SelectorExpr, field_name string ) bool {
724
+ return g.inside_comptime_for_field && node.expr is ast.Ident
725
+ && (node.expr as ast.Ident ).name == g.comptime_for_field_var && node.field_name == field_name
726
+ }
727
+
682
728
// check_comptime_is_field_selector checks if the SelectorExpr is related to $for variable accessing .typ field
683
729
[inline ]
684
730
fn (mut g Gen) is_comptime_selector_type (node ast.SelectorExpr) bool {
0 commit comments