LingDong- / wax

A tiny programming language that transpiles to C, C++, Java, TypeScript, Python, C#, Swift, Lua and WebAssembly 🚀
https://waxc.netlify.app/
MIT License
783 stars 45 forks source link

Does wax use short-circuit evaluation for boolean expressions? #6

Closed JobLeonard closed 3 years ago

JobLeonard commented 3 years ago

Hi, this is a cool project, I'm really enjoying reading through it right now.

Quick question regarding this part of the QUICKSTART.MD documentation:

a && b && c is:

(&& a b c)

which the compiler will read as:

(&& (&& a b) c)

I'm not proficient in all the languages that Wax compiles to, but the ones that I do know would use short-circuit evaluation on this expression - that is, if a is false, then expressions b and c are not evaluated. Does wax also do this to? Or does it always evaluate all expressions? In either case, does it take special care that all compile targets behave as expected? Or does it not make any assumptions about this at all?

https://en.wikipedia.org/wiki/Short-circuit_evaluation

LingDong- commented 3 years ago

Hi @JobLeonard,

Yes, wax takes special care that && || and a?b:c are short-circuited in all targets.

tmp = a
if (tmp) tmp = b
c = !!tmp

Glad you're enjoying the project!

JobLeonard commented 3 years ago

Cool, thanks for explaining! :)

Quickly added line to the quickstart doc to mention it there too