pure-c / purec

C backend for PureScript
232 stars 8 forks source link

Write a transform that turns expressions into statements #48

Open felixSchl opened 5 years ago

felixSchl commented 5 years ago

The current output is heavily based on expressions, meaning that we apply functions and feed the result into other functions etc. It would be interesting to explore a transform that would turn these expressions into statements by generating fresh variable names in the current block scope and then perform the wiring up.

For example:

Turn this:

purs_any_app(f, purs_any_app(g, purs_any_int_new(100)))

into this:

const ANY * $value0 = purs_any_int_new(100);
const ANY * $value1 = purs_any_app(g, $value0);
const ANY * $value2 = purs_any_app(f, $value1);
return $value2;

At least one benefit of this is that it makes it easier to set breakpoints in generated programs. I am currently performing these transformations by hand when debugging generated output.

felixSchl commented 4 years ago

One thing to potentially look out for though is increased stack usage for all these temporary variables, especially with fat pointers (#59, #54.) Perhaps clang and gcc already optimize for this? It would be nice to perform these optimisations on our AST as well, however, to produce more readable and more terse output. #54 introduces a LOT of noise in the generated code.