exaloop / codon

A high-performance, zero-overhead, extensible Python compiler using LLVM
https://docs.exaloop.io/codon
Other
15.01k stars 517 forks source link

if else not working for imports #424

Open JohnSzetela opened 1 year ago

JohnSzetela commented 1 year ago

I'm trying to build a Codon version of my markup language. I want to have the Python and Codon both work. The Python scheme for partitioning imports isn't working for Codon. It looks like you read the import file even if it's in a False segment of an If Else selector? I placed the resulting errors as comments under the imports. The Python version of the code works fine without the comments... codonHeader.txt

elisbyberi commented 1 year ago

@JohnSzetela Dead code is compiled. See https://github.com/exaloop/codon/issues/328#issuecomment-1496144848

You need to preprocess your Codon/Python code. See C Preprocessor conditional compilation, it can be done very easily using Python.

JohnSzetela commented 1 year ago

Thanks Elis! I rather suspected as much. I guess that compiling dead code is an interesting decision. I do already preprocess the base Python code to produce consumable Codon code. I would prefer to do it inside the code so that things were cleaner and more easily debugged. It might also be nice to have a few Codon defines accessible from the Python code. I now use your -DCodon=1 compiler directive, but it would be nice if the compiler had a few defines like Codon, Version, Options (Like -release). I can't use them to condition erroneous Codon code but they would be nice to know. Lastly it would be very nice to be able to run dir() on some of your includes to know what they have in them. Yes, I can read the code, but the code I read and what I run aren't quite the same are they? I know that documentation is a bear, but perhaps you could parse your library and build a list of classes, variables and functions... Thanks again. I quite like Codon and if I ever port some of my code I'll see how it does. Just avoiding the Python startup would be a great save for me! I also prefer native code. I also greatly prefer Static Native Code! I know this may not work for the Python library but it could for Codon. (I've had problems with keeping libraries and code synchronized in the past. I would prefer to not see them again.)

inumanag commented 1 year ago

Imports are handled at the compile level, so their code will be in.

However, you can jump over that if you use static conditions in if statement (e.g. if 0: import ... will not compile that import if it is not used anywhere else in -release mode). Otherwise, there is no way for us to know in advance will that import be used or not.

And thanks for the suggestions: we are working on the missing stdlib features. dir is planned to land in the subsequent releases, as well as other static variables (such as __codon__ etc).