#Weird bug in fmt_union

1 messages · Page 1 of 1 (latest)

rain beacon
#

C:/Odin/core/fmt/fmt.odin(2521:23) Index 860118579007 is out of range 0..<8

The union it tries to print:

Regex :: union{
    Rstring,
    Choice,
    Optional,
    Seq,
    Star,
    Plus,
    Wild,
    Power,
}

Rstring :: string
Choice  :: []^Regex
Optional:: distinct ^Regex
Seq     :: struct{lhs:^Regex,rhs:^Regex}
Star    :: distinct ^Regex
Plus    :: distinct ^Regex
Wild    :: struct{}
Power   :: struct{ex:^Regex,n:int}
#

full code:

package main

import "core:fmt"
import str"core:strings"

main :: proc(){

    // (a|b)(c|d)E+G?
    a:Regex = Rstring("a")
    b:Regex = Rstring("b")
    ab:Regex = Choice{&a,&b}

    c:Regex = Rstring("c")
    d:Regex = Rstring("d")
    cd:Regex = Choice{&c,&d}
    
    abcd:Regex = Seq{&ab,&cd}

    E:Regex = Rstring("E")
    eplus:Regex = Plus(&E)

    abcde:Regex = Seq{&abcd,&eplus}
    
    G:Regex = Rstring("G")
    gopt:Regex = Optional(&G)

    expr1:Regex = Seq{&abcde,&gopt}


    print_r(&ab)

}

print_r :: proc(_regx:^Regex){

    switch regx in _regx{
    case Rstring: 
        fmt.print(regx)
    case Choice : 
        fmt.print("Choice from:",regx)
        fmt.print("(")
        for choice in regx{
            print_r(choice)
        }
        fmt.print(")")
    case Optional:
        print_r(auto_cast(regx))
        fmt.print("?")
    case Seq:
        print_r(regx.lhs)
        print_r(regx.rhs)
    case Star:
        rstr,ok := regx^.(Rstring)
        if ok {fmt.print(rstr,"?"); return}

        fmt.print("(")
        print_r(auto_cast(regx))
        fmt.print(")")
        fmt.print("?")
    case Plus:
        rstr,ok := regx^.(Rstring)
        if ok {fmt.print(rstr,"+"); return}

        print_r(auto_cast(regx))
        fmt.print(")")
        fmt.print("+")
    case Wild:
        fmt.print("*")
    case Power:
        rstr,ok := regx.ex^.(Rstring)
        if ok {fmt.print(rstr,"+"); return}


        fmt.print("(")
        print_r(regx.ex)
        fmt.print(")")
        fmt.print("^",regx.n)
    }
    fmt.print("Finished printing:",_regx)
}

Regex :: union{
    Rstring,
    Choice,
    Optional,
    Seq,
    Star,
    Plus,
    Wild,
    Power,
}

Rstring :: string
Choice  :: []^Regex
Optional:: distinct ^Regex
Seq     :: struct{lhs:^Regex,rhs:^Regex}
Star    :: distinct ^Regex
Plus    :: distinct ^Regex
Wild    :: struct{}
Power   :: struct{ex:^Regex,n:int}