home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / A / AxoCalculator Package / AxoCalculator Documentation / Programming in Basic / The Basic Language < prev   
Encoding:
Text File  |  1993-03-13  |  8.1 KB  |  273 lines  |  [TEXT/AxoC]

  1.  
  2. AxoCalculator's Basic Programming Language
  3.  
  4. Contents
  5.  
  6. •    Introduction
  7. •    Strings
  8. •    Comments and Continuation Lines
  9. •    Dialogs
  10. •    Conditional Branch
  11. •    Loop Commands
  12. •    Variable Declaration
  13. •    Subroutines, Functions and Programs
  14. •    Declaring a Subroutine
  15. •    Declaring a Function
  16. •    Adding a Program to the Calculator Menu
  17. •    Auto-loading Programs
  18.  
  19. Introduction
  20.  
  21. AxoCalculator implements several programming languages. This document describes its implementation of the Basic language. It is a simplified subset of Microsoft QuickBASIC. One major omission is that statement labels and the "go to" command are not supported. All conditional branches and loops must be written using the modern block structured Basic commands :
  22.  
  23.     if - then - else - end if
  24.     for  -  next
  25.     while - wend
  26.     do - loop until
  27.  
  28. Another non-standard feature is that AxoCalculator outputs the result of any expression which lacks an assignment operator (=). To assign values to two variables, add them and output the result use the following three lines : 
  29.  
  30. a = 10
  31. b = 20
  32. a + b
  33.  
  34.  
  35. To execute the above program, first choose "Settings…" under the "Calculator" menu, and make sure the programming language is set to Basic. Select (highlight) the above three lines using the mouse, then press the "enter" key.
  36.  
  37.  
  38. Strings
  39.  
  40. Character strings are enclosed in double quotes (e.g. "This is a string"). To include a double quote, tab or return in a character string, use 
  41.     \"    for a double quote,  
  42.     \t    for a tab 
  43.     \r or \n for a return. 
  44.  
  45. String variable names do not need a "$" suffix. They can be created in two ways. The simplest is to assign a string to a previously unused variable name. For example,
  46.  
  47. aString = "Hello world"
  48.  
  49. As an alternative, use the "NewString" procedure.
  50.  
  51. NewString (bString)
  52. bString="abcde"
  53.  
  54. Strings can be appended to one another using the "Concat" function. 
  55.  
  56. cString = concat (aString, "  ", bString)
  57.  
  58. Individual characters in a string can be accessed and manipulated. A string variable behaves like a 255 element array. Each element is one character. Accessing an element returns the ASCII code of that character. The length of the string is stored in the zero indexed element. 
  59.  
  60. strLen = bString(0)
  61. char3 = bString(3)
  62. Print ("\nLength = ",strLen, "   ASCII code of 3rd char = ",char3)
  63.  
  64.  
  65. Comments and Continuation Lines
  66.  
  67. Comments are permitted, following a single quote ( ' ) as the first character in a line. An ampersand ( & ) as the first character of a line indicates a continuation line.
  68.  
  69. For example, select the following 6 lines then press "enter".
  70.  
  71. '    Demonstrate comments, continuation lines and character strings
  72. print  "\rCell B's dose-response data\rDose\tResponse\r",
  73. &             10,"    \t",2.3,"\r",
  74. &             30,"    \t",5.5,"\r",
  75. &             100,"   \t",10.4,"\r",
  76. &             300,"   \t",11.0
  77.  
  78.  
  79.  
  80. Dialogs
  81.  
  82. AxoCalculator programs can interact with the user via standard dialogs. This is done using two built in subroutine calls, "Alert" and "PoseDialog". These subroutines are described in the "Procedures and Functions" document. "Alert" is used for simple messages or for returning results. "PoseDialog" is used for requesting one or more numerical values. An example program follows :
  83.  
  84.  
  85. PoseDialog ("\r Calculate the volume and surface area of a sphere", 
  86. &                        "Radius of sphere ",radius)
  87. theSA = 4 * pi * radius ^ 2
  88. theVolume = (4 / 3) * pi * radius ^ 3
  89. Alert ("The volume and surface area of a \r sphere with radius ", radius,
  90. &                    " are : \r Volume = ",theVolume,
  91. &                              "\r Surface area = ",theSA)
  92.  
  93.  
  94.  
  95.  
  96.  
  97. Conditional Branch
  98.  
  99. AxoCalculator supports the standard "if   then   else  end if" statement for conditional branching. A program demonstrating this statement follows :
  100.  
  101. '   This program finds the square root of a number entered by the user
  102. PoseDialog ("\r Find the square root of a number", "Enter the number", a)
  103. if (a < 0) then
  104.     Alert ("Can't calculate the square root of a negative number : ",a)
  105. else
  106.     b = sqrt (a)
  107.     Alert ("\r The square root of ",a,"is ",b)
  108. end if
  109.  
  110.  
  111.  
  112. Loop Commands
  113.  
  114. AxoCalculator supports three commands for executing a group of statements multiple times. These loop commands are "For - Next", "While - Wend", and "Do - Loop Until". Executable programs demonstrating each of these commands follow. 
  115.  
  116. Note : To interrupt a running program (for example to escape from
  117.           an infinite loop) press the "esc" key, or the Cmd-period 
  118.           key combination.
  119.  
  120. •    The "For" command
  121.  
  122. For i = 1 to 5 
  123.     a = i * 5
  124.     b = sin (a)
  125.     Print "sin ( ",a,") = ",b 
  126. Next i
  127.  
  128.  
  129.  
  130.  
  131.  
  132. • The "While" command
  133.  
  134. a = 5
  135. While (a >= 0)
  136.     b = exp (a)
  137.     Print "exp ( ",a,") = ",b
  138.     a = a - 1
  139. Wend
  140.  
  141.  
  142. • The "Do" command
  143.  
  144. a = 0
  145. b = 0
  146. Do
  147.     a = a + 1
  148.     b = b + 2
  149.     c = b ^ a
  150.     Print b,"^ ",a," = ",c
  151. Loop Until a > 5
  152.  
  153.  
  154. Array Variables
  155.  
  156. Array variables can be created in two ways. They can be declared using the "DIM" statement at the start of a procedure or program,
  157.  
  158.     DIM anArr(50)
  159.  
  160. or they can be created anywhere using the "NewArray" procedure,
  161.  
  162.     NewArray (anArray, arraySize)
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. Subroutines, Functions and Programs
  172.  
  173. AxoCalculator's programming language can be extended by loading user defined Subs, Functions and Programs. These all have the same basic form : 
  174.  
  175. •  a declaration line which includes a name and lists any parameters 
  176. •  an optional local variable declaration section
  177. •  a program section terminating with an "end" statement. 
  178.  
  179. Note : Parameters are always passed by value.
  180. Variable parameters are not supported. 
  181. Programs can not have any parameters. 
  182.  
  183. A Sub, Function or Program can be loaded by selecting it's text, then pressing "enter" or choosing "Load" under the "Calculator" menu. The only difference is that "Load" will not attempt to execute any lines of code outside the subroutine declaration, and may therefore produce a clearer error message.
  184.  
  185. To run a Sub, Function or Program, type in its name followed by any parameters. A Function may be executed as part of a numerical expression. Subs and Functions may call other Subs and Functions. 
  186.  
  187.  
  188.  
  189. Declaring a Subroutine
  190.  
  191. Here is an example of how do declare a simple subroutine with a single parameter. This subroutine has no local variable declaration section. To load it, select the following 5 lines, and press "enter". To run it, type "CountTo(10)" then "enter".
  192.  
  193. Sub CountTo (Number)
  194. For j = 1 to Number 
  195.     Print j
  196. Next j
  197. end
  198.  
  199.  
  200. Declaring a Function
  201.  
  202. Here is an example of how do declare a simple Function with a single parameter. A function returns a numerical result. Load this example function as above. 
  203.  
  204. function Factorial (n)
  205. f = 1
  206. For j = 1 to n
  207.     f = f * j
  208. Next j
  209. Factorial = f
  210. end
  211.  
  212.  
  213.  
  214. Here are two examples of how to use the "Factorial" function.
  215.  
  216. Factorial(10)
  217.  
  218. Print "The natural log of factorial 5 = ", ln( Factorial(5)) 
  219.  
  220.  
  221. One practical use for functions is for unit conversion. Here is and example function which converts inches to centimeters.
  222.  
  223. function inchToCm (inch)
  224. inchToCm = inch * 2.54
  225. end
  226.  
  227. inchToCm(10)
  228.  
  229.  
  230. Note: A general unit conversion function is provided in the document
  231.         "Unit Conversions" in the "AxoCalculator AutoLoad" folder.
  232.  
  233.  
  234.  
  235.  
  236.  
  237. Adding a Program to the Calculator Menu
  238.  
  239. Here is an example of how do declare a simple program. Note that a program has no parameter list, and its name is followed by an optional character string. If present, this string is appended to the calculator menu when the program is loaded. If the second last character of the string is a slash ( / ) then the last character becomes a command key equivalent for running the program. Load the program as above. To run it, type "CountDown" then "enter", or select "Count Down to Lift Off" from the "Calculator" menu. 
  240.  
  241. program CountDown "Count Down to Lift Off/9"
  242.     Print ""
  243.     Print "Prepare for count down."
  244.     FlushOutput
  245. ' • Pause •
  246.     For k = 1 to 50
  247.         a = exp(2.0)
  248.     Next k
  249. ' • Start the Count Down •
  250.     For m = 0 to 10
  251.         j = 10 - m
  252.         if (j = 3) Print "Ignition."
  253.         if (j = 0) then
  254.             Print "Lift Off !!"
  255.         else
  256.             Print j
  257.         endif
  258.         FlushOutput
  259.         Beep
  260.         j = j - 1
  261.  
  262. ' • Slow down the count •
  263.         For k = 1 to 20
  264.             a = exp(2.0)
  265.         next k
  266.     next m
  267. end
  268.  
  269.  
  270. Auto-loading a Program
  271.  
  272. A useful program can be automatically loaded every time AxoCalculator is started up. To auto-load one or more programs, simply place them in the folder "AxoCalculator AutoLoad". This folder must be located in the same folder as the AxoCalculator program. 
  273.