stfc / HartreeParticleDSL

MIT License
1 stars 1 forks source link

Passing unresolved calls as function arguments causes indentation issues. #42

Open LonelyCat124 opened 2 years ago

LonelyCat124 commented 2 years ago

For example:

create_variable(c_double, "r", random_double())

should result in

double r = random_double();

but instead ends up with:

double r =         random_double();
;
LonelyCat124 commented 2 years ago

Ok, so I think this happens with the C_AOS module because a function call has been assumed to be an entire statement, but it doesn't have to be, so for example when coming across: c_function() - this is a full statement, so we need to result in c_function();\n However, if we have r = random_double() then this is not, so random_double() should not be transformed to random_double();\n, only random_double()

Since the other C/C++-based backends are build upon this backend, I imagine the same issue exists within those backends as well.

The code generated is valid for simple examples, however if I had:

r = random_double() + random_double()

the output code is:

        r = (         random_double( );
 +         random_double( );
 );

which is very clearly not valid C code, so this needs to be fixed ASAP.

LonelyCat124 commented 2 years ago

Ok - so the problem is caused by the final line in call_language_function:

string = string + ";\n"

(and adding indentation in front of the string)

Removing this fixes the cases where the entire line isn't just a function call. If the entire line IS a function call, then this breaks, because suddenly that line of code is not valid.

The way to solve this I think is to find the parent when I reach a visit_Call() that is not part of another assignment or similar, and add the indentation & ";\n" from the parent.