haumea-lang / haumea-rs

Haumea is an experimental language designed to be simple and easy to learn and program in.
MIT License
7 stars 2 forks source link

Output correctly formatted C #22

Open bates64 opened 8 years ago

bates64 commented 8 years ago

For example, examples/factorial.hau compiles to..

long factorial(long n) {

    {
        if  (n == 0l)             return 1l;        else             return (n * factorial((n - 1l)));
    }

    return 0l;
}

int main() {

    {
        display(factorial(5l));

    }

    return 0l;
}
jeandrek commented 8 years ago

Does the readability of output matter?

bates64 commented 8 years ago

No, but it'd be nice to output/format code readably anyway :stuck_out_tongue_winking_eye:

bates64 commented 8 years ago

Following the discussion in #23, looks like it's worth trying to output 1TBS as it's pretty standard?

long factorial(long n)
{
    if (n == 0l) {
        return 1l;
    } else {
      return (n * factorial((n - 1l)));
    }

    return 0l;
}

int main()
{
    display(factorial(5l));
    return 0l;
}
TheMonsterFromTheDeep commented 8 years ago

It also might be a good idea to output any comments as well, so people could see exactly how their code corresponded to C (in the cases that it did).

BookOwl commented 8 years ago

Well, right now this is what compiling examples/factorial.hau produces:

/* Haumea prolog */
#include <stdio.h>

long display(long n) {
    printf("%ld\n", n);
    return 0;
}

long read() {
    printf("Enter an integer: ");
    long n;
    scanf("%ld", &n);
    return n;
}

/* End prolog */

/* Start compiled program */

long factorial(long n) {
    {
        if (n == 0l)
            return 1l;
        else
            return (n * factorial((n - 1l)));
    }
    return 0l;
}

int main() {
    {
        display(factorial(5l));
    }
    return 0l;
}

/* End compiled program */

Does that mean this issue can be closed?

BookOwl commented 8 years ago

@TheMonsterFromTheDeep, good idea with the comments.

jeandrek commented 8 years ago

Does that mean this issue can be closed?

I think this is why @nanalan hasn't closed it yet.

bates64 commented 8 years ago

Also @BookOwl we should probably never output block braces if there's only one statement inside.

jeandrek commented 8 years ago

@nanalan The idea of the 1TBS is that the statements in if/while/for statements are always blocks.

bates64 commented 8 years ago

@Jonathan50 derp

BookOwl commented 8 years ago

You guys can try and make the C prettier if you want, but I don't think y'all should spend too much time on it, considering that the ultimate consumer of the C is a compiler, and that we may not even use C soon (#18).

bates64 commented 8 years ago

Being able to output well-formatted code is a good idea though in terms of people moving on from the language :smile: