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 Pascal / The Pascal Language < prev   
Encoding:
Text File  |  1993-03-13  |  10.9 KB  |  328 lines  |  [TEXT/AxoC]

  1.  
  2. AxoCalculator's Pascal 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. •    Procedures, Functions and Programs
  14. •    Declaring a Procedure
  15. •    Declaring a Function
  16. •    Adding a Program to the Calculator Menu
  17. •    Auto-loading Programs
  18.  
  19. Introduction
  20.  
  21. AxoCalculator implements several different programming languages. This document describes its implementation of the Pascal language. It is a simplified subset of true Pascal. Some of the strict requirements of Pascal have been relaxed to make programming AxoCalculator easier. Pascal requires that all variables be declared and that all statements end with a semicolon. It also defines the assignment operator as ' := '. These requirements make it laborious to perform simple calculations. For example, to set two variables, a and b, to 10 and 20 respectively, add them together, then output the result you would need to type the following using real Pascal :
  22.  
  23. var
  24.     a, b : Real;
  25. begin
  26.     a := 10;
  27.     b := 20;
  28.     writeLn (a+b);
  29. end;
  30.  
  31.  
  32.  
  33. In contrast, AxoCalculator's Pascal language does not require variables to be declared, ignores semicolons, accepts both ' = ' and ' := ' as assignment operators, and outputs the result of any expression which lacks an assignment operator. This means you only have to type :
  34.  
  35. a = 10
  36. b = 20
  37. a + b
  38.  
  39.  
  40. AxoCalculator can execute both of the above programs. First choose "Settings…" under the "Calculator" menu, and make sure the programming language is set to Pascal. To execute one or more lines of code, select (highlight) them using the mouse, then press the "enter" key.
  41.  
  42.  
  43. Strings
  44.  
  45. Character strings are enclosed in single quotes (e.g. 'This is a string'). To include a quote, tab or return in a character string, use 
  46.     \'   for a quote,  
  47.     \t  for a tab 
  48.     \r  or \n for a return. 
  49.  
  50. String variables can be created in three ways. The simplest is to assign a string to a previously unused variable name. For example,
  51.  
  52. aString = "Hello world"
  53.  
  54. As an alternative, use the "NewString" procedure.
  55.  
  56. NewString (bString)
  57. bString="abcde"
  58.  
  59. The third approach is to declare a string variable (Str255) at the start of a procedure or program (see "Variable Declaration" below). 
  60.  
  61.  
  62.  
  63. Strings can be appended to one another using the "Concat" function. 
  64.  
  65. NewString (cString)
  66. cString = concat (aString, "  ", bString)
  67.  
  68. 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. 
  69.  
  70. strLen = bString(0)
  71. char3 = bString(3)
  72. Print ("\nLength = ",strLen, "   ASCII code of 3rd char = ",char3)
  73.  
  74.  
  75. Comments and Continuation Lines
  76.  
  77. Comments are enclosed in curly brackets { a comment }. 
  78. An ampersand ( & ) as the first character of a line indicates a continuation line.
  79.  
  80. For example, select the following 6 lines then press "enter".
  81.  
  82. { Demonstrate comments, continuation lines and character strings }
  83. writeLn ('\rCell B\'s dose-response data\rDose\tResponse\r',
  84. &             10,'    \t',2.3,'\r',
  85. &            30,'    \t',5.5,'\r',
  86. &             100,'   \t',10.4,'\r',
  87. &             300,'   \t',11.0)
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. Dialogs
  100.  
  101. AxoCalculator programs can interact with the user via standard dialogs. This is done using two built in procedure calls, "Alert" and "PoseDialog". These procedures are described in the "Built in Procedures" 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 :
  102.  
  103.  
  104. PoseDialog ('\r Calculate the volume and surface area of a sphere', 
  105. &                        'Radius of sphere ',radius)
  106. theSA = 4 * pi * radius ^ 2
  107. theVolume = (4 / 3) * pi * radius ^ 3
  108. Alert ('The volume and surface area of a \r sphere with radius ', radius,
  109. &                    ' are : \r Volume = ',theVolume,
  110. &                              '\r Surface area = ',theSA)
  111.  
  112.  
  113. Conditional Branch
  114.  
  115. AxoCalculator supports the standard "if   then   else" statement for conditional branching. A program demonstrating this statement follows :
  116.  
  117. {  This program finds the square root of a number entered by the user  }
  118. PoseDialog ('\r Find the square root of a number', 'Enter the number', a)
  119. if a < 0 then
  120.     Alert ('Can\'t calculate the square root of a negative number : ',a)
  121. else
  122. begin
  123.     b = sqrt (a)
  124.     Alert ('\r The square root of ',a,'is ',b)
  125. end
  126.  
  127.  
  128. Note : because the "else" condition required two lines to be executed,
  129.         these lines are bracketed between a "begin" and an "end" statement.
  130.  
  131.  
  132.  
  133. AxoCalculator's Pascal language relaxes the use of semi-colons after every statement. This makes writing simple programs easier, but can lead to ambiguities with "if   then   else" statements. Do not use the following styles,
  134.  
  135. A) "if  then  else" all on the same line
  136. if (condition1) then action1 else action2
  137.  
  138. Instead use,
  139. if (condition1) then action1 
  140. else action2
  141.  
  142. or,
  143. if (condition1) then 
  144.     action1 
  145. else 
  146.     action2
  147.  
  148. B) "else if"  extending over more than one line
  149. if (condition1) then 
  150.     action1
  151. else if (condition2) then 
  152.     action2
  153. else
  154.     action3
  155.  
  156. Instead use,
  157. if (condition1) then 
  158.     action1
  159. else 
  160. begin
  161.     if (condition2) then 
  162.         action2
  163.     else
  164.         action3
  165. end
  166.  
  167.  
  168.  
  169.  
  170. Loop Commands
  171. AxoCalculator supports several commands for executing a group of statements multiple times. These loop commands are "For", "While" and "Repeat". Executable programs demonstrating each of these commands follow. 
  172.  
  173. Note : To interrupt a running program (for example to escape from
  174.           an infinite loop) press the "esc" key, or the Cmd-period 
  175.           key combination.
  176.  
  177. •    The "For" command
  178.  
  179. For i = 1 to 5 do
  180. begin
  181.     a = i * 5
  182.     b = sin (a)
  183.     WriteLn ('sin ( ',a,') = ',b)
  184. end
  185.  
  186. • The "While" command
  187.  
  188. a = 6
  189. While a > 0 do
  190. begin
  191.     a = a - 1
  192.     b = exp (a)
  193.     WriteLn ('exp ( ',a,') = ',b)
  194. end
  195.  
  196. • The "Repeat  Until" command
  197.  
  198. a = 0
  199. b = 0
  200. Repeat
  201.     a = a + 1
  202.     b = b + 2
  203.     c = b ^ a
  204.     WriteLn (b,'^ ',a,' = ',c)
  205. Until a > 5
  206.  
  207. These loop commands can also be used to execute a single line expression multiple times. For example :
  208.  
  209. For i = 1 to 20 do write (i)
  210.  
  211. Variable Declaration
  212.  
  213. Variables created without being declared (as in the above examples) are always "Real" (i.e. floating point). To work with Integer, Boolean or Array variables, they must be declared in standard Pascal fashion. Variable declaration begins with the reserved word, "Var". For example :
  214.  
  215. Var
  216.     i : Integer
  217.     b : Boolean
  218.     r : Real
  219.     anArr : Array[1..50]
  220.     aStr:    Str255
  221.     bStr:    String
  222.  
  223. Each variable name is followed by a colon, then the type of the variable. For array variables, the size of the array is also specified. AxoCalculator only supports an array low bound of 1, and accepts array declarations without a low bound (e.g. Array[50] ). Array elements are always Real. Arrays can be created anywhere in a program using the "NewArray" procedure,
  224.  
  225.     NewArray (anArray, arraySize)
  226.  
  227. Variables may be declared as either global (available to all programs and procedures) or local (available only to the currently active procedure). Global variables are preserved until they are explicitly unloaded. Local variables, which are declared within a procedure, are automatically unloaded when the procedure finishes executing. Local variables are declared immediately after a "Procedure", "Function" or "Program" declaration line (see below), and before the first "begin" statement. Global variables are declared outside (usually before) any "Procedure", "Function" or "Program" declarations. Examples of both local and global variable declaration can be found in the "Example Programs" document.
  228.  
  229. Procedures, Functions and Programs
  230.  
  231. AxoCalculator's programming language can be extended by loading user defined Procedures, Functions and Programs. These all have the same basic form : 
  232.  
  233. •  a declaration line which includes a name and lists any parameters 
  234. •  an optional local variable declaration section
  235. •  a program section bracketed between "begin" and "end" statements. 
  236.  
  237. Parameters are always passed by value.
  238. Variable parameters are not supported.
  239. Programs can not have any parameters. 
  240.  
  241. A Procedure, 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 procedure declaration, and may therefore produce a clearer error message.
  242.  
  243. To run a Procedure, Function or Program, type in its name followed by any parameters. A Function may be executed as part of a numerical expression. Procedures and Functions may call other Procedures and Functions. 
  244.  
  245.  
  246. Declaring a Procedure
  247.  
  248. Here is an example of how to declare a simple Procedure with a single parameter. This procedure 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".
  249.  
  250. procedure CountTo (Number)
  251. begin
  252.     For j = 1 to Number do Write (j)
  253.     WriteLn
  254. end
  255.  
  256.  
  257. Declaring a Function
  258.  
  259. Here is an example of how do declare a simple Function with a single parameter. A function returns a numerical result, and the type of the result (Real, Integer or Boolean) is specified following the function's parameter list. Load this example function as above. 
  260.  
  261. function Factorial (n) : Real
  262. Var
  263.     f : Real
  264.     j : Integer
  265. begin
  266.     f = 1
  267.     For j = 1 to n do f = f * j
  268.     Factorial = f
  269. end
  270.  
  271.  
  272. Here are two examples of how to use the "Factorial" function.
  273.  
  274. Factorial(10)
  275.  
  276. writeLn ('The natural log of factorial 5 = ', ln( Factorial(5)) )
  277.  
  278.  
  279. One practical use for functions is for unit conversion. Here is an example function which converts inches to centimeters.
  280.  
  281. function inchToCm (inch) : Real
  282. begin
  283.     inchToCm = inch * 2.54
  284. end
  285.  
  286.  
  287. Note: A general unit conversion function is provided in the document
  288.         "Unit Conversions" in the "AxoCalculator AutoLoad" folder.
  289.  
  290.  
  291.  
  292. Adding a Program to the Calculator Menu
  293.  
  294. 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. 
  295.  
  296. program CountDown 'Count Down to Lift Off/9'
  297. begin
  298.     WriteLn
  299.     WriteLn ('Prepare for count down.')
  300.     FlushOutput
  301. { Pause }
  302.     For k = 1 to 50 do a = exp(2.0)
  303. { Start the count down }
  304.     j = 10
  305.     While j >= 0 do
  306.     begin
  307.         if (j = 3) then 
  308.             WriteLn ('Ignition')
  309.         if (j <> 0) then 
  310.             WriteLn (j)
  311.         else
  312.             WriteLn ('Lift Off !!')
  313.         FlushOutput
  314.         Beep
  315.         j = j - 1
  316.     { Slow down the count }
  317.         For k = 1 to 20 do a = exp(2.0)
  318.     end
  319. end
  320.  
  321.  
  322.  
  323.  
  324.  
  325. Auto-loading a Program
  326.  
  327. 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. 
  328.