7.14.9. GRAPHICS AND MUSIC

7.14.9.1 CHARACTER GRAPHICS

To produce character graphics we must be able to output characters anywhere on the screen, in any order that we choose. The normal screen has 25 rows and 80 columns. To place the cursor at a specific location we can use the predefined procedure locate in the form

locate(row, column)

The row can be any integer between 1 and 25 inclusive, and the column can be any integer between 1 and 80 inclusive.

The following codes will print the letter "T" approximately at the center of the screen:

locate(13, 40)

put "T"..

Notice that there are two dots in the put instruction. This indicates that the cursor should be left where it was.

To choose a color for a character use the color procedure in the form

color(colorNumber)

To choose a color for the background of a character use the color back procedure in the form

colorback(colorNumber)

The available color numbers are

0 black             8 dark gray         
1 blue              9 light blue        
2 green             10 light green      
3 cyan              11 light cyan       
4 red               12 light red        
5 magenta           13 light magenta    
6 brown             14 yellow           
7 white             15 bright white     

If 16 is added to the color number the character will blink as it is displayed. The following is a simple sample program.

/**********************************************************

Filename: cgraph.t

Author: Zhiwei Wang

Date: July, 1995

Description: An example for character graphics

***********************************************************/

var x, y, c : int

cls

for i:1..maxrow-1

colorback(1) % blue

locate(i,1)

put " "

end for

loop

exit when hasch % exit when any key is pressed

randint(y, 1, maxcol)

randint(x, 1, maxrow-1)

randint(c, 1, maxcolor)

locate(x, y)

colorback(c)

put " "..

end loop

cls

for i: 0..15

color(i)

for j : 1..maxrow-1

locate(j, j+3*i)

put "Bye"..

end for

end for

/* end program */

/*********************************************************/

To hide the cursor, use the predefined procedure setscreen in the form setscreen("nocursor"). To have the cursor show again use setscreen("cursor").

7.14.9.2. PICXEL GRAPHICS

To set the screen to CGA graphics mode we use the statement setscreen("graphics"); to VGA graphics mode use setscreen("graphics:v16").

Pixel positions are given in terms of coordinates. The x-coordinate is the position number along a line starting from the left-hand side of the screen. The first position has number 0; The last position depends on the type of graphics. For CGA graphics it is 319, for VGA it is 639. The y-coordinate is the row number starting at the bottom of the screen at 0 and numbering upward. In CGA graphics the last row is 199, in VGA it is 479.

To place a dot on the screen at a point whose coordinates are (x, y) use the statement

drawdot(x, y, c)

where 'c' is an integer that sets the color of the dot. The actual color depends on the current palette. Values 1, 2, and 3 give either green, red, and brown for a palette number 0f 0; cyan, magenta, and white for a palette number 1. The palette number is set by a call to the predefined procedure palette in the form

palette(number)

Palette numbers 2 and 3 are the same as 0 and 1 respectively except that the colors are more intense.

The following example shows all the colors of palette number 3 for an IBM PC compatible using CGA graphics.

% File name : showcolor.t

setscreen("graphics")

palette(3)

for colorNumber: 0..maxcolor

color(colorNumber)

put "Color number", colorNumber

end for

To draw a line from point (x1, y1) to point (x2, y2) in color c use the statement

drawline(x1, y1, x2, y2, c)

To draw a rectangle in color c use the statement

drawbox(x1, y1, x2, y2, c)

where (x1, y1) is the lower left corner and (x2, y2) is the upper right corner of the rectangle.

To draw an oval in color c use the statement

drawoval(x, y, xRadius, yRadius, c)

where (x, y) is the center, xRadius is the half width and yRadius is the half height.

An arc is a portion of an oval. To draw an arc in color c use the statement

drawarc(x, y, xRadius, yRadius, initialAngle, finalAngle, c)

where (x, y) is the center, xRadius is the half width, yRadius is the half height, initialAngle is the initial angle, and finalAngle is the final angle.

To fill an area surrounded by a bordercolor with a fillcolor, choose a point (x, y) in the area and use the statement

drawfill(x, y, fillcolor, bordercolor)

If we want to add text to a pixel graphics, we can set the position of the characters to be output using the statement

locatexy(x, y)

To choose a color for the background use the color back procedure in the form

colorback(colorNumber)

The procedure cls will clear the screen and set it to the current background color.

A number of functions are provided to find out the current values of various parameters:

whatdotcolor(x, y)  gives the color of the pixel at (x, y)               
whatcolorback       gives the current background color                   
whatpalette         gives the current palette color                      
whatcolor           gives the current color                              

Here is an example that draws random figures.

/************************************************************

Filename: multi.t

Author: Zhiwei Wang

Date: August, 1995

Description: This is a example which draws random figures.

************************************************************/

setscreen("graphics")

var x1, y1, r1, r2, c, choice, count: int

var ch: string(1)

put "Hit any key to begin and kit any key to exit"

randomize

getch(ch)

count := 0

loop

randint(c, 0, maxcolor)

if (count mod 100 = 0) then

colorback(c)

cls

end if

count := count + 1

randint(r1, 5, 60)

randint(r2, 5, 60)

randint(x1, 0, maxx)

randint(y1, 0, maxy)

randint(choice, 1, 8)

case choice of

label 1:

drawfillstar(x1, y1, x1+r1, y1+r2, c)

label 2:

drawfillbox(x1, y1, x1+r1, y1+r2, c)

label 3:

drawfilloval(x1, y1, r1, r2, c)

label 4:

drawstar(x1, y1, x1+r1, y1+r2, c)

label 5:

drawbox(x1, y1, x1+r1, y1+r2, c)

label 6:

drawoval(x1, y1, r1, r2, c)

label 7:

drawfillmapleleaf(x1, y1, x1+r1, y1+r2, c)

label 8:

drawmapleleaf(x1, y1, x1+r1, y1+r2, c)

end case

exit when hasch %Exit when any key is pressed

end loop

cls

/* end of program */

/**********************************************************/

7.14.9.3. MOUSE RELATED FUNCTIONS

This section introduces some mouse related predefined functions which might be needed in graphics applications.

The mouse has two different modes: single-button mode and multi-button mode. In single-button mode, the mouse is treated as a one button mouse. A button is considered pressed when any button is pressed and released when all buttons are released. The default mode is single-button mode. To change the mode, we can use the procedure buttonchoose. The syntax is

buttonchoose(choice: string)

where the parameter choice can be "singlebutton", "onebutton", or "multibutton".

The current information about the status of a mouse can be got using the mousewhere procedure. The syntax is

mousewhere(var x, y, button: int)

The parameters x and y are the current location of the cursor. The parameter button is set depending the current mouse mode. In "single-button mode", the value of button is set to 0 if all the buttons are up, and 1 if any button is down. In "multi-button mode", it is assigned the sum of values from each button: 1 if the left button is down, 10 if the middle button is down, and 100 if the right button is down. For example, 101 means that the left and right buttons are down.

If you want to hide the mouse cursor, you can use the procedure mousehide. If you want it to re-appear, you can use the procedure mouseshow. Here is an example that hides the mouse whenever it is outside the box (50, 50)-(100, 100).

var x, y, button: int

loop

mousewhere(x, y, button)

if x>50 and x<100 and y>50 and y<100 then

mousehide

else

mousehide

end if

end loop

The buttonwait procedure gets information of a mouse event. The syntax is

buttonwait(motion: string,

var x, y, buttonnumber, buttonupdown: int)

The parameter motion can be one of "up", "down", "updown", and "downup". In "single-button" mode", a "down" occurs whenever all the buttons are up and a button is pressed. An "up" event occurs when the last button is released so that no buttons remains pressed. In "multi-button" mode", a "down" occurs whenever any button is pressed. An "up" event occurs whenever any button is released. The parameters x and y are set to the position of the mouse cursor when the button was pressed. The parameter buttonnumber is set to 1 in "single-button mode". In "multi-button mode", it is set to 1, 2, or 3 respectively when the left, middle, or right button was pressed. The parameter buttonupdown is set to 1 if a button was pressed and 0 if a button was released.

A mouse event is detected by the function buttonmoved. The syntax is

buttonmoved(motion : string) : boolean

The parameter motion can be one of "up", "down", "updown", and "downup". If an event of the type requested is in the queue, buttonmoved returns true, otherwise returns false.

Here is a sample program demonstrating how to use the above procedures.

/**********************************************************

Filename: mouseflag.t

Author: Zhiwei Wang

Date: August, 1995

Description: Every time you click and drag the mouse, a flag will be drawn. The flags drawn before will be changed to random colors in the background.

To exit the program, hit any key and then click the mouse.

**********************************************************/

setscreen("graphics")

procedure drawflag(x, y, newx, newy, c1, c2: int)

var xp: int

xp := (newx-x) div 4

drawfillbox(x, y, newx, newy, c1)

drawfillbox(x+xp, y, x+3*xp, newy, c2)

drawfillmapleleaf(x+xp, y, x+3*xp, newy, c1)

end drawflag

var x, y, buttonnumber, buttonupdown, buttons, c1, c2: int

var xtemp, ytemp, newx, newy := 0

colorback(1)

cls

loop

drawfillbox(0, 0, maxx, 10, 9)

locatexy(0, 0)

colorback(9)

put " Drag the mouse to draw a flag. Click on this line to exit"..

randint(c1, 0, maxcolor)

randint(c2, 0, maxcolor)

buttonwait("down", x, y, buttonnumber, buttonupdown)

exit when y < 10 %Exit when any key is pressed

drawflag(xtemp, ytemp, newx, newy, c1, c2)

loop

exit when buttonmoved("up")

mousewhere(newx, newy, buttons)

end loop

buttonwait("up", newx, newy, buttonnumber,buttonupdown)

drawflag(x, y, newx, newy, 4, 8)

xtemp := x

ytemp := y

end loop

drawflag(0, 0, maxx, maxy, 4, 8)

delay(1000)

cls

/* end of program */

/**********************************************************/


7.14.9.4. SOUND AND MUSIC

To produce a simple sound use the predefined procedure sound in the form

sound(frequency, duration)

where the frequency is in hertz and the duration is in milliseconds.

To produce a single note use the predefined procedure play in the form

play(value-of-note)

where the value-of-note is its duration followed by its pitch. The pitch of a note is given by the letters A to G inclusive. If the letter is followed by a plus sign, the note is sharp; if by a minus sign, the note is flat.

The duration is given by a digit according to the table below:

1 whole note

2 half note

4 quarter note

8 eighth note

6 sixteenth note

Example: This example will play C as an eighth note

play("8C")

Once the duration of a note has been set, all the subsequent notes will have the same duration unless explicitly set to a different duration.

The pitch is presumed to be in the middle octave. The middle octave notes are

CDEFGAB

To represent a note, say C, in the octave above the middle octave, use the notation >C; to represent it in the octave below the middle, use <C. Once > or < have been used, all subsequent notes will be shifted to the same octave. To prevent this, we might use a < or > to return the note to the middle octave after it is played. For example

play("8>A<")

A rest or pause is represented by P (or p). The duration of a rest is given in the same way as the duration of a note. A series of notes can be concatenated as one string and used as the parameter of a play instruction. For example:

play("4EDCDEE2E4DD2D4EG2G")

The following is a sample program. In this program, a series of pictures of a flag are shown in different places at different time, creating an animation, while a music note is chosen randomly and played.

/**********************************************************

Filename: musicflag.t

Author: Zhiwei Wang

Date: August, 1995

Description: A flag raises with some random music

**********************************************************/

setscreen("graphics")

const CScale := "CDEFGAB" % Define an array for music notes

procedure drawflag(x, y, newx, newy, c1, c2: int)

var xp, yp: int

xp := (newx-x) div 4

drawfillbox(x, y, newx, newy, c1)

drawfillbox(x+xp, y, x+3*xp, newy, c2)

drawfillmapleleaf(x+xp, y, x+3*xp, newy, c1)

end drawflag

var x, y, note: int

y := 0

x := 200

colorback(1)

cls

drawfillbox(x-5, 0, x, maxy, 7) % Draw a flag pole

loop

randint(note, 1, 7)

drawflag(x, y, x+100, y+50, 4, 8)

exit when y >= (maxy - 60)

play(CScale(note))

drawfillbox(x, y, x+100, y+10, 1) % Erase the old flag

y := y+10

end loop

play("4EDCDEE2E4DD2D4EG2G")

/* end of program */

/**********************************************************/

7.14.10. OBJECT -ORIENTED PROGRAMMING

Object-oriented programming views a program as a collection of objects. Each object is responsible for specific tasks. It is by the interaction of objects that computation proceeds.

An object is an encapsulation of state (data values) and behavior (operations). The behavior of objects is dictated by the object's class. Every object is an instance of some class. all instance of the same class will behave in a similar way in response to a similar request.

Upon receiving a message, an object will invoke a method (similar to executing a procedure). The specific method performed is decided by the object and may differ from one class of objects to another.

Classes can be organized into a hierarchical inheritance tree. Data and behavior associated with classes higher in the tree can also be accessed and used by classes lower in the tree. This is called inheritance.

The main task of writing a object-oriented Turing program is breaking the problem down into underlying data types (classes) and defining the properties of each class. In this section, we briefly introduce how to use the keywords module and class to create objects. Sample programs are also presented.

7.14.10.1. MODULE

In some other programming languages, a module is called a package, cluster, or object. A module declaration creates a package of variables, constants, types, subprograms, etc. The syntax is

module id

[implement implementItem]

[implement by implementByItem]

[import [var] importItem {, [var] importItem}]

[export [howExport] id {, [howExport] id}]

statementsAndDeclarations

end id

Items defined inside a module can be accessed outside the module only if they are exported. Items from outside a module that are to be used in the module need to be imported unless they are predefined or pervasive. These are done by using the keywords import and export. The optional howExport can be one of the keywords var, unqualified, pervasive, and opaque.

The keyword var applies only to variables. It means that the value of the exported variable can be changed. The keyword unqualified means that references to the exported item do not need to be prefixed by the name of the exporting item. For example, if p is the unqualified exported item and M is the exporting item, a call to p outside of M can be just p instead of the usual form M.p. The keyword pervasive is only meaningful if unqualified is also present. It specifies that the exported item is to be visible in subsequent scopes, that is, it is not necessary to import it into objects. The keyword opaque applies only to type names. When a type is exported using the keyword opaque, it is distinct from all other types. This means that the updates to the values of the type can be only done within the exporting item. Here is an example:

module stack

export push, pop

var top : int := 0

var nameList : array 1..100 of string

procedure push (s: string)

top ++

nameList(top) := s

end push

procedure pop (var s: string)

s := nameList(top)

top--

end pop

end stack

stack.push("Tom")

var name: string

stack.pop(name)

In this example, the module declaration creates an object called stack.. Outside of the stack module, the procedure push and pop can be called using the notation stack.push and stack.pop because they are exported from the module. Other items (top and name ) cannot be accessed because they are not exported.

The [implement implementItem] is an implement list which is used to specify that the module is to be the implementation of another module. The module containing the list gains access to (inherits) all the declarations inside the target item. The [implement by implementByItem] is an implement -by list which is used to specify that the module is to be the implemented by the implementByItem. Suppose D and C are two modules. D can be in C's implement-by list if and only if C is in D's implement list. If D is in C's implement-by list, then C is called the interface and D is called the implementation. The implementation is an expansion of the interface. Here is an example:

module stack2 % Interface

implement by stack2Body

export push, pop

deferred procedure push (s: string)

deferred procedure pop (var s: string)

end stack2

module stack2Body % Implementation

implement stack2

var top : int := 0

var nameList : array 1..100 of string

body procedure push (s: string)

top := top + 1

nameList(top) := s

end push

body procedure pop (var s: string)

s := nameList(top)

top := top - 1

end pop

end stack2Body

In this example, stack2 is an interface and stack2Body is an implementation. In the interface istack2, there are two deferred subprogram declarations. A deferred subprogram declaration consists of the keyword deferred and the followed subprogram header. A subprogram is declared to be deferred when you want to be able to override it in an expansion. A deferred subprogram is resolved by giving its body. This can be done in the module containing the deferred declaration or in any expansion of it. In the example, the procedures push and pop are resolved in the expansion stack2Body. The overriding procedure must use the keyword body as shown in the example.

7.14.10.2. CLASS

A class declaration creates a template for a package of variables, constants, types, subprograms, etc. instead of creating the objects. After creating a class, we can use the key word new to create an instance (objects) of the class.

The syntax of class declaration is

[monitor]

class id

[inherit inheritItem]

[implement implementItem]

[implement by implementByItem]

[import [var] importItem {, [var] importItem}]

[export [howExport] id {, [howExport] id}]

statementsAndDeclarations

end id

If the optional keyword monitor is present before the keyword class, the objects created are called monitors. A monitor is a module in which only one process can be active at a time. This means that a process will be blocked if it calls a monitor that is already active. The process will not be allowed to proceed until the monitor is inactive.

The features of module discussed in the preceding section are also valid for class. For example, items defined inside a class can be accessed outside the class only if they are exported. Items from outside the class that are to be used in the class need to be imported unless they are predefined or pervasive.

The keyword inherit and the inheritItem form an inheritance list. This inherit list specifies that the class containing the list is to be an expansion of another class. This expansion is called inheritance. The class containing the list inherits all the declarations inside the target item. If class C inherits class P, we say C is the child and P is the parent . Given two classes A and D, A is said to be an ancestor of D if A and D are the same class, or A is the parent of D, or A is the parent of the parent of D, etc. A is the ancestor of A is denoted as A <= D. If A is the ancestor of D, then D is said to be the descendant of A, denoted as D >= A. If A and D are not the same class, then we say A is the strict ancestor of D and D is the strict descendant of A, denoted as A < D and D > A respectively.

Example

class stackClass

export push, pop

var top : int := 0

var nameList : array 1..100 of string

procedure push (s: string)

top ++

nameList(top) := s

end push

procedure pop (var s: string)

s := nameList(top)

top--

end pop

end stackClass

var p: point to stackClass

new stackClass, p

stackClass(p).push("Tom") %Short form: p->push("Tom")

var name: string

stackClass(p).pop(name) %Short form: p->pop(name)

class stackWithDepth

inherit stackClass

export depth

function depth : int

result top

end depth

end stackWithDepth

var q: point to stackWithDepth

new stackWithDepth, q

%some more code here

free p

free q

In this example, the class stackClass is a template for creating objects. Keyword new is used to create the object p, which is an instance of class stackClass. Many instances of a class can exist at a given time, each located by a pointer.

Class stackWithDepth inherits class stackClass. StackClass is the parent and stackWithDepth is the child. Objects of the inherited class stackWithDepth are like objects of the parent class stack, except there is an additional function depth.

When an object is no longer needed, use the keyword free to return the memory storage.

7.14.10.3 SAMPLE PROGRAMS

/***********************************************************

Filename: stack_class.t

Author: Anonymous

Student #: 123 456 789

Purpose: an example showing a class, called stackWithDepth, inherits another class called stack by adding a function called depth.

************************************************************/

class stack

export push, pop

const MAXSTACK :int := 100

var top:int := 0

var contents:array 1..MAXSTACK of string

procedure push (s:string)

top := top +1

contents (top) :=s

end push

procedure pop (var s:string)

s := contents (top)

top := top - 1

end pop

end stack

class stackWithDepth

inherit stack

export depth

function depth:int

result top

end depth

end stackWithDepth

/*****************MAIN**************************************/

var contents: array 1..* of string

:= init ("a", "b", "c", "d", "e")

var stack1: pointer to stack

var stack2: pointer to stackWithDepth

new stack, stack1

new stackWithDepth, stack2

stack1->push (contents(1))

stack1->push (contents(2))

stack1->push (contents(3))

stack1->push (contents(4))

stack1->push (contents(5))

stack2->push (contents(5))

stack2->push (contents(4))

stack2->push (contents(3))

stack2->push (contents(2))

stack2->push (contents(1))

put "Depth of stack 2 => ",stack2->depth

for i:1..5

var tmp: string

stack1->pop(tmp)

put "stack 1 =>",tmp..

stack2->pop(tmp)

put " stack 2 =>",tmp..

put " contents 1 =>",contents(i)

end for

/* end of program */

/***********************************************************

Filename: inherit2.t

Author: Anonymous

Student #: 123 456 789

Purpose: an example showing that deferred procedures can be resolved in different expansions.

************************************************************/

class figure

export drawMe, var myColour, var x, var y, changeSize

var mySize : nat := 20

var myColour : int := Red

var x, y : int

randint (x, 0, maxx - 4 * mySize)

randint (y, 0, maxy - 4 * mySize)

deferred procedure changeSize (newWidth, newHeight : nat)

deferred procedure drawMe

end figure

class circle

inherit figure

var radius := mySize div 2

body procedure drawMe

drawoval (x, y, radius, radius, myColour)

end drawMe

end circle

class rectangle

inherit figure

var width, height : int

width := mySize

randint (height, 10, mySize)

body procedure changeSize (newWidth, newHeight : nat)

mySize := max (newWidth, newHeight)

width := newWidth

height := newHeight

end changeSize

body procedure drawMe

drawbox (x, y, x + width, y + height, myColour)

end drawMe

end rectangle

class square

inherit rectangle

body procedure changeSize

pre newWidth = newHeight

rectangle.changeSize (newWidth, newHeight)

end changeSize

end square

var f :array 1..3 of pointer to figure

new circle, f(1)

new rectangle, f(2)

new square, f(3)

f(1)->x := 40

f(1)->y := 40

f(2)->x := 40

f(2)->y := 40

f(2)->changeSize(100,200)

f(3)->x := 40

f(3)->y := 40

f(3)->changeSize(70,70)

for i:1..3

f(i) -> drawMe

end for

7.14.11. USING TURING ON IBM PCs AND COMPATIBLES

The PC Turing distribution includes tcomp.exe, turing.exe, and oturing.exe. Tcomp.exe is a Turing compiler, turing.exe and oturing.exe are Turing editors.

To use the Turing compiler, type

tcomp filename

If the compilation is successful, an executable file 'outfile.exe' will be created. If the optional output file name 'outfile.exe' is not provided, the name of the Turing file will be used with the extension '.exe'. To run the program, just type the file name of the executable file. To get a complete list of options, type 'tcomp' without filename.

Turing.exe is the Turing editor. It has pull-down menus, mouse support, and dialog boxes, much like DOS editor or Turbo Pascal's editor. To start this editor, just type 'turing' or 'turing filename.t'. If the file name is provided, the file will be opened, otherwise you get a blank window which is ready for you to input codes. This editor is easy to use, however, it takes more space than the 'oturing.exe' editor. If you are running large programs and running out of memory, then you might want to try the 'oturing.exe' editor.

Oturing.exe is the old turing editor. To use this editor, type 'oturing' alone or followed by a filename. If the file name is provided, the file will be opened, otherwise you get a blank window which is ready for you to input codes.

The initial window looks like :

When the cursor is in the window in the position in which you want to start typing, you can begin typing. When you are typing more than one line, use the return key at the end of each line to begin the next new line.

The ":" moves the cursor to the bottom of the screen, below the bottom border. You can then write a command that is longer than a single character.

Commands Used While Editing in Text Window:

These are the commands that work when the cursor is in the text window. While the cursor is inside the text window, the user is in text-editing mode. Other commands apply when the cursor is on the left border.

LEFT/RIGHT ARROW Move left/right a character

CTL-LEFT Move left/right a word

CTL-RIGHT ARROW Move left/right a word

HOME/END Move to beginning/end of the line

UP/DOWN ARROW Move up/down a line

PG UP/DN Move up/down a page

CTL-PG UP/DN Move to top/bottom of program

CTL-HOME/CTL-END Move to top/bottom of the screen

DELETE Delete character in place

BACKSPACE Delete character to left of cursor

CTL-B Delete from beginning of line

CTL-E Delete to end of line

CTL-Y Delete line

F1 Run program

F2 Paragraph program

F9, F10 Call up previous command

CTL-U Undo edits on line

ENTER Move to next line / splits line in insert mode

INSERT Enter or exit from insert mode

ESCAPE Move to left hand bar

Commands From the Left Border:

These are commands that work when the cursor is on the left border of the Turing editor. These commands act on a line at a time or the program as a whole.

a Append below current line

CTL-Y, d Delete line or marked text

f Display default filename

h Help

i Insert above current line

m Enter or exit mark mode

n Repeat last find in forward direction

p Repeat last find in backwards direction

s Repeat last substitute on line or marked text

S Repeat last substitute for every instance on line or marked text

t Take line or marked text and place in buffer

u Undelete saved lines

v, V Current version

w Write file

N, E Move to next error

X Cancel errors

F1, r, R Run program

F2 Paragraph program

F9, F10 Call up previous command

RIGHT ARROW Enter text editing window

SPACE BAR Enter text editing window

HOME/END Move to beginning/end of the line and enter text editing window

CTL-RIGHT ARROW Move right a word and enter text editing window

UP ARROW, - Move up a line

DOWN ARROW, Move down a line

+, ENTER Move down a line

PG DN, > Move down a page

PG UP, < Move up a page

CTL-PG UP, 1 Move to top of program

CTL-PG DN, $ Move to bottom of program

CTL-HOME/CTL-END Move to top/bottom of the screen

INSERT Enter or exit from insert mode

CTL-U Redraw screen

% Turn lines into comments

= Line number

:e <file>, :edit <file> Edit a file called <file>

:w <file>, :write <file> Write program out to <file>

:new Start a new program

:q, :quit Quit Turing

:r, :run Run program

:h, :help Help

:para Paragraph program

/xxx Find next occurrence of xxx

?xxx Find previous occurrence of xxx

:g/xxx/yyy Replace all instances of xxx with yyy

:s/xxx/yyy Replace the first instance of xxx with yyy

:S/xxx/yyy Replace all instances of xxx in a line with yyy

:j, :join Join to next line

:%, :comment Turn current or marked lines into comments

:%-, :uncomment Remove % from beginning of current line or marked lines

DOS-like Commands:

:cd, :chdir Change directory

:dir, :ls Get a directory listing

:ren, :rename, :mv Rename a file on disk

:copy, :cp Copy a file on disk

:sh, :shell Enter a DOS shell

:del, :rm Delete a file From disk

Information Commands:

:info Editor Options/Memory/Video Information

:size Compiled Program Memory Information

:v, :version Current version

Printing Commands:

:print Print file on the printer

:lprint Print file with line numbers on the printer

:printhelp Print command help on the printer

7.14.12. TURING SUMMARY

Compiling and running:

toot [-i infilename] [-o outfilename] filename.t

Arithmetic Operators:

+ for addition

- for subtraction

* for multiplication

** for exponentiation

/ for division

div for integer division

mod for remainder of integer division

Boolean Operators:

not negation (a unary operator)

and and

or or

= equal

not= not equal

< less than

<= less than or equal to

> greater than

>= greater than or equal to

Comments:

% begins a single line comment

/* and */ surround multi-line comments

Keywords:

all and array assert begin

bind body boolean case collection

const decreasing div else elsif

end enum exit export false

fcn for forward free function

get if import in init

int invariant label loop mod

module new not of opaque

open or pervasive pointer post

pre proc procedure put read

real record result return seek

set skip string tag tell

then to true type union

var when write

Variable declaration:

var identifierList : dataType

where var the keyword used for variable declarations,

identifierList list of one or more identifiers separated by commas,

: a colon precedes the data type,

dataType specifies a particular data type.

Standard data types:

int for integer numbers

real for floating point numbers

string[(length)] for character strings (default length is 255 characters)

boolean for the boolean values of "true" or "false"

Type declaration:

type identifier : dataType

Assignment statements:

identifier := expression

var identifier : dataType := value

const identifier : = expression

String Operations:

Substring Specification:

string_name ( start_position .. end_position )

Note: "*" can be used to denote the last character position.

String Concatenation:

string1 + string2

Array Use:

Declaration:

var array_name : array 1 .. last_element [ , 1 .. last_element] of dataType

[ := init ( value, ... value ) ]

Reference by: array_name(index)

Record Use:

Declaration:

var record_name

record

field_1 : dataType

.

field_n : dataType

end record

Reference by: record_name. field_name

Input/Output:

put data_item[:width] [, data_item[:width] ]

put " " outputs a blank line

put skip outputs 2 blank lines

put variable list .. the 2 dots stop the cursor from going to a new line

get variable [ : width ] [, variable [ : width ]]

File Input/Output:

open: stream_number, "file_name", "mode"

where:

stream_number is a previously declared integer variable, its value is assigned by the open statement

file_name is the name of the disk file

mode is "r" for read, or "w" for write

assert stream_number not= 0 % set to true if file cannot be opened

get: stream_number, list of input items

eof(stream_number )

put: stream_number, list of output items

close: stream_number

Loops:

for [ decreasing ] variable : start_count .. end_count

statement(s)

end for

loop

statement(s)

exit when condition

statement(s)

end loop

Conditions:

if condition1 then

statement1

elsif condition2 then

condition2

else

statement3

end if

case expression of

label case-label1: statement1

label case-label2: statement2

...

label case-labeln: statementn

label: statement

end case

Sub Programs:

procedure procedure_name [ ( [var] parameter : type ... ) ]

[ local variable declarations ]

statement(s)

end procedure_name

function function_name [ ( parameter : type ... ) ]

x(: function_type )

[ local variable declarations ]

statement(s)

x(result variable_name or expression )

end function_name

Collections:

Declaration:

var collection_name : collection of dataType

var pointer_name : pointer to collection_name

Access by:

collection_name(pointer_name)

new collection_name, pointer_name

free collection_name, pointer_name

Turing - READER'S COMMENTS

If you find an error in this manual, or have a suggestion as to how it can be improved, please specify the problem and the page on which it occurs.

_______

Please return this form to the Computer Science office, or Mail to:

Computer Science Department Office

University of Regina

S4S 0A2