phillipstanleymarbell / Noisy-lang-compiler

Noisy language compiler
MIT License
17 stars 1 forks source link

Optimized Function Override #633

Closed PeiMu closed 1 year ago

PeiMu commented 1 year ago

When a function is called more than one time with different ranges of arguments, and if this results in different optimized functions, it could be necessary to generate the related optimized function. Somehow like the override features in objective-oriented languages.

Here's a possible example:

typedef double bmx055xAcceleration; // [3 mjf, 10 mjf]
typedef double bmx055yAcceleration; // [15 mjf, 36 mjf]

double funcB(double param1) {
    double ret;
    if (param1 > 15) {
        ret = param1 + 100;
    } else {
        ret = param1 * 100;
    }
    return ret;
}

double funcA(bmx055xAcceleration x, bmx055yAcceleration y) {
    double ret1 = funcB(x);
    double ret2 = funcB(y+x);
    double ret3 = funcB(x+15.786);
    return ret1 + ret2 + ret3;
}

int main(int argc, char** argv) {
    char* pEnd;
    bmx055xAcceleration x;
    bmx055yAcceleration y;
    if (argc == 3) {
        x = strtod(argv[1], &pEnd);
        y = strtod(argv[2], &pEnd);
    } else {
        x = 8.75;
        y = 15.781;
    }
    double res = funcA(x, y);
    printf("res = %f\n", res);
    return 0;
}