ponylang / ponyc

Pony is an open-source, actor-model, capabilities-secure, high performance programming language
http://www.ponylang.io
BSD 2-Clause "Simplified" License
5.73k stars 415 forks source link

Conditional code #8

Closed andymcn closed 10 years ago

andymcn commented 10 years ago

We need to allow conditional code, both platform / hardware specific and for debug vs release.

We could switch at the expression, method, type or file level. We need to provide system / build info (OS, CPU capabilities, debug / release). Should we also allow programmer configurable info? This could be to allow the aardvark build to be different to the wombat build. It could also be used to make complex decisions based on OS, CPU, etc jsut once, rather than in every file, function, etc.

andymcn commented 10 years ago

Conclusion of long discussion:

  1. In builtin we define functions of the form: class Platform fun tag linux(): Bool => compiler_intrinsic Currently defined functions are linux, windows, osx and debug.
  2. We add a condition clause to "use" commands, which is currently only legal for the lib: scheme. This allows the use of the Platform functions to specify whether to the use command should be included in each build.
  3. The code generator / llvm branch optimisor completely removes any branches in the code whose condition only depends on the Platform functions. Note that we still fully type check all code paths.
  4. We explicitly do not provide any mechanism for conditional code based on CPU capabilities (eg SSE), endianness or other factor that can only be determined at runtime. Standard if expressions can of course be used, with the associated overhead.
sylvanc commented 10 years ago

This is in the code generator now. It works with if expressions and anything the compiler can determine to be a constant expression, so you can combine Platform booleans with other booleans, and if that results in a branch that can never execute, that branch won't be generated.

This isn't implemented for while loops.

Andy, that leaves the "use lib" conditional guard implementation to you.

andymcn commented 10 years ago

The "lib:" handler for use commands isn't in yet, but the conditional guards are.

I've actually made it so that all use commands can have a condition, including using packages. The condition is evaluated in the scope pass (ie where we already handled use commands). If the condition fails we pretend that use command doesn't exist.

This allows entire packages to be platform specific if you want to.