Shop OBEX P1 Docs P2 Docs Learn Events
"#include" files under SPIN — Parallax Forums

"#include" files under SPIN

Alex.StanfieldAlex.Stanfield Posts: 198
edited 2012-05-03 10:30 in Propeller 1
Is there a statement to get CON sections included from an external file? (like an #include directive under C/C++)

Problem is to get all modules of my project to use the same constant definitions in order to minimize chances of different definitions during the several iterations/unit testing of different modules.

The most I could imagine is to build an object for all my definitions and then call them with the # connector, eg:

definition object
'' PIN definition module (pins.spin)
''
CON
  BUZZER = 6

PUB null

calling module(s)
OBJ
  PIN  : "pins"

PUB void
   beep(PIN#BUZZER)

but although it works it not as elegant as an #include and it wastes 3 longs for the null procedure (PropTool will not compile a module with no PUB routines)

Any ideas?

Alex

Comments

  • Mike GMike G Posts: 2,702
    edited 2012-05-02 07:21
    That's how I do it. Create a file that hold constants and enums. I call it Globals.spin. Usually, I'll have a static variable that i can expose using the required method. IsNull is handy.

    There is a "file" directive that can be used to load a DAT section.
  • trodosstrodoss Posts: 577
    edited 2012-05-02 07:58
    In the case of CON's, that would be a way (as MikeG mentions). Having to use 3 longs just to include constants doesn't seem ideal.

    If you are looking for use of #include (including data), there is a way of handling that at least (the "file" directive).
    DAT
    tiles   file "tiles.dat"
    

    The open-source Spin compiler being developed (http://code.google.com/p/open-source-spin-compiler/) may (eventually) have pre-processing capability, and some of the other Spin compilers (like homespun) have some basic pre-processing directives.
  • Cluso99Cluso99 Posts: 18,069
    edited 2012-05-02 20:02
    There are two ways to use includes...

    1. Use Brad's bst or Mpark's homespun

    2. Use Andy's PreSpin '' Compile with PropTool + "PreSpin 0.3" by Andy Schenk www.insonix.ch/propeller
    '#include "__MODULE.spin" ' include the common code for OS modules
    Warning: Don't forget the comment char ' at the start of the '#include comand!
  • Alex.StanfieldAlex.Stanfield Posts: 198
    edited 2012-05-03 07:57
    Cluso99 wrote: »
    There are two ways to use includes...

    1. Use Brad's bst or Mpark's homespun

    2. Use Andy's PreSpin '' Compile with PropTool + "PreSpin 0.3" by Andy Schenk www.insonix.ch/propeller
    '#include "__MODULE.spin" ' include the common code for OS modules
    Warning: Don't forget the comment char ' at the start of the '#include comand!

    I just downloaded the latest fix of bst but I couldn't find the #include directive in the manual and I couldn't get it to work by trying :tongue:. Is there a way to use it?

    I haven't tried PreSpin but from the description seems to be a one-file solution, does it work across nested objects?

    Thanks to everyone for the comments.

    Alex
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-05-03 09:02
    The 12-byte overhead with the "globals.spin" technique doesn't seem to be a big issue. The advantage to this technique is that it works with all the Spin compilers. 12 bytes out of 32K is fairly insignificant.
  • Mike GMike G Posts: 2,702
    edited 2012-05-03 09:32
    With the "global.spin" technique, I always find a use for the required (12-byte) method.
    DAT
      null         byte   $00
      globalFlag   byte   $00
    
    PUB IsNull(value)
      return value == null
    
    PUB _Flag
      return globalFlag   
    
    PUB  Flag_(value)
      globalFlag  := value
    
  • bruceebrucee Posts: 239
    edited 2012-05-03 09:43
    Why reinvent the wheel?

    The C pre-processor is perfectly good, well understood by most, well documented, thoroughly debugged, and open-source. It's relatively easy edits to handle comments that aren't //
  • Alex.StanfieldAlex.Stanfield Posts: 198
    edited 2012-05-03 10:06
    brucee wrote: »
    Why reinvent the wheel?

    The C pre-processor is perfectly good, well understood by most, well documented, thoroughly debugged, and open-source. It's relatively easy edits to handle comments that aren't //

    So how do we use a C preprocessor (+ something else i guess) to compile a spin project?
  • Alex.StanfieldAlex.Stanfield Posts: 198
    edited 2012-05-03 10:20
    Mike G wrote: »
    With the "global.spin" technique, I always find a use for the required (12-byte) method.
    DAT
      null         byte   $00
      globalFlag   byte   $00
    
    PUB IsNull(value)
      return value == null
    
    PUB _Flag
      return globalFlag   
    
    PUB  Flag_(value)
      globalFlag  := value
    

    Ok, granted it's a workable solution.

    However a project wide pre-processor does look useful, BST has it along with some interesting features like eliminating unsed routines. Has anyone discovered if it has an equivalent form of #include ?

    Alex
  • bruceebrucee Posts: 239
    edited 2012-05-03 10:26
    The C pre-processor reads in a file and spits out another file, doing all the changes you need.

    {comments may cause some issues, but worst would be to alter the comment before compilation and the compiler would not care}

    ' comments like BASIC are handled by a altered version I use for BASIC.

    point me someplace that I can upload it, and it's yours, its GNU licensed anyway...
  • Cluso99Cluso99 Posts: 18,069
    edited 2012-05-03 10:30
    I just downloaded the latest fix of bst but I couldn't find the #include directive in the manual and I couldn't get it to work by trying :tongue:. Is there a way to use it?

    I haven't tried PreSpin but from the description seems to be a one-file solution, does it work across nested objects?

    Thanks to everyone for the comments.

    Alex
    I dont think I have actually used the #include directive in bst but was sure someone recently told me it worked there. Homespun works because Catalina C uses it. PreSpin works down nested objects. Just remember to save your file(s) - but no need to close them - before clicking prespin.
Sign In or Register to comment.