• Print

Author Topic: '$INCLUDE subs at beginning of code  (Read 310 times)

duo

  • Guest
'$INCLUDE subs at beginning of code
« on: March 03, 2011, 10:09:28 AM »
It would be nice to be able to include subs at the beginning of qb64 code, like in all other programming languages. Would this be a difficult addition?

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: '$INCLUDE subs at beginning of code
« Reply #1 on: March 03, 2011, 10:20:16 AM »
WHY?

There is no BIG difference including them at the END. Including them is VERY simple!

DEFS, DIMS, SHARED and DATA statements need to be included BEFORE the main code...
« Last Edit: March 03, 2011, 12:49:48 PM by Clippy »
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

codeguy

  • Hero Member
  • *****
  • Posts: 3552
  • what the h3ll did i name that code?
    • stuff at dkm
    • Email
Re: '$INCLUDE subs at beginning of code
« Reply #2 on: March 03, 2011, 10:31:48 AM »
and if ya have nested includes, try my OpenInclude() sub. works perfectly every time and even works in qbxx.
Code: [Select]
SUB OpenInclude (f$, CodeText() AS STRING, Lines&)
IF f$ > "" THEN
    c% = FREEFILE
    OPEN "i", c%, f$
    DO
        IF EOF(c%) THEN
            EXIT DO
        ELSE
            LINE INPUT #c%, readx$
            AddStringToArray CodeText(), Lines&, readx$
            IF LCASE$(LEFT$(RTRIM$(LTRIM$(CodeText(Lines&))), LEN("'$include:"))) = "'$include:" THEN
                insinc% = INSTR(LCASE$(CodeText(Lines&)), "'$include:")
                MID$(CodeText(Lines&), insinc%) = "'included:"
                insc1% = INSTR(LEN("'$include:"), CodeText(Lines&), "'")
                insc2% = INSTR(insc1% + 1, CodeText(Lines&), "'")
                IF insc2% > insc1% THEN
                    PRINT MID$(CodeText(Lines&), insc1% + 1, insc2% - insc1% - 1)
                    OpenInclude MID$(CodeText(Lines&), insc1% + 1, insc2% - insc1% - 1), CodeText(), Lines&
                END IF
            END IF
            Lines& = Lines& + 1
        END IF
    LOOP
    CLOSE c%
END IF
END SUB

SUB AddStringToArray (CodeText$(), lines&, readx$)
IF lines& > UBOUND(CodeText$) THEN
    REDIM _PRESERVE CodeText$(LBOUND(CodeText$) TO lines&)
END IF
CodeText$(lines&) = readx$
END SUB
corrected as i forgot to include assignment in AddStringToArray()
this one is a generalized sub and works correctly with all forms of '$include:, including the '$include: 'yourfile' (found in qbxx) and '$include:'yourfile' (found in qb64). have it write an output file and then use the created file which will have all lines from all $include(d) files --  8)
in some cases, this was overwriting opening apostrophe, causing malfunction. now tested and approved.
« Last Edit: March 09, 2011, 11:09:12 PM by codeguy »
http://denteddisk.forums-free.com/make-an-appointment-with-the-resident-code-guru-f34.html

unseenmachine

  • Hero Member
  • *****
  • Posts: 3285
  • A fish, a fish, a fishy o!
Re: '$INCLUDE subs at beginning of code
« Reply #3 on: March 03, 2011, 12:00:42 PM »
Quote
There is no BIG difference including them at the END.

I have found this not to be the case. If i want to make a new sub that uses a type defined in a lib, i have to make the sub inderneath the include line to have access to the type.

Stick this in test.bm

Code: [Select]
TYPE Position
  X AS INTEGER
  Y AS INTEGER
END TYPE

and then try this

Code: [Select]
DIM A AS Position

sub NewPosition (Pos as position)

end sub

REM $INclude:'Test.bm'

Then move the sub below the include, it works again.
UnseenGDK Download : http://dl.dropbox.com/u/8822351/UnseenGDK.bm
GDK Tutorial : http://dl.dropbox.com/u/8822351/UnseenGDK_Tutorial.doc
VQB02 : http://dl.dropbox.com/u/8822351/VQB02.zip

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: '$INCLUDE subs at beginning of code
« Reply #4 on: March 03, 2011, 12:05:48 PM »
QB64 also allows you to put the TYPE INSIDE of the SUB or an initiate SUB and it is GLOBAL(shared with all areas of the program).
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

Cyperium

  • Hero Member
  • *****
  • Posts: 3285
  • Knowledge is good, but understanding is better
    • Cyperium
    • Email
Re: '$INCLUDE subs at beginning of code
« Reply #5 on: March 03, 2011, 04:37:17 PM »
I agree with duo, it would be much more intuitive to have one include file instead of many like we have to have now. Subs aren't allowed at the top of the program anyway so why not shuffle them to the end when it encounters them in the include file? Would give a much better user experience at little effort programmatically. I would hate to explain that the .bi file should be on the top and the .bm (what is that???) file should be on the bottom. It's enough to have one that is on the top. Always, like QB 4.5.
Venture - New Prototype, QB64 Editor v1.95b (linux compatible, if you compile the source).

Clippy

  • Hero Member
  • *****
  • Posts: 16431
  • I LOVE π = 4 * ATN(1)    Use the QB64 WIKI >>>
    • Pete's Qbasic Site
    • Email
Re: '$INCLUDE subs at beginning of code
« Reply #6 on: March 03, 2011, 04:54:57 PM »
Naturally it would be BETTER to have them all in ONE file but NOT YET!

Are YOU gonna press Galleon to make it possible? I have my own wants.

Wouldn't it be nice if a LOT of things were different...in the MEAN TIME, LIVE WITH IT!
QB64 WIKI: Main Page
Download Q-Basics Code Demo: Q-Basics.zip
Download QB64 BAT, IconAdder and VBS shortcuts: QB64BAT.zip
Download QB64 DLL files in a ZIP: Program64.zip

Cyperium

  • Hero Member
  • *****
  • Posts: 3285
  • Knowledge is good, but understanding is better
    • Cyperium
    • Email
Re: '$INCLUDE subs at beginning of code
« Reply #7 on: March 03, 2011, 05:14:23 PM »
I do live with it and it's no big problem, the directqb library is setup as two files, but I have to agree that it's better to have one single file. You asked WHY? and I gave my answer. It doesn't matter if it happens today or in a year as long as it happens and I don't want Galleon to think that we have all settled on this setup, it's good that this comes up from time to time so Galleon can see that there is a need for it.
Venture - New Prototype, QB64 Editor v1.95b (linux compatible, if you compile the source).

  • Print