ooc-lang / rock

:ocean: self-hosted ooc compiler that generates c99
http://ooc-lang.org/
MIT License
403 stars 40 forks source link

Detect common root for numbers #861

Closed horasal closed 9 years ago

horasal commented 9 years ago

The first step to detect common root for numbers. The following codes will work after this commit:

check: func<T>(t: T, name: String){
    if(t class name != name){
        Exception new("Expected %s, get %s!" format(name, t class name)) throw()
    }
}

mydouble: cover from Double

check(1.f + 1.l, "LDouble")
check(1.f + 1., "Double")
check(1.f + 1.f, "Float")
check(1l + 1.f, "Float")
check(1.f + 1. as mydouble, "mydouble")

In this patch, every "base type" (c type) has a score. For ooc types are cover from c types, we find the root and return the type with higher score. This is not a really "good" solution, just as @shamanas said, we need to handle each case manually.

And also, Standard says that:

Otherwise, if one operand is a long int and the other unsigned int, then if a long int can represent all the values of an unsigned int, the unsigned int shall be converted to a long int; otherwise both operands shall be converted to unsigned long int.

I am not sure how to judge if a type can represent other at compile time....

horasal commented 9 years ago

please hord on, something is broken...

horasal commented 9 years ago

fixed.

but I'm wondering if I'm introducing more bugs...