Is there a spin version of #include?
vetter
Posts: 8
in Propeller 1
I'm writing what is starting to be a fairly sizable code and I've broken it up into individual objects. So now I have individual objects for things like my LCD, real time clock, SDcard etc. Several of the files call functions that are located inside other objects, such as the SDcard accessing the real time clock functions. Having multiple instances of the same objects is starting to reduce a significant amount of space I have available for my main code and I'm wondering if spin has some form of #include like C does. I've looked through other threads and wasn't able to find what I was looking for (or I didn't realize what I was looking at ) however I did fint the PreSpin file which is supposed to have the #include function. Unfortunatley my antivirus seems to be stopping that from opening and I can't disable the antivirus at the moment. Thanks in advance
Comments
So including objects multiple times does not hurt as much as you think.
Sadly Spin does not offer a way to access sub objects from other sub objects. You have to build wrapper functions to archive that.
Enjoy!
Mike
I feel certain it's possible, as Parallax added conditional compilation (not #include, though) to PBASIC more than 15 years ago. For the moment I use constants as switches to run certain sections of code, but I'd really like to remove them from the compilation when not needed (e.g., debugging tools).
There may be some unexposed tools lurking in the compiler used by PropellerIDE. I think what needs to happen is to allow the user to control compiler switches.
I hope the Antivirus software will not have a problem with it anymore. Sometimes heuristic scanner methodes of Antivirus tools have a problem with compiled PureBasic programs.
The example and description files are not included, you can download a full ZIP from:
here
Andy
homespun has #include.
2. #include may not help you. All it does is deposit a (literal) copy of the file into the source code. This can make programming repetitive things easier, but it won't save on code size.
I do kinda like Spin. In the past I've avoided C/C++ like the plague, but working with it on my Propeller bots (and a couple of other projects), I'm finding I rather prefer it to Spin.
Amanda
#include is probably not a good idea for any Spin compiler, since including things via OBJ blocks will always use less memory than #including it. Spin is a very different language than C. The reason C has #include is because of how translation units work. Since Spin doesn't really have translation units, #include isn't necessary.
Do you really need #include, or are you splitting your code into objects wrong?
Andy has some good examples of using PreSpin to conditionally select an output stream.
OpenSpin has #define / #ifdef. So do fastspin, bstc, and (I think) homespun.
Wrapper methods are useful if you want your objects to reference a small number of objects. However, the same number of objects will be loaded whether you use wrapper methods or not since the wrapper method will reference low-level methods. One feature of Spin is that an object can only reference objects located after it memory. You cannot reference a method in an object loaded before another object.
As an example, let's say there are two objects -- A and B. A can call methods in B, but B cannot call methods in A. This would cause an infinite circular reference if it were allowed. You can work around this by creating a third object, C, that contains methods used by both A and B. In some cases I will include small duplicate methods in low-level object so I don't have to create another object.
Couple that with conditional compilation directives, and Prop tool would be the cat's meow.
There is no reason whatsoever to need conditional compilation to prevent the instantiation of an object. If the object is not referenced, it should be removed at link time.
If-then-else and case statement folding when the predicates can be reduced to constants at compile time are far, far, far superior to any sort of conditional compilation hack.
Similarly, theres no reason to use include directives for pin constants. Simply have an object with those named numbers and instantiate that object when the pin names are required.
Perhaps "if then else" and "case" statements are superior, but I would be happy to settle for "includes" to reduce the clutter the programmer has to wade through in the source code, and "conditional compilation" to simplify coding for different hardware and software options. I suspect includes and conditionals are also simpler to add to an existing compiler as well.
But what is the alternative?
If I want to compile my code for different operating systems or platforms or with different capabilities and features, then how else do we do it?
It requires choices to be made at compile time.
Arguably one could support that kind of configuration in the language syntax itself, rather than bolting on a pre-processor like C/C++. but it needs to be there. Somewhere.
Somewhere down in the bowels of your code it has to be built this way or that for different systems.
Even at the higher levels one may want to include or exclude features.
Include files and ifdef etc are a crude way of "meta programming" like that.
I don't know what is better than that.
-Phil
-Phil
I guess one could leave the code untouched and swap out the actual files being referenced.
Or one could do some funky things with symbolic links to alternative files. On proper operating systems anyway.
http://forums.parallax.com/discussion/122321/pst-with-usb-cable-unplugged
Simply change the name in the obj declaration.
Maybe not what you re expecting, but it works fine.
Massimo