home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / Basic / MAXONB32.DMS / in.adf / docs.lha / Concepts < prev    next >
Encoding:
Text File  |  1994-10-31  |  47.0 KB  |  1,633 lines

  1. Concepts
  2.  
  3.  
  4. This chapter describes the technical details of
  5. the MaxonBASIC language, together with some of
  6. its more advanced features. It is intended for
  7. users who already have a good understanding of
  8. the BASIC language and want to get to grips
  9. with MaxonBASIC quickly.
  10. If you are new to BASIC please read the
  11. Tutorials chapter first.
  12.  
  13.  
  14. Character Set
  15.  
  16.  
  17. MaxonBASIC uses plain ASCII characters in its
  18. input files. The following characters have
  19. special meanings:
  20. a-z,   The letters, which are used in
  21. A-Z    reserved words and the user´s
  22.        variable names, labels and sub-
  23.        program names. Lower and upper
  24.        case are treated as the same in
  25.        variable and reserved word
  26.        definitions so that THEN, then and
  27.        Then are all the same reserved
  28.        word.
  29. E, e,  These are also used for exponents
  30. D, d   in numbers.
  31. 0-9    The digits, which are used in
  32.        numbers and can also be used in
  33.        names as long as they are not the
  34.        first character.
  35. .      The full stop or period, which is
  36.        used as the decimal point in
  37.        numbers and can also be used in
  38.        names as long as it is not the
  39.        first character.
  40. %      The percentage sign, which is used
  41.        to indicate that a variable is a
  42.        16 bit integer i.e. whose values
  43.        must be in the range -32768 to
  44.        32767.
  45. &      The ampersand, which is used to
  46.        indicate that variables are long
  47.        integers i.e. whose values must be
  48.        in the range -2^31 to 2^31-1. Also
  49.        used to introduce hexadecimal,
  50.        octal and binary constants.
  51. !      The exclamation mark, which is
  52.        used to indicate that a variable
  53.        is a single-precision floating
  54.        point number.
  55. #      The hash or number sign, which is
  56.        used to indicate that a variable
  57.        is a double precision floating
  58.        point number and also used to
  59.        indicate that certain input/output
  60.        operations are to be directed to
  61.        channels rather than the screen
  62.        (e.g. PRINT #).
  63. $      Used to indicate string variables.
  64. _      The underline character, which can
  65.        be used in variables after the
  66.        first character assuming the
  67.        Underlines in Names (UNDERLINES)
  68.        option has not been turned off. If
  69.        it appears at the start of a
  70.        symbol or the underlines has been
  71.        disabled (NOUNDERLINES) then it
  72.        indicates the rest of the line is
  73.        to be ignored and that the
  74.        following line is to be considered
  75.        part of the current one.
  76. "      The quotation mark or double quote
  77.        which is used to delimit string
  78.        literals.
  79. ´      The apostrophe or single quote
  80.        which is used to indicate that the
  81.        rest of this line is to be
  82.        regarded as a comment.
  83. ( )    The parentheses or round brackets,
  84.        which are used to enclose function
  85.        arguments, over-ride the priority
  86.        of operators and indicate arrays.
  87. + - *  The basic arithmetic operators.
  88. /
  89. =      The assignment operator and
  90.        equality operator.
  91. < >    Less than and greater than
  92.        comparison operators - also used
  93.        as parts of the shift operators.
  94. ^      Exponentiation operator.
  95. \      The back-slash character, which is
  96.        used as the integer division
  97.        operator.
  98. ,      Comma.
  99. ;      Semi-colon.
  100. ?      Used as an abbreviation for PRINT.
  101.  
  102. Other characters with ASCII values lower than
  103. 32 are treated as white space, and ignored so
  104. you may, for example, include form-feed
  105. (chr$(12)) characters to give a new page on
  106. your printer when listed.
  107. Other characters may be used in strings, but
  108. otherwise will generate a warning and will be
  109. ignored.
  110.  
  111.  
  112. Program lines and labels
  113.  
  114. Program lines consist of an optional line
  115. number or label, one or more statements
  116. separated by colons and an optional comment,
  117. which starts with an apostrophe or single
  118. quote.
  119.  
  120. Line numbers may be any number between 1 and
  121. 65529 inclusive. (65529 may seem a strange
  122. number, it is the maximum allowed by Microsoft
  123. BASIC).
  124.  
  125. Line labels consist of any valid variable name
  126. that is not used as a variable or a sub-program
  127. and labels are followed by a colon. There is no
  128. limit to the number of characters in a line
  129. label but lower- and upper-case letters are
  130. treated as the same and the characters must not
  131. be a reserved word. Thus the following line
  132. labels are allowed:
  133.  
  134. Label9999:
  135.  
  136. A.very.long.label.that.causes.problems.to.other
  137. .BASICs:
  138.  
  139. Hello:
  140. The following line labels are the same:
  141.  
  142. Start:
  143. START:
  144. start:
  145. Line numbers and labels may be preceded by
  146. white space. White space is not required after
  147. the last digit or colon. Line numbers may not
  148. contain spaces.
  149. Note: For compatibility with other BASICs we
  150. recommend that you do not use full stops (.) or
  151. underlines ( _ ) in labels and keep them to
  152. less than 40 characters.
  153. Line number 0 is not allowed because it would
  154. be confused when using ON ERROR GOTO 0 which
  155. does not mean go to line 0 if an error occurs.
  156. In general we do not recommend the use of line
  157. numbers since line labels are much more
  158. readable. The exception to this is when using
  159. ERL when the use of line numbers is useful
  160. since otherwise you must change the line
  161. numbers in your program each time you insert or
  162. delete lines in your program.
  163. Most of the time you do not need to use line
  164. numbers or line labels because MaxonBASIC has
  165. such a rich set of structured statements (much
  166. better than Pascal and even more flexible than
  167. C and Modula 2).
  168. You may have many statements per line provided
  169. each is separated by a colon.
  170. MaxonBASIC has an extension to call sub-
  171. programs without the CALL  keyword. However you
  172. cannot do this if the sub-program has no
  173. parameters and is the first statement of a
  174. multi-statement line. For example, if you have:
  175.  
  176. SUB john STATIC
  177. PRINT "John";
  178. END SUB
  179. then
  180.  
  181. john
  182. PRINT " Smith"
  183. will print
  184.  
  185. John Smith
  186. as will
  187.  
  188. call john: print " Smith"
  189. However
  190.  
  191. john: print " Smith"
  192. is wrong because the john: could be a label
  193. definition; normally the compiler will warn you
  194. on the parsing phase if you make this error.
  195. This problem does not apply if the sub-program
  196. has a parameter, for example:
  197.  
  198. SUB john(para$) static
  199. PRINT "John ";para$;
  200. END SUB
  201.  
  202. john "David": print " Smith"
  203. is fine because it cannot be mistaken for a
  204. label.
  205. If an apostrophe or single quote (´) appears on
  206. a line then the rest of the line is treated as
  207. comment and ignored. The only exception to this
  208. is DATA statements which treat the apostrophe
  209. as part of the data. If you want a comment on
  210. such a line precede it with a colon; this will
  211. terminate the DATA statement.
  212. Program lines may be any length theoretically,
  213. but it is generally a good idea to keep them
  214. less than 80 characters so that the whole line
  215. is displayed at once. If you need a line that
  216. is significantly longer than this then the
  217. chances are that the line is more complicated
  218. than it should be as far as ease of
  219. understanding is concerned. There are two
  220. exceptions to this. The FIELD statement where,
  221. for large records, you need many more than 80
  222. characters and the TAGLIST statement where you
  223. need to set up many tags when calling the
  224. operating system.
  225. To get around this the underline character ( _
  226. ) may be used to cause lines to be continued on
  227. the next physical line. Anything after the
  228. underline is ignored.
  229. For example:
  230.  
  231. FIELD #3,20 AS name$, _  ´ surname only
  232.  5 AS initials$ ,_
  233.  50 AS street$, _  ´ include name or number
  234. here
  235.  20 AS town$, _
  236.  20 AS county$ ,_  ´ or state if applicable
  237.  20 AS country$
  238. which would be much more readable than the one-
  239. line equivalent where you would also have to
  240. leave out the comments.
  241. Normally MaxonBASIC allows underlines in
  242. variable names, unlike traditional BASICs.
  243. Underlines are treated as continuation
  244. characters if they are not part of a variable
  245. name. If you are porting programs that have
  246. continuation characters immediately after
  247. identifiers or reserved words then use the
  248. NOUNDERLINES option or de-select the
  249. appropriate box in the Compiler Options
  250. requester.
  251. For example:
  252.  
  253.  IF x THEN_
  254.  PRINT "hello"
  255. will be accepted in some BASICs as a one-line
  256. IF statement (no need for an END IF). Without
  257. the NOUNDERLINES option MaxonBASIC will give an
  258. error because it thinks you are using a
  259. variable called THEN_. To solve this use
  260. UNDERLINES or insert a space in front of the _
  261. character.
  262. If you use NOUNDERLINES and inadvertently use
  263. an identifier containing an underline you may
  264. get a very strange error message because the
  265. compiler has ignored the rest of the line.
  266.  
  267.  
  268. Data Types
  269.  
  270. There are five types of data in MaxonBASIC:
  271.  
  272.  
  273. .ib.Strings;
  274.  
  275. A string is a sequence of characters that may
  276. be up to 16 megabytes long assuming you have
  277. enough memory. Strings may contain any
  278. character with a value of 0 to 255 inclusive.
  279.  
  280.  
  281. .ib.Integers;
  282.  
  283. Integers are numeric and consist of the whole
  284. numbers from -32768 to 32767.
  285.  
  286.  
  287. .ib.Long Integers;
  288.  
  289. Long integers are numeric and consist of the
  290. whole numbers between -2147483648 and
  291. 2147483647.
  292.  
  293.  
  294. .ib.Single precision numbers;
  295.  
  296. Single-precision numbers have approximately
  297. seven digits of precision and a range of 5.4E-
  298. 20 to 9.2E-18 for positive values and -2.7E-20
  299. to -9.2E18 for negative values.
  300.  
  301.  
  302. .ib.Double precision numbers;
  303.  
  304. Double precision numbers have approximately 16
  305. digits of precision and a range of 4.9E-324 to
  306. 1.8E308 for positive numbers and -4.9E-324 to -
  307. 1.8E308 for negative numbers. There is loss of
  308. precision with numbers of magnitude less than
  309. 2.2E-308.
  310.  
  311.  
  312. Constants
  313.  
  314. Constants are values which do not change during
  315. program execution. Constants may be of all 5
  316. types.
  317. A string constant is a sequence of ASCII
  318. characters enclosed in double quotes ("). These
  319. can be any character between ASCII values 32
  320. (space) and 255. To obtain a double-quote in a
  321. string repeat it, so that, for example, the
  322. string consisting of one double quote character
  323. is """". The first is the start of the string,
  324. the second and third form the character itself
  325. and the last is the closing quote.
  326. Numeric constants are formed in one of the
  327. following ways:
  328.  
  329.  
  330. .ib.Decimal numbers;
  331.  
  332. A sequence of decimal digits followed
  333. optionally by a decimal point (.) and more
  334. digits and/or an exponent. An exponent consists
  335. of the letter d, D, e or E followed by a
  336. decimal integer. E indicates single precision
  337. and D indicates double precision. The number
  338. may be preceded by a minus sign as may the
  339. numeric part of the exponent.
  340. The number before the decimal point may be
  341. omitted. The number may be followed by a type
  342. specifier (%,!,& or #).
  343.  
  344.  
  345. .ib.Hexadecimal Constants;
  346.  
  347. Hexadecimal constants start with &H or &h and
  348. are followed by hexadecimal digits(0-9, a-f, A-
  349. F). The number may be followed by a type
  350. specifier (%,!,& or #).
  351. Hexadecimal integer constants between &h8000
  352. and &hFFFF are taken as signed 16 bit integers.
  353. Hexadecimal long integer constants between
  354. &h80000000 and &hFFFFFFFF are treated as signed
  355. 32 bit constants so that for example:
  356.  &h7FFF     =  32767     integer
  357.  &h8000     = -32768     integer
  358.  &h8001     = -32767     integer
  359.  &hFFFF     = -1         integer
  360.  &h10000    =  65536     long integer
  361.  &h7FFFFFFF =  2147483647long integer
  362.  &h80000000 = -2147483648long integer
  363.  &hFFFFFFFF = -1         long integer
  364.  &h100000000= 4294967296 double
  365. If you want &h8000 to be treated as +32768 then
  366. follow the number with & and it will be treated
  367. as a long and thus positive e.g.
  368. &h8000&     = 32768       long integer
  369.  
  370.  
  371. .ib.Octal Constants;
  372.  
  373. Octal constants start with &O or &o or just
  374. simply &. and are followed by octal digits (0-
  375. 7). The number may be followed by a type
  376. specifier (%, !, & or #). The type of an un-
  377. terminated octal constant is determined by the
  378. same rules as for hexadecimal constants (see
  379. above).
  380.  
  381.  
  382. .ib.Binary Constants;
  383.  
  384. Binary constants start with &B or &b and are
  385. followed by the digits 0 or 1. The number may
  386. be followed by a type specifier (%,!,& or #).
  387. The type of an un-terminated binary constant is
  388. determined by the same rules as for hexadecimal
  389. constants described previously.
  390.  
  391.  
  392. .ib.Character constants;
  393.  
  394. These start like strings of only one character
  395. and are followed by the % character and have a
  396. value equivalent to the ASC() of the character.
  397. However they are generally easier to read and
  398. more efficient than the ASC() equivalent.
  399.  
  400.  
  401. Types of Constants
  402.  
  403. The rules regarding what type a constant is are
  404. rather complicated but in general you should
  405. find that normally the compiler does what you
  406. expect. The most common problem is that some
  407. hexadecimal constants are treated as negative.
  408. If this is a problem please see the Hexadecimal
  409. Constants section above. The following are in
  410. decreasing order of importance:
  411. 1. A terminating character is used. If the
  412. number is terminated by:
  413.  %   it is taken as an integer
  414.  &   it is taken as a long integer
  415.  !   as a single precision floating point
  416.  number
  417.  #   as a double precision floating point
  418.  number.
  419. 2. If the number is hexadecimal or octal and
  420. lies in the following range:
  421.  0 to &hFFFF      it is taken as an integer
  422.  &h10000 to &hFFFFFFFF   it is taken as a long
  423.  integer
  424.  &h100000000 upwards     it is taken as a
  425.  double.
  426. For the rules concerning whether a constant is
  427. treated as negative see above.
  428. 3. If the number is decimal and it is not a
  429. whole number and has more than 6 digits in the
  430. whole number and decimal parts, then it is a
  431. double.
  432. 4. If it has an exponent of the form D or d
  433. then it is a double.
  434. 5. If it has an exponent of the form E or e
  435. then it is an integer.
  436. 6. If it has a decimal point then it is a
  437. single precision number.
  438. 7. If it is a whole number less than or equal
  439. to 32767 it is an integer.
  440. 8. If it is a whole number less than or equal
  441. to 2147483647 it is a long integer.
  442. Examples:
  443. 1                  integer
  444. 1.0                single precision (.)
  445. 1.0E0              single precision(.
  446.                    and E)
  447. 1.00000            single precision
  448. 1.000000           double precision (7
  449.                    digits)
  450. 1.000000E0         single precision(E
  451.                    overrides 7 digits)
  452. 1D0                double precision (D)
  453. 1D0%               integer (% over-rides
  454.                    D)
  455. 1.0&               long integer (&
  456.                    overrides .)
  457. 1D0!               single precision (!
  458.                    overrides D)
  459. 1#                 double precision.
  460.  
  461.  
  462. Variables and Reserved Words
  463.  
  464.  
  465.  
  466. Variable names start with a letter and
  467. subsequent characters may be letters, digits,
  468. or full stops (.). In addition underlines (_)
  469. may be included if you haven´t switched this
  470. off using the NOUNDERLINES option. For maximum
  471. compatibility with other BASICs don´t use
  472. underlines or full stops.
  473. Lower and upper case are treated as the same in
  474. variable names and reserved words so that
  475. PRINT, Print and Print are all the same
  476. reserved word.
  477. Variables may be terminated with a type
  478. specifier % (integer), & (long integer), !
  479. (single precision floating point) or # (double
  480. precision floating point). If there is no type
  481. specifier then the type is determined by the
  482. current DEFtype statement for the first letter
  483. of the variable. If there have been no DEFtype
  484. statements then single precision (!) is used.
  485. Compiler error messages specifying variable
  486. names always include the type specifier that
  487. has been assumed.
  488. For example, the following gives the types of
  489. the respective variables:
  490.  
  491. DEFINT i-k
  492. DEFSTR s
  493. DEFDBL q-r
  494.  i%            integer
  495.  i             integer i% (same as
  496.                above)
  497.  I             (also the same as above)
  498.  i&            long integer (different)
  499.  str1          string (same as str1$)
  500.  real_value1   double (same as
  501.                real_value1#)
  502. You can not use reserved words as the names of
  503. variables or sub-programs. The reserved words
  504. are listed in full in Appendix C. Reserved
  505. words and variables may be entered in upper or
  506. lower case or a mixture of both.
  507. In general using reserved words with type
  508. specifiers should be avoided for compatibility
  509. reasons.
  510. GO is not a reserved word. However if GO is
  511. followed by TO or SUB then it is made into GOTO
  512. or GOSUB respectively; so you can have white
  513. space between GO and TO and it will still be
  514. treated as GOTO. Thus you can use GO as a
  515. variable name if you like but some strange
  516. things can happen, such as
  517.  
  518. FOR i=go TO from STEP 2
  519. is misunderstood because the compiler considers
  520. this to be
  521.  
  522. FOR I = GOTO  from STEP 2
  523.  
  524. Variables must not start with FN because they
  525. would be treated as function names. The same
  526. rules for determining the type of a variable
  527. are used to determine the types of functions.
  528. The following are FN function names:
  529. FNtest
  530. FNsine&
  531. FNget.one.character
  532. Sub-program names must not have a type
  533. specifier because they do not have a type. You
  534. may use the same name for a variable and a sub-
  535. program although the type specifier of the
  536. variable must be used explicitly. However, this
  537. can be very confusing. For example
  538.  
  539. SUB john
  540.  john%=42
  541. END SUB
  542.  
  543. john%=52 : john
  544.  
  545.  
  546. .ib.Arrays
  547.  
  548.  
  549.  
  550. ;
  551.  
  552. Array names follow the same rules as for
  553. variables and their types are determined in the
  554. same way. You may use the same name for an
  555. array and an ordinary variable. Normally an
  556. array name is followed by an open parenthesis,
  557. except in the ERASE statement and the UBOUND
  558. and LBOUND functions when this is assumed
  559. automatically.
  560. Arrays are tables of values each of the same
  561. type. Normally the number of elements in an
  562. array and the number of dimensions is specified
  563. with a DIM statement. There are no restrictions
  564. on the size of arrays other than available
  565. memory and subscripts may be long integer
  566. values if applicable. The maximum number of
  567. dimensions for an array is 31 (which would take
  568. up a minimum of 4 gigabytes of memory if each
  569. index had more than one element).
  570. If the DIM statement is not used, the maximum
  571. subscript is assumed to be 10. If you have
  572. switched off array checks using compiler option
  573. NOARRAY then you must use the DIM statement. To
  574. check whether you have inadvertently auto-
  575. dimensioned an array then you should use the
  576. Array Checks Warnings option - this will give a
  577. run-time error when an array is used before it
  578. has been DIMensioned.
  579. The minimum value of subscripts is 0 unless an
  580. OPTION BASE statement is used. When referenced,
  581. the element of the array to be accessed is
  582. specified by one or more expressions inside
  583. parentheses and separated by commas. The
  584. expressions may be of any numeric type although
  585. single and double precision real values will be
  586. converted to long integers.
  587. For example, given:
  588.  
  589. DIM A(30), B$(table_entries,4), table&(100000)
  590. DIM t%(fred*fred),c(n,n,n)
  591. then the following are valid array references:
  592.  
  593. A(i)
  594. B$(j*3,2)
  595. table&(i&)
  596. t%(k-l)
  597. c(i,j,k)
  598. By default MaxonBASIC arrays are static i.e.
  599. you cannot re-dimension them; this can be
  600. changed through the use of the REM $dynamic
  601. compiler option - for more information on this
  602. and other features of arrays see the Advanced
  603. Arrays section later in this chapter.
  604.  
  605.  
  606. .ib.Operators;
  607.  
  608. Expressions are made up of constants,
  609. variables, array variables, function calls and
  610. operators. The order of priority is listed
  611. below with the highest priority first:
  612.  1.  Exponentiation ( to the power of ) (^)
  613.  2.  Unary Minus (-)
  614.  3.  Multiplication (*) and Floating Point
  615.  Division (/)
  616.  4.  Integer Division (\)
  617.  5.  Modulus ( MOD)
  618.  6.  Addition (+) and Subtraction (-)
  619.  7.  Shift left (<<) and Shift right (>>)
  620.  8.  Comparisons (=,<>,>,<,>=,<=, ==)
  621.  9.  NOT
  622.  10. AND
  623.  11. OR and XOR (exclusive or)
  624.  12. EQV
  625.  13. IMP
  626. The only exception to this is that x^-y is
  627. evaluated as x^(-y).
  628. To change the order of evaluation use
  629. parentheses (round brackets).
  630. The guiding principle for the precision used
  631. when evaluating expressions is that the minimum
  632. precision is used that will ensure that
  633. accuracy is not lost.
  634. The exponentiation operator (^) always has its
  635. operands converted to either single or double
  636. precision floating point and returns a result
  637. of the same type. Single precision is used if
  638. the operands are either integer or single
  639. precision. If either operand is a long integer
  640. or is double precision then it is evaluated in
  641. double precision for accuracy. See 6-2 below.
  642. This operator uses logarithms to give its
  643. result and as such is slow and inaccurate if
  644. the second operand is a small integer.
  645. The multiplication, addition, subtraction and
  646. unary minus operators may have operands of any
  647. numeric type with the following table giving
  648. the result of the expressions:
  649.          integer  long   single  double
  650.      integer           integer           long
  651.  single  double
  652.   long   long     long   double  double
  653.   single single   double single  double
  654.   double double   double double  double
  655.     6-1 Type conversion for most operators
  656. Addition may be also used for strings when it
  657. means concatenation so that, for example:
  658.  
  659. "ABC" +"DEF" ="ABCDEF"
  660. Floating point division operands are always
  661. converted to single or double precision
  662. floating point numbers, the following table
  663. gives the result of the expression:
  664.          integer  long  single   double
  665.     integer           single           double
  666.  single  double
  667.     long     double    double          double
  668.  double
  669.     single  single    double           single
  670.  double
  671.     double  double    double           double
  672.  double
  673.   6-2 Type conversion for division operators
  674. The integer division operator \ uses long
  675. integer (32-bit) arithmetic unless both
  676. operands are integers in which case integer (16-
  677. bit) arithmetic is used.
  678. The comparison operators always return an
  679. integer value of -1 for true and 0 for false.
  680. The comparison is evaluated using the type
  681. given in 6-1 above for numeric types. Strings
  682. may also be compared.
  683. The comparison operators are
  684. =         equality
  685. <>        inequality
  686. >         greater than
  687. <         less than
  688. >=        greater or equals
  689. <=        less than or equals
  690. ==        almost equals (two equals signs)
  691. The ´almost-equals´ operator is a MaxonBASIC
  692. extension for single or double precision
  693. floating point comparisons and it is defined as
  694. follows:
  695.  
  696. x==y
  697. calculates
  698.  
  699. ABS(x-y) <= ABS(y * 1E-6)
  700. Thus == can be used to check for near equality
  701. even if a small number of rounding errors have
  702. been introduced. For integers and long integers
  703. the comparison is the same as equals and for
  704. strings the comparison is the same as equals
  705. except that lower case letters are treated as
  706. equal to their uppercase counterparts. For
  707. example:
  708.          2.0==2.0        is true
  709.          2.0==1.999999   is true
  710.          2.0==1.99999    is false
  711. A string is considered less than another if its
  712. first character that differs is lower in the
  713. ASCII set than the corresponding character in
  714. the first string. If the strings are the same
  715. until one string is exhausted then the shorter
  716. string is less. All the following examples are
  717. true:
  718. Fred"<"Hello"       because "F"<"H"
  719. "Frederick"<"Hello  because "F"<"H"
  720. "
  721. "fred">"Hello"      because "f">"H". The
  722.                     lower case letters
  723.                     come after the upper.
  724. "Frederick">"Fred"  because "Frederick"
  725.                     is longer
  726. The logical shift operators, <<,>> shift their
  727. first operand left or right respectively by the
  728. number of bits given as their second operand.
  729. The shift operations are unsigned and the first
  730. operand should be of either integer type - if
  731. you use a single or double this will be
  732. converted to a long integer. The resulting type
  733. is the same as the first operand after any
  734. conversion.
  735. Note that i<<1 is the same as i*2 except when
  736. would occur and that i>>1 is the same as i\2
  737. except when i is negative.
  738. For example:
  739.     1<<8           &h100      256
  740.     (-1)<<8        &hFF00     -256
  741.     1<<15          &h8000     -32768
  742.     &h1234<<4      &h2340     9024
  743.     &h12345678<<4  &h23456780 591751940
  744.     &h12345678>>4  &h1234567  305419896
  745. Note that the shift operators are not supported
  746. by Microsoft BASIC on the Macintosh or PC.
  747. All the logical operators NOT, AND, OR, XOR,
  748. EQV and IMP use long integer arithmetic (32-
  749. bit) unless both operands are integers in which
  750. case integer arithmetic is used. These
  751. operators work bitwise, with each bit affected
  752. as shown below.
  753.  
  754.   X    Y    NOT  AND OR   XOR  IMP  EQV
  755.   0    0    1    0   0    0    1    1
  756.   0    1    1    0   1    1    1    0
  757.   1    0    0    0   1    1    0    0
  758.   1    1    0    1   1    0    1    1
  759.  
  760. Although these operations work on the
  761. individual bits they have the same affect as
  762. the corresponding logical operators if you use
  763. -1 for TRUE and 0 for FALSE.
  764. Examples:
  765.   -1 OR -1      =-1
  766.   4 OR 3= 7     (100 and 011 in binary)
  767.   -1 XOR 0      =-1
  768.   8 AND 4       = 0          (1000 and 100 in
  769.   binary)
  770. These logical operators can be particularly
  771. useful when writing routines that interface
  772. directly to the operating system as many of the
  773. flag parameters are based on bits.
  774.  
  775.  
  776. Sub-programs and User Defined Functions
  777.  
  778. .ib.Sub-programs;
  779. .ib.Functions;
  780. Sub-programs and user defined functions are one
  781. of the most powerful features of many modern
  782. BASICs and MaxonBASIC takes these ideas even
  783. further.
  784. The idea of using a sub-program is to isolate
  785. part of the code of your program in a way that
  786. makes it easy to call and easy to ensure that
  787. it is not interfering with variables that are,
  788. logically, not to do with the sub-program´s
  789. code.
  790. The simplest definition of a sub-program is
  791. something like
  792.  
  793. SUB hello
  794.  PRINT "hello"
  795. END SUB
  796. The SUB statement defines the name of the sub-
  797. program that we are defining and the END SUB
  798. indicates that we have finished.
  799. Sub-program definitions may not contain other
  800. sub-program definitions.
  801. The hello sub-program can be called using
  802.  
  803. call hello
  804. or even just
  805.  
  806. hello
  807. and will print the word hello. You can call sub-
  808. programs before or after their declarations.
  809. So far this doesn´t give us anything that you
  810. can´t do with old-style BASIC GOSUBÉRETURN
  811. statements. However by passing parameters to
  812. sub-programs we can make the sub-program work
  813. on different variables or values.
  814.  
  815.  
  816. Variable Parameters
  817.  
  818.  
  819.  
  820. Sub-programs may take two different sorts of
  821. parameters, value and variable parameters. By
  822. default parameters are variable parameters and
  823. are passed by reference. This means that if the
  824. sub-program changes the value of the variable
  825. parameter, its value will be modified globally.
  826. For example:
  827.  
  828. SUB TimesTwo(v)
  829.  v=v*2
  830. END SUB
  831. If we call this using
  832.  
  833. INPUT " Enter a number";i
  834. CALL TimesTwo(i)
  835. PRINT i
  836. and enter the number 42, i will be modified and
  837. then twice this, 84, will be printed.
  838. The shortened form of the CALL statement above
  839. is:
  840.  
  841. TimesTwo i
  842. Note that the brackets are not used when
  843. omitting the CALL keyword.
  844. When using variable parameters, if you pass an
  845. expression rather than a variable of the
  846. required type then any modifications to the
  847. parameter are lost. In order for a variable
  848. parameter to be modified globally by the sub-
  849. program to which it is passed, the type of the
  850. passed variable must be the same as the type of
  851. the parameter and it must be a simple variable.
  852. If we changed the calling code to be:
  853.  
  854. INPUT i#
  855. TimesTwo i#
  856. PRINT i#
  857. then the variable i# would not be modified.
  858. You can pass array elements as variable
  859. parameters; this causes the subscripting
  860. expression to be calculated before the sub-
  861. program is called e.g.
  862.  
  863. TimesTwo a(3)
  864. would double the value of a(3). However this
  865. should be avoided if you are using ERASE and
  866. REDIM APPEND/PRESERVE inside sub-programs; see
  867. the Advanced Arrays section in this chapter for
  868. more information.
  869. If you want to call a sub-program that normally
  870. would modify the variable, but on this occasion
  871. you don´t want this to happen, then enclose the
  872. variable name in parentheses e.g.
  873.  
  874. TimesTwo (i)
  875. This forces the parameter to be passed by
  876. value. If you use a CALL statement instead you
  877. use the same method e.g.
  878.  
  879. CALL TimesTwo ((i))    ´passes i by value
  880. If you have more than one parameter for a sub-
  881. program they should be separated by commas in
  882. both the call and the definition.
  883. For example:
  884.  
  885. SUB Multiply(i,j,k)
  886.  k=i*j
  887.  
  888. END SUB
  889.  
  890. Multiply 2,3,i
  891. PRINT i
  892. This will print 6. Note that the i that is a
  893. parameter and used in the sub-program is an
  894. entirely different entity to the i in the main
  895. program.
  896.  
  897.  
  898. Value Parameters
  899.  
  900.  
  901. Parameters may also be called by value, which
  902. means that the parameter will not be modified
  903. by the sub-program or function.
  904. To indicate that a parameter is passed by value
  905. precede it in the definition with the keyword
  906. BYVAL. So the above example could be coded as:
  907.  
  908. SUB Multiply(BYVAL i, BYVAL j,k)
  909.  k=i*j
  910. END SUB
  911.  
  912. Multiply 2,3,i
  913. PRINT i
  914. Value parameters are more efficient than
  915. variable parameters and are a MaxonBASIC
  916. extension. In most other BASICs with sub-
  917. programs you must use variable parameters and
  918. enclose them in parentheses. This works fine
  919. unless you forget the brackets, when you can
  920. modify your main program variables by mistake.
  921. In general make a parameter a BYVAL parameter
  922. unless you want to return a value.
  923. By default, parameters to sub-programs are
  924. passed by reference, as variable parameters.
  925. For compatibility with earlier versions of
  926. MaxonBASIC you can use VAL instead of BYVAL; we
  927. do not recommend this for new programs as BYVAL
  928. is used by the latest Microsoft BASICs for the
  929. PC.
  930.  
  931.  
  932. STATIC variables
  933.  
  934.  
  935. i. Variables :STATIC;
  936. In the examples so far we have only used
  937. parameters inside sub-programs. However sub-
  938. programs may have their own variables. For
  939. example
  940.  
  941. SUB Sum(BYVAL n, k)
  942. STATIC count,total
  943.  total=0
  944.  FOR count=1 TO n
  945.      total=total+count
  946.  NEXT count
  947.  k=count
  948. END SUB
  949.  
  950. Sum 4,result
  951. PRINT result
  952. will print
  953.  
  954. 10
  955. which is 1+2+3+4. The word STATIC is used to
  956. introduce ordinary local variables. You can use
  957. commas to separate them. In fact if you omit
  958. the STATIC statement, the above will still work
  959. because STATIC is assumed by the compiler.
  960. However we recommend strongly that you use this
  961. statement together with the variable checks
  962. flag (V+). This will warn you if you misspell
  963. variables in sub-programs.
  964. For example if we had typed
  965.  
  966. k=k+cont
  967. in the example above, the compiler would
  968. complain that the variable cont was not
  969. declared.
  970. STATIC variables are zeroed when the program
  971. starts running but are not modified between
  972. different calls to the procedure. In the
  973. example above if we called the Sum sub-program
  974. again Total would have a value of 10 so we must
  975. zero it each time.
  976.  
  977.  
  978. SHARED variables
  979.  
  980.  
  981.  
  982.  
  983. You can also use variables from your main
  984. program inside sub-programs by using the SHARED
  985. statement. For example we could code the
  986. example above as:
  987.  
  988. SUB Sum(BYVAL n)
  989. STATIC count,total
  990. SHARED k
  991.  total=0
  992.  FOR count=1 TO n
  993.      total=total+count
  994.  NEXT count
  995.  k=count
  996. END SUB
  997.  
  998. Sum 4
  999. PRINT k
  1000. This is however less flexible than the original
  1001. example because it modifies only one particular
  1002. variable.
  1003. Using SHARED variables with variable parameters
  1004. which should be value parameters can lead to
  1005. the following difficult-to-spot bug shown
  1006. below:
  1007.  
  1008. SUB process(t)
  1009. SHARED token
  1010.  IF t=3 OR t=4 THEN
  1011.  .
  1012.  .
  1013.      token=5
  1014.  .
  1015.  .
  1016.      IF t=4 THEN ´ problem
  1017.  .
  1018.  .
  1019.      END IF
  1020.  END IF
  1021. One would naturally expect t to be 3 or 4 at
  1022. the point marked problem since t was 3 or 4 in
  1023. the previous IF statement. However if the sub-
  1024. program process was called as
  1025.  
  1026. process token
  1027. then this would not be the case because the
  1028. modification of token will also change t. This
  1029. can be solved by enclosing t in parentheses or,
  1030. even better, by making the parameter a value
  1031. parameter. This problem can also occur if the
  1032. variable token was modified by a sub-program
  1033. that is called inside process, which is even
  1034. more difficult to spot.
  1035. If you have some variables that are imported
  1036. into many sub-programs and you wish to avoid
  1037. having SHARED statements each time, you can use
  1038. the DIM SHARED statement which causes the
  1039. variable to be SHARED with every sub-program.
  1040. For example, if you have
  1041.  
  1042. DIM SHARED debug_flag
  1043. then you can use debug_flag anywhere in your
  1044. program.
  1045.  
  1046.  
  1047. Recursion and Local variables
  1048.  
  1049.  
  1050.  and
  1051.  
  1052. Sub-programs may be called recursively i.e.
  1053. they may call themselves.
  1054.  
  1055. SUB Fibonacci(BYVAL n, r)
  1056. LOCAL temp1,temp2
  1057.  
  1058.  SELECT CASE n
  1059.      CASE 0: r=0
  1060.      CASE 1: r=1
  1061.      CASE REMAINDER:
  1062.          Fibonacci n-1, temp1
  1063.          Fibonacci n-2, temp2
  1064.          r=temp1*temp2
  1065.  END SELECT
  1066.  
  1067. END SUB
  1068.  
  1069. FOR i=0 TO 15
  1070.  Fibonacci i,res
  1071.  PRINT res;
  1072. NEXT i
  1073. This prints the first few numbers in the
  1074. Fibonacci sequence, in which the nth term is
  1075. the sum of the two previous terms with the
  1076. sequence starting with 0, 1, ÉÉ. This is, in
  1077. fact not the most efficient way to code this
  1078. algorithm in MaxonBASIC; the algorithm can also
  1079. be improved very easily.
  1080. The above example also introduces LOCAL
  1081. variables. These are like STATIC variables in
  1082. that they cannot be accessed outside the sub-
  1083. program. However a new variable is created for
  1084. each invocation of the sub-program. This
  1085. becomes important when you have recursive
  1086. calls. In the example above if there was only
  1087. one variable temp1 then it would be corrupted
  1088. during the second recursive call. Try it and
  1089. see.
  1090. The memory for use of local scalar numeric
  1091. variables is allocated on the machine stack. If
  1092. you make heavy use of recursive calls with
  1093. large numbers of local variables it is possible
  1094. to run out of stack. Use the MINSTACK option,
  1095. see Chapter 4 for details.
  1096. Strings may also be used as parameters and
  1097. local variables in exactly the same way as
  1098. numbers. The only difference is that the actual
  1099. data in the strings is allocated on the heap
  1100. and not on the machine stack.
  1101.  
  1102.  
  1103. User-Defined Functions
  1104.  
  1105. .ib.User-Defined Functions;
  1106. .ib.Functions: User-Defined ;
  1107. As well as sub-programs you can also have user-
  1108. defined functions. In MaxonBASIC there are two
  1109. methods of defining user-defined functions, DEF
  1110. FN and FUNCTION. The latter is the preferred
  1111. modern form so we will discuss this first.
  1112.  
  1113.  
  1114. Using FUNCTION
  1115.  
  1116. The FUNCTION syntax makes user-defined
  1117. functions use the same syntax as for sub-
  1118. programs. User defined functions return results
  1119. by assigning to a pseudo-variable with the name
  1120. of the function.
  1121. For example, here´s another coding of the
  1122. Fibonacci example:
  1123.  
  1124. FUNCTION Fibonacci(BYVAL n)
  1125.  SELECT CASE n
  1126.      CASE 0: Fibonacci=0
  1127.      CASE 1: Fibonacci=1
  1128.      CASE REMAINDER:
  1129.          Fibonacci= Fibonacci(n-1)+Fibonacci(n-
  1130. 2)
  1131.  END SELECT
  1132. END FUNCTION
  1133.  
  1134. FOR i=0 TO 15
  1135.  PRINT Fibonacci (i);
  1136. NEXT i
  1137. This gives probably the neatest solution to
  1138. this classic problem.
  1139. There is no restriction on the name of the
  1140. function and the rules for parameters and local
  1141. variables are the same as for sub-programs.
  1142. FUNCTIONs must be declared before they are
  1143. used. The normal way to this is to ensure that
  1144. the FUNCTION END FUNCTION statements are before
  1145. any calls of the function. If you wish to use a
  1146. function before you define it then you can use
  1147. the .ib.DECLARE ;statement. This specifies the
  1148. parameters of a function in the same way as a
  1149. FUNCTION statement but does not actually
  1150. contain any code.
  1151. For example,
  1152.  
  1153. DECLARE FUNCTION Fibonacci(BYVAL n)
  1154.  
  1155. FOR i=0 TO 15
  1156.  PRINT Fibonacci (i);
  1157. NEXT i
  1158.  
  1159. FUNCTION Fibonacci(BYVAL n)
  1160.  SELECT CASE n
  1161.      CASE 0: Fibonacci=0
  1162.      CASE 1: Fibonacci=1
  1163.      CASE REMAINDER:
  1164.          Fibonacci= Fibonacci(n-1)+Fibonacci(n-
  1165. 2)
  1166.  END SELECT
  1167. END FUNCTION
  1168.  
  1169. FOR i=0 TO 15
  1170.  PRINT Fibonacci (i);
  1171. NEXT i
  1172. This is our final Fibonacci example
  1173. (honestly!). A couple of points to note:
  1174.  
  1175. · The form of the DECLARE statement is exactly
  1176. the same as the FUNCTION statement with the
  1177. word DECLARE at the front.
  1178.  
  1179. · DECLARE statements are needed for two reasons.
  1180. Firstly, they enable the compiler to check that
  1181. the correct number and type of parameters have
  1182. been used. Secondly if DECLARE was not used,
  1183. the compiler might think that Fibonacci(i) in
  1184. the example above was referring to an array
  1185. Fibonacci(). In fact probably the only
  1186. advantage of the FN syntax is that you can
  1187. instantly see the difference between a function
  1188. call and an array access. Of course, you could
  1189. easily decide to use the similar conventions
  1190. with FUNCTION definitions.
  1191.  
  1192. DECLARE statements can also be used for sub-
  1193. programs. They are not required by MaxonBASIC
  1194. but can be useful for compatibility with other
  1195. modern BASICs such as QuickBASIC. You can also
  1196. use them as a documentation aid by having
  1197. DECLARE statements at the front of your program
  1198. for all the sub-programs and functions.
  1199. If you call a function which performs
  1200. input/output inside another statement that
  1201. performs input/output strange things may
  1202. happen. There is no good reason for doing this
  1203. and it should be avoided.
  1204.  
  1205.  
  1206. Using the FN notation
  1207.  
  1208. In the past you could only define a function by
  1209. using FN as a prefix to the function name; old-
  1210. style BASICs restricted you even further in
  1211. that the function could only be one line long.
  1212. For example,
  1213.  
  1214. DEF FNrad(x)=x*3.141592653589793/180
  1215. which converts an angle in radians to degrees
  1216. and could be used as follows
  1217.  
  1218. PRINT SIN(FNrad(45))
  1219. to give the sine of 45 degrees. The names of
  1220. such user-defined functions must start with FN.
  1221. However, in MaxonBASIC, DEF FN functions may
  1222. have all the facilities of sub-programs with
  1223. the following differences:
  1224. User defined functions return results by
  1225. assigning to a pseudo-variable with the name of
  1226. the function. For example
  1227.  
  1228. DEF FNfactorial(n)
  1229.  IF n<=1 THEN
  1230.      FNfactorial=1
  1231.  ELSE
  1232.      FNfactorial=n*FNfactorial(n-1)
  1233.  END IF
  1234. END DEF
  1235. which calculates the famous factorial function.
  1236. Note that the definition finishes with END DEF
  1237. and that on the right hand side of the
  1238. assignment FNfactorial causes the function to
  1239. be called again recursively.
  1240. The big difference between user-defined FN
  1241. functions and sub-programs is that, in
  1242. functions, parameters are call-by-value by
  1243. default and to specify call-by-variable you
  1244. should precede them with VARPTR. If you do not
  1245. use variable checks, variables are assumed to
  1246. be SHARED rather than STATIC. Naturally this
  1247. difference can be confusing and so we recommend
  1248. using the FUNCTION syntax instead.
  1249. Here is yet another coding of the Fibonacci
  1250. example:
  1251.  
  1252. DEF FNfibonacci( n)
  1253.  SELECT CASE n
  1254.      CASE 0: FNfibonacci=0
  1255.      CASE 1: FNfibonacci=1
  1256.      CASE REMAINDER:
  1257.          FNfibonacci= FNfibonacci(n-
  1258. 1)+FNfibonacci(n-2)
  1259.  END SELECT
  1260. END DEF
  1261.  
  1262. FOR i=0 TO 15
  1263.  
  1264.  PRINT FNfibonacci (i);
  1265. NEXT i
  1266. Incidentally you can insert a space between the
  1267. FN and the function name.
  1268.  
  1269.  
  1270. Arrays and Sub-programs
  1271.  
  1272.  
  1273.  
  1274. Arrays may be used as parameters to sub-
  1275. programs and user-defined functions. They are
  1276. specified both in call statements and
  1277. definitions with open and close parentheses
  1278. after their names. The definition should
  1279. contain the number of dimensions of the array.
  1280. Arrays are always passed by reference.
  1281.  
  1282. DIM b(3,6)
  1283. MatSum b(),res
  1284.  
  1285. PRINT res
  1286.  
  1287. SUB MatSum(a(2),x)
  1288. STATIC i,j,x
  1289.  x=0
  1290. F    OR i=LBOUND(a,1) TO UBOUND(a,1)
  1291.      FOR j=LBOUND(a,2) TO UBOUND(a,2)
  1292.          x=x+a(i,j)
  1293.      NEXT j
  1294.  NEXT i
  1295.  
  1296. END SUB
  1297. This sums all the elements of the two
  1298. dimensional array.
  1299. The corresponding function definition would be:
  1300.  
  1301. DIM b(3,6)
  1302. PRINT fnMatSum( b())
  1303.  
  1304. DEF fnMatSum(a(2))
  1305. STATIC i,j,x
  1306.  x=0
  1307.  FOR i=LBOUND(a,1) TO UBOUND(a,1)
  1308.      FOR j=LBOUND(a,2) TO UBOUND(a,2)
  1309.          x=x+a(i,j)
  1310.      NEXT j
  1311.  NEXT i
  1312.  fnMatSum=x
  1313. END SUB
  1314. Sub-programs may share arrays with the main
  1315. program. The SHARED and DIM SHARED statements
  1316. may be used as for scalar variables. The DIM
  1317. SHARED statement when used with arrays also
  1318. dimensions them. The SHARED variablesstatement
  1319. should specify the number of dimensions of the
  1320. array although this is not enforced.
  1321. For example,
  1322.  
  1323. DIM SHARED table(100)
  1324. ´table() can now be access anywhere in the
  1325. program.
  1326. or alternatively
  1327.  
  1328. DIM table(10)
  1329. table(10)=42 : Silly
  1330.  
  1331. SUB Silly
  1332. SHARED table(1)
  1333.  PRINT table(10)
  1334. END SUB
  1335. This will print 42.
  1336.  
  1337.  
  1338. Local Arrays
  1339.  
  1340.  
  1341.  
  1342. Arrays may also be local to a sub-program and
  1343. both STATIC and LOCAL varieties are supported.
  1344. When using STATIC you need to make sure that
  1345. the array is not dimensioned more than once. In
  1346. the STATIC statement the number of dimensions
  1347. may be included in parentheses.
  1348. For example,
  1349.  
  1350. ´constants for Table Handler operations
  1351. CONST init=0, insert=1, find =2, replace=3
  1352.  
  1353. TableHandler init,0,0  ´initialise the table
  1354. SUB TableHandler(operation, index, value)
  1355. STATIC table(1), first_free
  1356.  
  1357.  SELECT CASE operation
  1358.      CASE init
  1359.          DIM table(100)
  1360.          first_free=0
  1361.  
  1362.      CASE insert
  1363.      .
  1364.      .
  1365. In this example the array will only be
  1366. dimensioned once, as long as the TableHandler
  1367. sub-program is not called with a parameter of
  1368. init more than once.
  1369. Using the LOCAL statement arrays may be created
  1370. for the duration of this call to the sub-
  1371. program. They are erased automatically at the
  1372. end of the call. The actual dimensions are
  1373. given in the LOCAL statement. For example:
  1374.  
  1375. SUB Recursive
  1376. ´a temporary array with elements up to
  1377. temp(40).
  1378. LOCAL temp(40)
  1379. .
  1380. .
  1381. END SUB
  1382.  
  1383.  
  1384. Advanced Arrays
  1385.  
  1386.  
  1387. As well as the DIM, SHARED, STATIC and LOCAL
  1388. statements described above there are a number
  1389. of other array facilities that are not
  1390. available in primitive BASICs.
  1391. The UBOUND and LBOUND functions return the size
  1392. of arrays. See the example MatSum previously.
  1393. The lower-bound of arrays created by the DIM
  1394. statement can be changed from the default value
  1395. of 0 to 1 by the OPTION BASE statement, for
  1396. example:
  1397.  
  1398. OPTION BASE 1 ´arrays now start at one.
  1399. OPTION BASE is an executable statement, and so
  1400. its effect depends on the order of execution in
  1401. the program, not the order of the program text.
  1402. It is thus possible for an array to have
  1403. different dimensions if it is ERASEd and then
  1404. REDIMmed. Using OPTION BASE normally only saves
  1405. a considerable amount of memory if you are
  1406. using 3 or more dimensions in an array.
  1407. When array checks are switched off OPTION BASE
  1408. statements are ignored.
  1409.  
  1410.  
  1411. Dynamic and static arrays
  1412.  
  1413.  
  1414.  
  1415. Normally arrays that are DIMensioned using a
  1416. constant value, for example DIM A(100) are
  1417. treated as static arrays. Static arrays have a
  1418. constant size specified by the DIM statement
  1419. and may not be re-dimensioned or erased which
  1420. means the compiler can generate code that will
  1421. access them very quickly. If you have an array
  1422. that you will subsequently want to re-dimension
  1423. or erase then you´ll need to use REM $dynamic
  1424. which causes the compiler to make all DIMension
  1425. statements declare dynamic arrays from this
  1426. point in your program.
  1427. For example:
  1428.  
  1429. REM $dynamic
  1430. CONST arrsize=200
  1431. DIM arr(arrsize)
  1432.  
  1433. REDIM arrsize(300)
  1434.  
  1435. You can revert to the normal behaviour by using
  1436. REM $static.
  1437. Dynamic arrays can grow and shrink in size and
  1438. therefore MaxonBASIC has to generate more code
  1439. to handle this possible behaviour - if you want
  1440. arrays to be accessed as quickly as possible
  1441. using the minimum amount of code, use static
  1442. arrays, which is the default. Static arrays are
  1443. stored in the program´s global space rather
  1444. than on the heap.
  1445. Note that you must have used REM $DYNAMIC
  1446. before you DIM the array for the first time;
  1447. you cannot change the type of an array after it
  1448. is declared.
  1449.  
  1450.  
  1451. Other array features
  1452.  
  1453. ERASE works in two different ways, depending on
  1454. the type of the array (dynamic or static). For
  1455. static arrays, ERASE simply clears all the
  1456. elements to 0 or to the null string. For
  1457. dynamic arrays, ERASE may be used to free the
  1458. space used by the array when it is no longer
  1459. required. This is particularly useful if you
  1460. have temporary results stored in an array. Once
  1461. a dynamic array has been ERASEd you can DIM it
  1462. again. For example,
  1463.  
  1464. REM $dynamic
  1465. DIM temp(10000) ´ 10000 temporary results
  1466. .
  1467. .
  1468. ERASE temp
  1469.  
  1470. ´ note that temp is not followed by parentheses.
  1471. This
  1472. ´ anomaly is present for compatibility with
  1473. other BASICs
  1474. The REDIM statement for dynamic arrays gives
  1475. the equivalent of an ERASE followed by a DIM in
  1476. one statement. Thus
  1477.  
  1478. REDIM temp(100)
  1479. is equivalent to
  1480.  
  1481. ERASE temp: DIM temp(100)
  1482. REDIM cannot be used for static arrays.
  1483. MaxonBASIC has a powerful extension to let you
  1484. change the size of dynamic arrays whilst
  1485. retaining their data called REDIM PRESERVE.
  1486. For example:
  1487.  
  1488. REM $dynamic
  1489.  
  1490. SUB AddElement(value)
  1491.  
  1492. SHARED table(1), maxentries, nextentry
  1493.  
  1494.  IF nextentry> maxentries THEN
  1495.  ´ no room for this entry
  1496.      maxentries=maxentries+100
  1497.      REDIM PRESERVE table(maxentries)
  1498.  ´ the above makes the array 100 elements
  1499. larger
  1500.  END IF
  1501.  
  1502.  table(nextentry)=value  ´enter the value
  1503.  nextentry=nextentry+1 ´ready to store the next
  1504. one
  1505.  
  1506. END SUB
  1507. This example shows how you can avoid fixed
  1508. limits on the sizes of dynamic arrays. If you
  1509. run out of room just make it bigger. REDIM
  1510. PRESERVE requires enough memory to make a copy
  1511. of the array. You can also use REDIM PRESERVE
  1512. to make dynamic arrays smaller; again a copy of
  1513. the array is made.
  1514. Normally the ERASE, REDIM and REDIM PRESERVE
  1515. statements cause dynamic arrays to be moved in
  1516. memory. As a result, if there are any pending
  1517. array elements that have been used in variable
  1518. parameters, then these will be become invalid.
  1519. The best way to avoid this is by not passing
  1520. array elements by reference. For example the
  1521. following may not work as intended:
  1522.  
  1523. REM $dynamic
  1524. DIM x(50), a(30)
  1525. .
  1526. .
  1527. Subprog a(3) ´ note variable parameter.
  1528. .
  1529. SUB Subprog(b)   ´ note variable parameter
  1530.  
  1531.  ERASE x
  1532.      ´ a() will now become corrupt, it has
  1533.      ´ been moved because it was declared
  1534.      ´ after x() which has been erased
  1535.  
  1536. .
  1537. .
  1538. END SUB
  1539. Unlike many BASIC compilers, MaxonBASIC will
  1540. let you change the number of dimensions of
  1541. dynamic arrays with a REDIM statement. This may
  1542. prove useful when porting certain programs that
  1543. were developed with interpreters, however we
  1544. recommend strongly that you avoid this as it
  1545. can make programs almost un-maintainable.
  1546. For compatibility with earlier versions of
  1547. MaxonBASIC, you can use REDIM APPEND instead of
  1548. REDIM PRESERVE. However we recommend the use of
  1549. the latter as it is the form used by Microsoft
  1550. BASIC 7.1 on the PC.
  1551.  
  1552.  
  1553. Limitations Imposed by the Compiler
  1554.  
  1555.  
  1556. We have tried to avoid placing limits on the
  1557. programs you can write. For example, most
  1558. compilers have a limit on the number of
  1559. characters that are significant in an
  1560. identifier; MaxonBASIC does not impose any
  1561. limit on this so that
  1562. A_very_long_identifier_indeed_which_goes_on_and
  1563. _on
  1564. is different from
  1565. A_very_long_identifier_indeed_which_goes_on_and
  1566. _on_and_is_different
  1567. This sort of limitation may not seem important
  1568. to you, but such possible restrictions have the
  1569. annoying habit of appearing when you think you
  1570. have nearly finished a large program.
  1571. This section intends to list the remaining
  1572. limitations other than the total workspace of
  1573. the compiler. If you find these restrictive
  1574. please tell us.
  1575. A program may not have more than 16383 lines.
  1576. If you hit this limit you can probably get
  1577. round it by having more than one statement per
  1578. line. We have a 5000 line program which is
  1579. about 135K bytes of source. If you exceed this
  1580. you will be given a Too many lines in program
  1581. compiler error.
  1582. The total number of active labels in the code
  1583. generation phase of the compiler, as specified
  1584. by the Label Table option (LABEL) may not
  1585. exceed 5641. A label is generated for each line
  1586. number or label that is referenced (not for
  1587. those that are un-used) together with 2-3 or
  1588. each sub-program, 2 for each CASE in SELECT
  1589. statements plus 2 for the SELECT itself and 2
  1590. for most structure statements. For example our
  1591. 5000 line program requires about 1100 such
  1592. labels.
  1593. There is a limit of approximately 8000 on the
  1594. number of sub-programs, shared and local
  1595. variables and parameters in the entire program;
  1596. if you exceed this you will be given a
  1597. Structure Table Full error message.
  1598. The total number of different names in your
  1599. program may not exceed 14000.
  1600. The total code of a SELECT statement may not
  1601. exceed 32k bytes. To avoid this make some of
  1602. the alternatives into sub-programs.
  1603. The total size of some FORÉNEXT loops may not
  1604. exceed 32k bytes. To avoid this make some or
  1605. all of the loop into a sub-program.
  1606. Sub-programs and user defined functions may not
  1607. have more than 128 parameters.
  1608. The total space for global and STATIC local
  1609. variables and the descriptor table may not
  1610. exceed approximately 29K bytes. The amount of
  1611. storage, in bytes, in this area required for
  1612. the different types is:
  1613.   2   integers
  1614.   4   long integers, single precision numbers
  1615.   8   strings, double-precision numbers, all
  1616.   arrays
  1617. The data in strings and arrays is not stored in
  1618. this area. Static arrays are stored at the end
  1619. of this static array but they are not subject
  1620. to this 29K restriction.
  1621. Local variable stack space may not be more than
  1622. 32k bytes per invocation. The different types
  1623. require the number of bytes given in the table
  1624. above.
  1625. There are limits of 255 channels, 14 user
  1626. libraries, 4 screens opened via the SCREEN
  1627. statement and 12 windows opened via the WINDOW
  1628. statement.
  1629. ON GOTO and ON GOSUB statements may not have
  1630. more than 8190 line numbers each (!)
  1631.  
  1632.  
  1633.