FAQ  •  Register  •  Login

array bounds must be constant

<<

slenkar

Posts: 253

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Tue Feb 22, 2011 5:02 pm

array bounds must be constant

How do I create an array with an integer instead of a literal number?

I want to do this:
  Code:
DIM personX (numPersons) as ubyte
DIM personY (numPersons) as ubyte
DIM personHealth (numPersons) as ubyte
DIM personWeapon (numPersons) as ubyte


instead of:
  Code:
DIM personX (6) as ubyte
DIM personY (6) as ubyte
DIM personHealth (6) as ubyte
DIM personWeapon (6) as ubyte

just in case I decide to change my mind about the number of persons
<<

boriel

Site Admin

Posts: 1144

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Feb 22, 2011 6:15 pm

Re: array bounds must be constant

Unfortunately this is not (yet) supported. What you're asking is a DYNAMIC ARRAY. :( This is a feature planned for 1.2.8 (or higher). Dynamic array are a bit trickier, slower and might take a little more memory, but on the other hand are more powerful and allow redimension during runtime. Meanwhile, you can create an array with a MAXPERSONS size (e.g. 20), and use a variable to store the real number of persons:
  Code:
CONST MaxPersons As Ubyte  = 20
DIM a(MaxPersons) As Ubyte
LET numPersons = 10 : REM number of persons by now...
FOR i = 1 TO numPersons
    LET a(i) = RND * 10
NEXT i

And yes, you will allocate 10 unused positions.
DYNAMIC Arrays are planned for the next release, please, be patient.
<<

slenkar

Posts: 253

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Tue Feb 22, 2011 6:18 pm

Re: array bounds must be constant

ah const, I didnt know we had that type thanks :D
<<

boriel

Site Admin

Posts: 1144

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Feb 22, 2011 6:48 pm

Re: array bounds must be constant

slenkar wrote:ah const, I didnt know we had that type thanks :D

You can also use #define (like a C preprocessor):
  Code:
#define MaxPersons 20

In fact, what's new in 1.2.7 is the complete new preprocessor, which allow powerful macros to be used as inline functions
  Code:
#define MyFunc(x, y)  \
    PRINT x * y + 1; \
    PRINT " is the result"

MyFunc(3, 2)

The above program is literally converted to
  Code:
PRINT 3 * 2 + 1;
PRINT " is the result"

And even much more complex examples!
<<

slenkar

Posts: 253

Joined: Sun Feb 13, 2011 3:33 am

Location: Kentucky US, used to be Birmingham UK

Post Tue Feb 22, 2011 7:55 pm

Re: array bounds must be constant

inline functions are cool too ;)
<<

compiuter

Posts: 51

Joined: Mon May 10, 2010 10:47 pm

Post Wed Feb 23, 2011 12:13 am

Re: array bounds must be constant

Hi, Can I use #define functions in fastcalls or subs?
<<

boriel

Site Admin

Posts: 1144

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Wed Feb 23, 2011 12:30 am

Re: array bounds must be constant

compiuter wrote:Hi, Can I use #define functions in fastcalls or subs?

Yes; #define are just MACROS. Wherever you invoke them, they will be *replaced* with its definition.
<<

compiuter

Posts: 51

Joined: Mon May 10, 2010 10:47 pm

Post Sat Feb 26, 2011 9:15 am

Re: array bounds must be constant

macros functions created with #define can contain embebed asm or only basic?
<<

boriel

Site Admin

Posts: 1144

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Sat Feb 26, 2011 11:22 am

Re: array bounds must be constant

compiuter wrote:macros functions created with #define can contain embebed asm or only basic?

Whatever! Of course, if you must use ASM...END ASM to use macros. Remember also to end each line with \ or _ character (line break continue), to tell the compiler those lines are macro ones:
  Code:
DIM a As Ubyte = 1

#define MyMacro  \
    PRINT a; " MACRO START" \
    ASM \
    ld hl, _a \
    inc (hl) \
    END ASM \
    PRINT a; " MACRO END"

PRINT "HELLO WORLD"
MyMacro

Notice line continuations "\". You can use "_" instead.
You can think of macros like inline SUBs.

Update: errr... :oops: Just discovered the ASM inside the macro wasn't being correctly supported!. This bug has been fixed and now it works. Please download r2109
<<

compiuter

Posts: 51

Joined: Mon May 10, 2010 10:47 pm

Post Tue Mar 08, 2011 2:24 pm

Re: array bounds must be constant

  Code:
#define MyFunc(x, y)  \
    PRINT x * y + 1; \
    PRINT " is the result"

MyFunc(3, 2)


I was testing and I can not compile macros with parameters. :(
<<

boriel

Site Admin

Posts: 1144

Joined: Wed Nov 01, 2006 6:18 pm

Location: Santa Cruz de Tenerife, Spain

Post Tue Mar 08, 2011 3:02 pm

Re: array bounds must be constant

It works ok to me. :?:
Which compiler version are you using? Download 1.2.7 or the latest devel, and try again...
<<

compiuter

Posts: 51

Joined: Mon May 10, 2010 10:47 pm

Post Tue Mar 08, 2011 10:36 pm

Re: array bounds must be constant

Perhaps the version. I´ll take a look.

Return to Help & Support

Who is online

Users browsing this forum: No registered users and 0 guests

cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin for Free Forums/DivisionCore.

phpBB SEO