MacTech | Volume Number: 4 | Issue Number: 9 | Column Tag: HyperChat® Comparing HyperTalk to Pascal By Paul F. Merrill, Brigham Young University, Provo, Utah Pascal and HyperTalk: A Comparison Paul F. Merrill Brigham Young University [Paul F. Merrill is a professor of instructional science and computer based education at BYU. He teaches courses in Pascal and computer based authoring systems. He has also written a book entitled, Computers in Education published by Prentice-Hall] Introduction HyperCard has produced a lot of excitement and discussion in the Macintosh community. It has generated a large following and no small number of critics. Many proponents have argued that HyperCard enables the weekend programmer to create sophisticated applications which tap the power of the Macintosh user interface without having to digest the formidable tool box and “Inside Macintosh.” John Sculley has predicted that it will have an impact in the Macintosh world similar to the impact of Applesoft Basic in the Apple II world. But just how powerful is the HyperCard authoring environment and its associated programming language, HyperTalk? One way to address that question is to compare HyperTalk with some standard language. Therefore, the purpose of this article is to compare HyperTalk with the Pascal programming language. This comparison will not only reveal the power of HyperTalk, but it will also provide a bridge for the Pascal programmer to cross over into the new exciting world of HyperCard. This article is neither a tutorial in Pascal or HyperTalk. It is assumed that the reader has some familiarity with Pascal and has at least seen a demonstration of or read a good review article on HyperCard. The following comparisons will be done by annotated examples rather than by complicated syntax diagrams. Since one or two examples cannot show the full range of possibilities for a given command, page number references are provided to a full discussion of the command found in Danny Goodman’s excellent book, “The Complete HyperCard Handbook.” Pascal examples are shown in boldface while HyperTalk examples are shown in italics type. Annotations or comments are shown within curly brackets. Single spacing will be used to indicate commands that go together as a set, while double spacing will be used to separate independent examples. Program structure Unlike Pascal, HyperTalk cannot be used to create stand-alone programs. It can only be used within HyperCard to control the activity of HyperCard objects (stacks, backgrounds, cards, fields and buttons). HyperTalk is not used to create a “program” that is executed from beginning to end. Instead each HyperCard object may have associated with it HyperTalk commands called a “script”. Each script may contain several Pascal like procedures called “message handlers.” The commands within a message handler are executed when an object receives a message which matches the handler. HyperCard sends messages to an appropriate object whenever certain events occur. For example, a button script may have a message handler which will instruct HyperCard to go to the next card, using a dissolve visual effect, when the mouse is clicked on the button and the “mouseUp” message is received. The script associated with that next card might contain a message handler which will be executed when the card is opened and a corresponding “openCard” message is received by the card (see Goodman, pgs. 345-360). An example of a simple Pascal program and two HyperTalk message handlers are shown below. The Pascal program calls a procedure “getname” which requests the user’s name and then displays “Thank you” on the screen. The first HyperTalk message handler performs essentially the same functions. It is executed when a specific card is opened. The second message handler is executed when the mouse is clicked on a button. HyperTalk message handlers begin with the word “on” followed by the name of a particular message. They always terminate with the word “end” followed by the same message name. In these and subsequent examples, you will find that most HyperTalk commands are closer to standard English commands than their Pascal counterparts. program example; procedure getname; var name: string; begin writeln(‘What is your name?’); readln(name); writeln(‘Thank you’) end; begin getname end. on openCard ask “What is your name?” put “Thank you” into field 1 end openCard on mouseUp visual effect dissolve go to next card end mouseUp Simple variables & assignment statements In Pascal, the programmer has to declare what type of values will be stored in a variable before it can be used. Local variables do not need to be declared in HyperTalk. Global variables are declared in any handler which uses them. All HyperTalk variables are of type text. Numeric characters are automatically converted to numbers before mathematical operations are performed (Goodman, p. 362). In addition to variables, information may be stored and retrieved from other “containers” such as fields, the Message Box, and any text selected by the user (p.400). The following Pascal examples show the declaration of several different types of variables followed by three assignment statements where mathematical expressions are evaluated and variables are given a value. The HyperTalk “put” command also evaluates expressions and stores values into variables or other containers. Note the additional HyperTalk arithmetic commands. var x, y, sum,counter: integer; name: string; r: real; name:=”Paul”; sum:= (x+y)*r/2; counter:=counter +1; put “Paul” into name {p. 415} put (x+y)*r/2 into sum {p. 415} add 1 to counter {p. 441} subtract field 1 from sum{p. 442} multiply salary by selection{p. 444} divide field “total” by num{p. 445} Simple input/output Macintosh Pascal provides a text and a drawing window for the display of text or graphics. Multiple windows can be displayed on the screen using tool box routines in LightSpeed Pascal. The present version of HyperCard only allows one window or card to be displayed on the screen at a time.[In HC version 1.2 and later you can selectively hide and show Card and Background Picts giving you more graphic control. HyperEd] However, HyperCard differentiates between text fields and a graphics layer. In the following examples the Pascal “write” command is used to display a simple string in the text window, while the HyperTalk “put” command is used to display information in a text field. write (‘Display on the text screen’); put “Display in field” into field 2{p. 415} These examples show how values from variables and literal strings are combined for display on the text screen or in a field. In HyperTalk, the double ampersand (&&) adds a space. write (str1 ,’ literal string ‘, str2,’ ‘, str3); put str1 & “ text “ & str2 && str3 into field “Hello” The results of the evaluation of a mathematical expression may also be mixed with literal strings. The “:7:2” after the expression is used to format the resulting real number in a seven column field with two digits after the decimal. HyperTalk requires an additional command, “set numberFormat,” in order to format the real number. write (‘Your salary is ‘, rate*hours:7:2); set numberFormat to “0.00” {p. 489} put “Your salary is “ & rate*hours into field “salary” The Pascal “writeln” command is exactly the same as the “write” command, shown above, except it places a “carriage return” character at the end of the line. To do the same thing in HyperTalk, the “return” character must be given explicitly as shown in the following example. However, in HyperTalk it is generally not necessary to specify the “return” character since the text will automatically be “word wrapped” within the dimensions of the field. This capability makes it possible to use one HyperTalk “put” command where several Pascal “writeln” commands would be necessary. writeln (‘Text followed by return’); put “Text” & return after field “display” {p.415} Macintosh Pascal provides three commands, “drawChar,” “drawString, “ and “writeDraw” for displaying text on the drawing screen. Each of these must be preceded by a “moveto” command which specifies the coordinates of where the text is to be displayed. In HyperCard, the “type” command is used to place text in the graphics layer of a card. This command must be preceded by other commands which select the text tool and specify the location of where the text is to be placed. For many applications, text may be placed in fields and on the graphics layer of a card using the MacPaint like tools built into HyperCard rather than using HyperTalk commands. Text placed in fields may be edited by the user, while painted text in a graphics layer cannot be edited. However, painted text may be erased using the erasure tool or moved using the lasso tool. moveto (40,100); writeDraw (‘Display in drawing window’); choose text tool {p. 429} click at 40,100 {p. 430} type “This is painted text” {p. 433} In Pascal, user input from the keyboard can be obtained by using the “read” or “readln” commands. These are generally preceded by a prompt displayed by the “write” or “writeln” commands. HyperTalk provides several techniques for obtaining keyboard input from the user. The “ask” command displays a dialog box with the first string argument as a prompt and the second string as the default selected response. The user’s response is stored in the HyperTalk system variable called “it.” The “answer” command displays a dialog box with the first string argument as a prompt and two or three pushbuttons each labeled by the succeeding strings. When the user clicks a button, the button label is placed in the variable “it.” Unfortunately, the location of these dialog boxes cannot be controlled by HyperTalk. The “put” command may be used to place a copy of whatever the user types into an editable field into a specified variable, while the “get” command places the contents of a field in the system variable “it.” write (‘Please enter your name ‘); readln(name); ask “Please enter your name” with “Paul” {p. 465} answer “Are you happy” with “yes” or “no” or “maybe” {p. 463} put field 1 into address {p. 415} get field name {p. 419} Operators and functions The following math and boolean operators are the same in both languages. However, HyperTalk includes the additional operators: “is” and “ is not” (p. 563-576). + - * / div mod = <> < <= >= and or not The Pascal “random” function returns a random number between -32767 and +32767, while “the random” function in HyperTalk returns a random number between 1 and a specified value. x:=random; put the random of upper into x {p. 545} The following math functions are the same in both languages. HyperTalk also provides several additional functions such as “annuity” and “average” (p. 546-551). round, trunc, abs ln, sqrt, cos, sin, exp Sequence control structures Both Pascal and HyperTalk provide powerful if-then-else control structures with very similar syntax. The following examples display different messages on the text screen (Pascal) or in a field (HyperTalk) and increment different variables (count or ave) depending on the values of the variables “status” and “gpa.” There is no HyperTalk equivalent of the Pascal “case” statement other than nested “if-then-else” commands (p. 589). if (status = ‘undergrad’) and (gpa > 3.7) then writeln(‘Deans list’) else if gpa < 2.0 then begin writeln(‘Probation’); count := count + 1 end else begin writeln(‘Average’); ave := ave + 1 end; if stat = “undergrad” and gpa > 3.7 then {p. 584} put “Deans List” into field 1{p. 415} else if gpa < 2.0 then {p. 587} put “Probation” into field 1 {p. 415} add 1 to count {p. 441} else {p. 587} put “Average” into field 1 {p. 415} put ave + 1 into ave{p. 415} end if {p. 587} Both Pascal and HyperTalk provide several very powerful repetitive control structures with similar syntax. However, HyperTalk provides an additional structure not found in Pascal and also adds several abnormal exit features not shown in the examples: “next repeat,” “exit repeat,” “exit if,” and “exit (p. 594). The following repeat loops will continue asking for a number from the user and display the value of the number multiplied by 10 until the user enters the number “0.” repeat Writeln(‘Enter a number’); readln (it); writeln(it*10); until it = 0; repeat until it = 0{p. 591} ask “Enter a number”{p. 465} put it *10 & return after field 1 {p. 415} end repeat The following “while” loops will continue asking the user to enter a number and display the dividend produced by dividing the number into 50 until the user enters the number “0.” writeln (‘Enter a number’); readln (it); while it<>0 do begin writeln (50/it); writeln(‘Enter a number’); readln(it); end; ask “Enter a number” {p. 465} repeat while it is not 0 {p. 592} put 50/it & return after field “answer” {p. 415} ask “Enter a number”{p. 465} end repeat These “for do” loops will display the product of the integers 5 through 20 each multiplied by 5. The Pascal “for do” may also use “20 downto 5” while the HyperTalk “repeat with” may use “20 down to 5.” for i:= 5 to 20 do writeln(i*5); repeat with i = 5 to 20 {p. 593).} put i*5 & return after field “product” end repeat This HyperTalk loop will repeat 20 times. Each time it will place the string “I love you” into field 3 and sound a beep. {No Pascal equivalent} repeat 20 times {p. 590} put “I love you “ after field 3 {p. 415} beep 1 {p. 469} end repeat QuickDraw Graphics Pascal and HyperTalk both provide commands for accessing the Macintosh QuickDraw Graphics routines. In most HyperCard stacks, the use of HyperTalk graphics commands within scripts will not be necessary since the drawing of graphics can be done with the MacPaint like tools included within HyperCard. In fact, HyperTalk graphics commands, used in scripts, actually provide access to the same tools found in the HyperCard tools menu. Therefore, the equivalent of most Pascal graphics commands are implemented in HyperTalk by selecting a particular graphics tool using the “choose” command and then simulating the dragging of the mouse across the screen using the “drag” command. Note that the arguments for the “drag” command are in a different order than those of the Pascal “frameRect” command. Ovals and rounded rectangles can also be drawn with HyperTalk by first selecting the corresponding tool. Three or four HyperTalk commands are required to implement the equivalent of some Pascal graphics commands such as “paintCircle” and “invertRect.” drawLine (x1,y1,x2,y2); choose line tool {p. 429} drag from x1,y1 to x2,y2 {p. 431} frameRect (top,left,bottom,right); choose rectangle tool{p. 429} drag from left,top,right,bottom {p. 431} paintRect (r); set filled to true {p. 495} choose rectangle tool{p. 429} drag from x1,y1 to x2,y2 {p. 431} paintCircle (x1,y1,r); set filled to true {p. 495} set centered to true {p. 495} choose oval tool {p. 429} drag from x1,y1 to x1,y2 {p. 431} invertRect(r); choose select tool {p. 429} drag from x1,y1 to x2,y2 {p. 431} doMenu “invert” {p. 421 & 247} eraseRect (r); choose select tool {p. 429} drag from x1,y1 to x2,y2 {p. 431} doMenu “clear picture” {p. 421} penSize (4,4); set lineSize to 4{p. 497} penPat (gray); set pattern to 10{p. 498} penMode (patXor); {No Hypertalk equivalent.} Pascal provides several commands for controlling the properties of text displayed on the drawing screen by subsequent writeDraw commands. In HyperTalk, many properties of painted text, buttons, and fields may be changed using the “set” command. textFont (3); set textFont to Chicago {painted text (p. 499)} textFace ([bold,underline]); set textStyle of button ok to bold, underline {p. 499} textSize(18); set textSize of field 2 to 18 {p. 499} textMode (srcXor); {No HyperTalk equivalent .} Mouse Control The Macintosh tool-box ROM contains several routines, which can be accessed by Pascal commands, for tracking mouse events such as clicking the mouse button and moving the mouse. The “button” function returns a value of “true” if the mouse button is pressed down at the time the function is called; otherwise it returns “false.” The “getMouse” procedure returns the coordinates of the position of the mouse pointer at the time the procedure is called. Although there are directly equivalent commands in HyperTalk, as shown below, mouse events are mainly tracked by message handlers which trap for mouse related messages: “mouseDown,” “mouseUp,” “mouseEnter,” “mouseLeave,” “mouseWithin,” and “mouseStillDown” (p. 383-386). A “mouseUp” message handler is generally used to respond to normal mouse clicks within buttons, fields and card. In the examples below, “the mouseLoc” function returns the current position of the mouse pointer, while “the clickLoc” function returns the position of the mouse at the last click. tf:=button; put theMouseClick into tf {p. 534} getmouse(x,y); put the mouseLoc into xy {p. 529} put the clickloc into xy {p. 535} String procedures and functions Macintosh Pascal provides several powerful commands for manipulating strings. HyperTalk provides even greater power by making it possible to manipulate characters, words, lines and items within any container. it:=length (str); get the length of word 3 of field “input” {p. 537} it:=concat(str1,’ literal ‘, str2,’ ‘, str3); put str1 & “ literal string “ & str2 && str3 into it {&& adds a space (p. 415)} it:=copy(source, firstchar, numofchar); get char 3 to 8 of line 3 of field “source” {p. 419} delete (source, firstchar, numofchar); delete item 3 to 8 of field 3 {p. 420} insert(source, dest, 5); put selection before fifth word of field 2 {p. 415} The Pascal command “pos” is used to find the location of a pattern in a source string. It returns a number indicating the location of the first occurrence of the pattern. Zero is returned if the pattern is not found. The HyperTalk “offset” command performs exactly the same function. Two other HyperTalk commands, “contains” and “is in” return the value “true” if the pattern is found in the source. if pos (pattern, source) <> 0 then writeln (‘Correct’); if offset (pattern, source) <> 0 then {p. 538} put “correct” into field “feedback” {p. 415} if source contains pattern then {p. 573} put “correct” into field “feedback” {p. 415} if pattern is in source then {p. 573} put “correct” into field “feedback” {p. 415} Sound Commands A single voice note can be played by using the “note” command in Pascal. This command requires three integer arguments which specify the pitch, loudness and duration of one note. The HyperTalk “play” command is more versatile with arguments to indicate the instrument or waveform, the overall tempo, and a series of notes specified by normal musical symbols. For example, the notation “c#4e.” would indicate a dotted-eighth c-sharp note in the 4th octave (middle octave). The “play” command can also be used to produce various self contained digitized sound effects. Such sounds may be recorded using a digitizer and converted to a Mac resource file. note (440, 75, 48) play “harpsichord” tempo 380 “b3 c#4 d e f# g a#3” play “breaking glass” {p. 470} User defined types: arrays and records On first glance, HyperTalk doesn’t seem to support user defined types such as arrays and records. However, a card is analogous to a record which can contain many different named fields. Each field can contain up to 32 K. Field data can be accessed by line, item, word or character. Lines are separated by carriage returns, items by commas, and words by spaces. Data fields may be hidden from view if desired. The following HyperTalk script will compute the average of a set of numbers found in a field labeled “scores” for each card in a stack. Each number is separated by a space and considered to be a word. Type scorearray = array [1..25] of integer; student = record of name: string[35]; data: scorearray; end; class = array [1..100] of student; repeat with n = 1 to number of cards {p. 593} go to card n {p. 407} put 0 into sum {p. 415} put the number of words in field “scores” into num {p. 415} repeat with i = 1 to num {p. 593} add word i of field “scores” to sum {p. 441} end repeat put sum/num into field average {p. 415} end repeat Disk file input/output Pascal has three different commands to open a file. “Rewrite” is used to open a new sequential file, “reset” is used to open a sequential file that has been previously created, and “open” is used to open a new or previously created random access file. These commands can be used to open simple text files or structured or typed files. In contrast, HyperTalk only deals with simple text files and only has one open command: rewrite (f,’filename’); open file “filename” {p. 475} reset (f,’filename’); open (f,’filename’); In HyperTalk, if several fields are written to a file they should be separated by writing a “tab” character to the disk (write tab to file “filename”). Records or cards should be separated by writing a Return character to the file (write return to file “filename”). write (f, class[i].data); write field “data” of card i to file “filename” {p. 478} The HyperTalk “read” command just reads characters from the file until it finds a delimiter such as “tab” or “return.” The text is placed in the variable “it”. The delimiter characters should be removed before placing the text in a field. read (f, class[i].data); read from file “filename” until tab {p. 476} close (f); close file “filename”{p. 475} User defined functions and subroutines or procedures The specification and calling of user defined functions in Pascal and HyperTalk is almost identical. The following example functions require two integer parameters and return the value of the first parameter raised to the power specified by the second parameter. function power (num, exp: integer): integer; var i, answ: integer; begin answ:=1; for i:=1 to exp do answ:=answ*num; power:=answ; end; function power num,exp {p. 561} put 1 into answ {p. 415} repeat with i = 1 to exp {p. 593} multiply answ by num{p. 444} end repeat return answ{p. 561} end power The following commands show how the functions defined above might be called. writeln(power(5,3)); put power (5,3) into field 1{p. 561} Similar to Pascal, HyperTalk provides for user defined subroutines, procedures or handlers with value parameters. HyperTalk also provides for both local and global variables. Local variables do not have to be declared in HyperTalk. Global variables must be declared in all subroutines or handlers which access them. In contrast with Pascal, HyperTalk handlers must return values through global variables since there is no provision for variable parameters. The following example procedure and handler compute the power of a number similar to the functions shown above. procedure power (num, exp: integer; var answ: integer); var i:integer; begin answ:=1; for i:=1 to exp do answ:=answ*num; end; on power num,exp global answ{p. 457} put 1 into answ {p. 415} repeat with i = 1 to exp {p. 593} multiply answ by num{p. 444} end repeat end power The procedure or handler defined above would be called using the following commands. In the HyperTalk example, the global variable “answ” must be declared in any handler which calls the subroutine “power.” power (5,3,result); writeln (result); global answ power 5,3 put answ into field “output” Summary and Conclusions Based on the comparisons presented above between Pascal and HyperTalk, it should be clear that HyperTalk is indeed a powerful language with many similarities to Pascal. It provides sophisticated sequence control structures, user defined subroutines and functions with parameter passing, a variety of data structures, powerful math operations and functions, string manipulation functions, text display, graphics and sound commands, and disk file input/output routines. Not shown in the comparison is HyperTalk’s ability to call subroutines and functions recursively. These similarities should make it possible for Pascal programmers to become proficient in HyperTalk without undo effort. In addition to the similarities with Pascal, HyperTalk also has some object oriented programming features similar to languages such as SmallTalk and some list processing features similar to languages such as Logo and Lisp. HyperTalk contains several other commands that have no Pascal equivalent, and therefore are not included in this comparison. Two of these which deserve special mention are the “edit script” and “do” commands. “Edit script” can be used to create and modify scripts from within a HyperTalk script, while the “do” command can be used to execute commands stored in a container such as a variable or field. The HyperCard environment also provides a text editor and MacPaint type graphics tools. These tools, along with the capability to import text and graphics, makes it possible to easily create screen displays without having to rely on complicated programming commands. HyperCard with HyperTalk does indeed provide a powerful and exciting programming environment. There are limitations such as screen size, lack of color, no commands for creating menus and dialog boxes, and the speed limitations of an interpreted language. However, many of these limitations may be rectified in future versions of HyperCard, or through the use of external commands written in other languages.