thebennybox-Community / Community-Compiler

MIT License
16 stars 5 forks source link

Affix Funtions #15

Open jorolf opened 6 years ago

jorolf commented 6 years ago

Instead of allowing user to use operator overloading we should allow infix funtions.

Infix are funtions that take two parameters and are marked with infix. They can be used like this:

param1 funtion param2

the declaration would look something like this:

infix fn name(param1: type, param2: type): type {
    //do stuff
    type
}

possible usages would be operator overloading:

infix fn -(vec1: vec3, vec2: vec3): vec3 {
    vec3(vec1.x - vec2.x, ...)
}

fn distance(vec1: vec3, vec2: vec3): i32 {
    (vec1 - vec2).length()
}

or ranges:

fn downTo(from: i32, to: i32): i32[] {
    (to...from).reverse() //experimental syntax
}

fn main() {
    for(i in 5 downTo 1) {
        print(i);
    }
}
jorolf commented 6 years ago

We are also going to include other affixes like suffix and prefix this would enable us to also define unary operators:

prefix fn dispose(...) {
    thing.dispose();
}

dispose [...];

We are also going to split out these functions to normal functions (fn) and operators (op). The difference would be that operators must have special character names (e.g. *+-/$><) and affix funtions need to be seperated by spaces when being used

infix op -(a: i32, b: i32) { a-b } //pointless but get's the point across

infix fn minus(a: i32, b: i32) { a-b }

a-b: possible a - b: possible as well a minus b: possible aminusb: not possible -> ambiguous is it minus(a, b) or the variable aminusb

timtomtim7 commented 6 years ago

Affix operator ambiguity:

Code Result
a* -b Syntax error, two expressions a* and -b
a *-b Syntax error, two expressions a and *-b
a*- b Syntax error, two expressions a*- and b
a*-b Syntax error
a * - b Syntax error
a* - b Infix operator - with operands a* and b
a * -b Infix operator * with operands a and -b
a *- b Infix operator *- with operands a and b
Myvar commented 6 years ago

we do need to handel precedence

@precedence(0)

smaller numbers takes precedence