Added example programs.
This commit is contained in:
		
							parent
							
								
									d2c505ad6b
								
							
						
					
					
						commit
						681e2e0432
					
				
					 8 changed files with 1595 additions and 0 deletions
				
			
		
							
								
								
									
										7
									
								
								examples/.distr
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								examples/.distr
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,7 @@ | |||
| hilo.b | ||||
| hilo.c | ||||
| hilo.mod | ||||
| hilo.ocm | ||||
| hilo.p | ||||
| paranoia.c | ||||
| README | ||||
							
								
								
									
										26
									
								
								examples/README
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								examples/README
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,26 @@ | |||
| # $Source$ | ||||
| # $State$ | ||||
| # $Revision$ | ||||
| 
 | ||||
| A few notes on the examples | ||||
| --------------------------- | ||||
| 
 | ||||
| This directory contains a handful of working example programs that can be | ||||
| built with the ACK. They're intended as a convention source of test code | ||||
| and as a starting point when writing your own programs. | ||||
| 
 | ||||
| They consist of: | ||||
| 
 | ||||
| hilo.c             ANSI C version of the Hi/Lo game | ||||
| hilo.b             Basic version of the Hi/Lo game | ||||
| hilo.mod           Modula-2 version of the Hi/Lo game | ||||
| hilo.ocm           Occam 1 version of the Hi/Lo game | ||||
| hilo.p             Pascal version of the Hi/Lo game | ||||
| 
 | ||||
| paranoia.c         An ancient public domain K&R C adventure game | ||||
| 
 | ||||
| Enjoy. | ||||
| 
 | ||||
| David Given | ||||
| dg@cowlark.com | ||||
| 2007-02-25 | ||||
							
								
								
									
										39
									
								
								examples/hilo.b
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								examples/hilo.b
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,39 @@ | |||
| 1 ' $Source$ | ||||
| 2 ' $State$ | ||||
| 3 ' $Revision$ | ||||
| 
 | ||||
| 10 print "Hi there! Before we start, what is your name?" | ||||
| 20 input "> ", PlayerName$ | ||||
| 30 print | ||||
| 40 print "Hello, "; PlayerName$; "!" | ||||
| 
 | ||||
| 100 gosub 1000 | ||||
| 110 print | ||||
| 120 print "Would you like another go?" | ||||
| 130 input "> ", s$ | ||||
| 140 s$ = left$(s$, 1) | ||||
| 150 if s$ = "n" or s$ = "N" then goto 200 | ||||
| 160 print | ||||
| 170 print "Excellent! "; | ||||
| 180 goto 100 | ||||
| 200 print | ||||
| 210 print "Thanks for playing --- goodbye!" | ||||
| 220 end | ||||
| 
 | ||||
| 1000 print "See if you can guess my number." | ||||
| 1010 Number% = rnd(1) mod 100 | ||||
| 1020 Attempts% = 1 | ||||
| 
 | ||||
| 1030 print | ||||
| 1040 input "> ", guess% | ||||
| 1050 if guess% < Number% then print: print "Try a bit higher." | ||||
| 1060 if guess% > Number% then print: print "Try a bit lower." | ||||
| 1070 if guess% = Number% then goto 1100 | ||||
| 1080 Attempts% = Attempts% + 1 | ||||
| 1090 goto 1030 | ||||
| 1100 print | ||||
| 1110 print "You got it right in only"; Attempts%; | ||||
| 1120 if Attempts% = 1 then print "go"; else print "goes"; | ||||
| 1130 print "!" | ||||
| 1140 return | ||||
| 
 | ||||
							
								
								
									
										83
									
								
								examples/hilo.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								examples/hilo.c
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,83 @@ | |||
| /* $Source$
 | ||||
|  * $State$ | ||||
|  * $Revision$ | ||||
|  */ | ||||
| 
 | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| 
 | ||||
| char buffer[32]; | ||||
| char PlayerName[32]; | ||||
| 
 | ||||
| int Number; | ||||
| int Attempts; | ||||
| 
 | ||||
| void reads(void) | ||||
| { | ||||
| 	char* p; | ||||
| 	 | ||||
| 	printf("> "); | ||||
| 	fflush(stdout); | ||||
| 	 | ||||
| 	fgets(buffer, sizeof(buffer), stdin); | ||||
| 	 | ||||
| 	p = strchr(buffer, '\n'); | ||||
| 	if (p != NULL) | ||||
| 		*p = '\0'; | ||||
| } | ||||
| 
 | ||||
| void game(void) | ||||
| { | ||||
| 	printf("See if you can guess my number.\n"); | ||||
| 
 | ||||
| 	Number = rand() % 100; | ||||
| 	Attempts = 1; | ||||
| 	 | ||||
| 	for (;;) | ||||
| 	{ | ||||
| 		int guess; | ||||
| 		 | ||||
| 		printf("\n"); | ||||
| 		reads(); | ||||
| 		guess = atoi(buffer); | ||||
| 		 | ||||
| 		if (guess == Number) | ||||
| 		{ | ||||
| 			printf("\nYou got it right in only %d %s!\n", Attempts, | ||||
| 				(Attempts == 1) ? "go" : "goes"); | ||||
| 			return; | ||||
| 		} | ||||
| 		 | ||||
| 		if (guess < Number) | ||||
| 			printf("\nTry a bit higher.\n"); | ||||
| 		if (guess > Number) | ||||
| 			printf("\nTry a bit lower.\n"); | ||||
| 		Attempts++; | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| int main(int argc, char* argv[]) | ||||
| { | ||||
| 	printf("\nHi there! Before we start, what is your name?\n"); | ||||
| 	reads(); | ||||
| 	strcpy(PlayerName, buffer); | ||||
| 	printf("\nHello, %s! ", PlayerName); | ||||
| 	 | ||||
| 	for (;;) | ||||
| 	{ | ||||
| 		game(); | ||||
| 		printf("\nWould you like another go?\n"); | ||||
| 		reads(); | ||||
| 		 | ||||
| 		if ((buffer[0] == 'n') || (buffer[0] == 'N')) | ||||
| 		{ | ||||
| 			printf("\nThanks for playing --- goodbye!\n"); | ||||
| 			exit(0); | ||||
| 		} | ||||
| 		 | ||||
| 		printf("\nExcellent! "); | ||||
| 	} | ||||
| 	 | ||||
| 	return 0; | ||||
| } | ||||
							
								
								
									
										95
									
								
								examples/hilo.mod
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								examples/hilo.mod
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,95 @@ | |||
| (* $Source$ | ||||
|  * $State$ | ||||
|  * $Revision$ | ||||
|  *) | ||||
| 
 | ||||
| MODULE HiLo; | ||||
| FROM InOut IMPORT WriteInt, WriteLn, WriteString, ReadString, ReadInt; | ||||
| FROM random IMPORT Uniform; | ||||
| 
 | ||||
| VAR | ||||
| 	buffer : ARRAY [0..32] OF CHAR; | ||||
| 	 | ||||
| PROCEDURE reads; | ||||
| BEGIN | ||||
| 	WriteString("> "); | ||||
| 	ReadString(buffer); | ||||
| END reads; | ||||
| 
 | ||||
| PROCEDURE game; | ||||
| VAR | ||||
| 	Number : INTEGER; | ||||
| 	Attempts : INTEGER; | ||||
| 	guess : INTEGER; | ||||
| 	finished : BOOLEAN; | ||||
| BEGIN | ||||
| 	WriteString("See if you can guess my number."); | ||||
| 	WriteLn; | ||||
| 	 | ||||
| 	Number := Uniform(0, 99); | ||||
| 	Attempts := 1; | ||||
| 	finished := FALSE; | ||||
| 	 | ||||
| 	WHILE NOT finished DO | ||||
| 		WriteLn; | ||||
| 		WriteString("> "); | ||||
| 		ReadInt(guess); | ||||
| 		 | ||||
| 		IF guess = Number THEN | ||||
| 			WriteLn; | ||||
| 			WriteString("You got it right in only "); | ||||
| 			WriteInt(Attempts, 0); | ||||
| 			WriteString(" "); | ||||
| 			IF Attempts = 1 THEN | ||||
| 				WriteString("go"); | ||||
| 			ELSE | ||||
| 				WriteString("goes"); | ||||
| 			END; | ||||
| 			WriteString("!"); | ||||
| 			WriteLn; | ||||
| 			finished := TRUE; | ||||
| 		ELSIF guess < Number THEN | ||||
| 			WriteLn; | ||||
| 			WriteString("Try a bit higher."); | ||||
| 			WriteLn; | ||||
| 		ELSIF guess > Number THEN | ||||
| 			WriteLn; | ||||
| 			WriteString("Try a bit lower."); | ||||
| 			WriteLn; | ||||
| 		END; | ||||
| 		 | ||||
| 		Attempts := Attempts + 1; | ||||
| 	END; | ||||
| END game; | ||||
| 
 | ||||
| VAR | ||||
| 	finished : BOOLEAN; | ||||
| BEGIN | ||||
| 	WriteLn; | ||||
| 	WriteString("Hi there! Before we start, what is your name?"); | ||||
| 	WriteLn; | ||||
| 	reads; | ||||
| 	WriteLn; | ||||
| 	WriteString("Hello, "); | ||||
| 	WriteString(buffer); | ||||
| 	WriteString("! "); | ||||
| 	 | ||||
| 	finished := FALSE; | ||||
| 	WHILE NOT finished DO | ||||
| 		game; | ||||
| 		WriteLn; | ||||
| 		WriteString("Would you like another go?"); | ||||
| 		WriteLn; | ||||
| 		reads; | ||||
| 		 | ||||
| 		IF (buffer[0] = 'n') OR (buffer[0] = 'N') THEN | ||||
| 			finished := TRUE; | ||||
| 			WriteLn; | ||||
| 			WriteString("Thanks for playing --- goodbye!"); | ||||
| 			WriteLn; | ||||
| 		ELSE | ||||
| 			WriteLn; | ||||
| 			WriteString("Excellent! "); | ||||
| 		END; | ||||
| 	END; | ||||
| END HiLo. | ||||
							
								
								
									
										131
									
								
								examples/hilo.ocm
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										131
									
								
								examples/hilo.ocm
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,131 @@ | |||
| # | ||||
| -- $Source$ | ||||
| -- $State$ | ||||
| -- $Revision$ | ||||
| 
 | ||||
| #include "dec.ocm" | ||||
| 
 | ||||
| -- General utilities: read and write strings. | ||||
| 
 | ||||
| proc puts(value s[]) = | ||||
|   seq i = [1 for s[byte 0]] | ||||
|     output ! s[byte i] | ||||
| : | ||||
| 
 | ||||
| proc gets(var s[]) = | ||||
|   var length, finished, c: | ||||
|   seq | ||||
|     finished := false | ||||
|     length := 0 | ||||
|      | ||||
|     while not finished | ||||
|       seq | ||||
|         input ? c | ||||
|         if | ||||
|           c = 10 | ||||
|             finished := true | ||||
|           true | ||||
|             seq | ||||
|               length := length + 1 | ||||
|               s[byte length] := c | ||||
|      | ||||
|     s[byte 0] := length | ||||
| : | ||||
| 
 | ||||
| -- Our random number generator. | ||||
| 
 | ||||
| var seed: | ||||
| proc randomise(value s) = | ||||
|   seq | ||||
|     seed := s | ||||
| : | ||||
| 
 | ||||
| proc random(value range, var result) = | ||||
|   seq | ||||
|     seed := (20077 * seed) + 12345 | ||||
|     if  | ||||
|       seed < 0 | ||||
|         seed := -seed | ||||
|       true | ||||
|         skip | ||||
|     result := seed \ range | ||||
| : | ||||
| 
 | ||||
| -- Does the reading-in-the-name thing. | ||||
| 
 | ||||
| proc getname = | ||||
|   var seed, buffer[128]: | ||||
|   seq | ||||
|     puts("*nHi there! Before we start, what is your name?*n") | ||||
|     puts("> ") | ||||
|     gets(buffer) | ||||
|      | ||||
|     seed := 0 | ||||
|     seq i = [1 for buffer[byte 0]] | ||||
|       seed := seed + buffer[byte i] | ||||
|     randomise(seed) | ||||
| 
 | ||||
|     puts("*nHello, ") | ||||
|     puts(buffer) | ||||
|     puts("! ") | ||||
| : | ||||
| 
 | ||||
| -- Plays a single game. | ||||
| 
 | ||||
| proc game = | ||||
|   var Number, Attempts, finished, guess: | ||||
|   seq | ||||
|     puts("See if you can guess my number.*n") | ||||
|     random(100, Number) | ||||
|     Attempts := 1 | ||||
|     finished := false | ||||
|     while not finished | ||||
|       seq | ||||
|         puts("*n> ") | ||||
|         var c: | ||||
|           seq | ||||
|             c := '*s' | ||||
|             decin(input, guess, c) | ||||
|          | ||||
|         if | ||||
|           guess = Number | ||||
|             seq | ||||
|               puts("*nYou got it right in only ") | ||||
|               decout(output, Attempts, 0) | ||||
|               puts(" ") | ||||
|               if | ||||
|                 Attempts = 1 | ||||
|                   puts("go") | ||||
|                 true | ||||
|                   puts("goes") | ||||
|               puts("!*n") | ||||
|               finished := true | ||||
|            | ||||
|           guess < Number | ||||
|             puts("*nTry a bit higher.*n") | ||||
|           guess > Number | ||||
|             puts("*nTry a bit lower.*n") | ||||
|         Attempts := Attempts + 1 | ||||
| : | ||||
| 
 | ||||
| var finished, buffer[128]: | ||||
| seq | ||||
|   output ! TEXT | ||||
|   getname | ||||
|    | ||||
|   finished := false | ||||
|   while not finished | ||||
|     seq | ||||
|       game | ||||
|        | ||||
|       puts("*nWould you like another go?*n") | ||||
|       puts("> ") | ||||
|       gets(buffer) | ||||
|       if | ||||
|         (buffer[byte 1] = 'n') or (buffer[byte 1] = 'N') | ||||
|           seq | ||||
|             finished := true | ||||
|             puts("*nThanks for playing --- goodbye!*n") | ||||
|         true | ||||
|           puts("*nExcellent! ") | ||||
|    | ||||
							
								
								
									
										167
									
								
								examples/hilo.p
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										167
									
								
								examples/hilo.p
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,167 @@ | |||
| (* $Source$ | ||||
|  * $State$ | ||||
|  * $Revision$ | ||||
|  *) | ||||
| 
 | ||||
| (*$U+ --- enables underscores in identifiers *) | ||||
| 
 | ||||
| program hilo(input, output); | ||||
| 
 | ||||
| type | ||||
| 	string = array [0..255] of char; | ||||
| 	 | ||||
| var | ||||
| 	playing : Boolean; | ||||
| 	seed : integer; | ||||
| 	 | ||||
| { This version of Pascal seems to have no random number generator I can find, | ||||
|   so we have to implement our own here. This is a hacked up and probably | ||||
|   broken version of the C library generator. } | ||||
| 
 | ||||
| procedure randomise(s : integer); | ||||
| 	begin | ||||
| 		seed := s; | ||||
| 	end; | ||||
| 	 | ||||
| function random(range : integer) : integer; | ||||
| 	begin | ||||
| 		seed := (20077 * seed + 12345); | ||||
| 		random := seed mod range; | ||||
| 	end; | ||||
| 
 | ||||
| { Pascal doesn't provide string input, so we interface to the _read() syscall | ||||
|   and do it manually. } | ||||
|    | ||||
| function _read(fd : integer; var buffer : char; count : integer) : integer; | ||||
| 	extern; | ||||
| 
 | ||||
| function readchar : char; | ||||
| 	var | ||||
| 		c : char; | ||||
| 		dummy : integer; | ||||
| 		 | ||||
| 	begin | ||||
| 		c := chr(0); | ||||
| 		dummy := _read(0, c, 1); | ||||
| 		readchar := c; | ||||
| 	end; | ||||
| 	 | ||||
| procedure readstring(var buffer : string; var length : integer); | ||||
| 	var | ||||
| 		finished : Boolean; | ||||
| 		c : char; | ||||
| 		 | ||||
| 	begin | ||||
| 		write('> '); | ||||
| 		 | ||||
| 		length := 0; | ||||
| 		finished := FALSE; | ||||
| 		seed := 0; | ||||
| 		while not finished do | ||||
| 			begin | ||||
| 				c := readchar; | ||||
| 				if (ord(c) = 10) then | ||||
| 					finished := true | ||||
| 				else | ||||
| 					begin | ||||
| 						buffer[length] := c; | ||||
| 						length := length + 1; | ||||
| 					end | ||||
| 			end; | ||||
| 	end; | ||||
| 
 | ||||
| procedure getname; | ||||
| 	var | ||||
| 		name : string; | ||||
| 		namelen : integer; | ||||
| 		i : integer; | ||||
| 		seed : integer; | ||||
| 		 | ||||
| 	begin | ||||
| 		writeln; | ||||
| 		writeln('Hi there! Before we start, what is your name?'); | ||||
| 		writeln; | ||||
| 		readstring(name, namelen); | ||||
| 		writeln; | ||||
| 		write('Hello, '); | ||||
| 		 | ||||
| 		seed := 0; | ||||
| 		for i := 0 to (namelen-1) do | ||||
| 			begin | ||||
| 				write(name[i]); | ||||
| 				seed := seed + ord(name[i]); | ||||
| 			end; | ||||
| 			 | ||||
| 		randomise(seed); | ||||
| 		write('! '); | ||||
| 	end; | ||||
| 	 | ||||
| procedure game; | ||||
| 	var | ||||
| 		Number : integer; | ||||
| 		Attempts : integer; | ||||
| 		guess : integer; | ||||
| 		 | ||||
| 	begin | ||||
| 		writeln('See if you can guess my number.'); | ||||
| 		Number := random(100); | ||||
| 		Attempts := 0; | ||||
| 		 | ||||
| 		guess := -1; | ||||
| 		while guess <> Number do | ||||
| 			begin | ||||
| 				Attempts := Attempts + 1; | ||||
| 				write('> '); | ||||
| 				readln(guess); | ||||
| 			 | ||||
| 				if guess < Number then | ||||
| 					begin | ||||
| 						writeln; | ||||
| 						writeln('Try a bit higher.'); | ||||
| 					end; | ||||
| 			 | ||||
| 				if guess > Number then | ||||
| 					begin | ||||
| 						writeln; | ||||
| 						writeln('Try a bit lower.'); | ||||
| 					end; | ||||
| 			end; | ||||
| 			 | ||||
| 		writeln; | ||||
| 		write('You got it right in only ', Attempts:0, ' '); | ||||
| 		if Attempts = 1 then | ||||
| 			write('go') | ||||
| 		else | ||||
| 			write('goes'); | ||||
| 		writeln('!'); | ||||
| 	end; | ||||
| 	 | ||||
| function question: Boolean; | ||||
| 	var | ||||
| 		response: char; | ||||
| 		 | ||||
| 	begin | ||||
| 		write('> '); | ||||
| 		readln(response); | ||||
| 		 | ||||
| 		question := not ((response = 'n') or (response = 'N'));  | ||||
| 	end; | ||||
|   | ||||
| begin | ||||
| 	getname; | ||||
| 
 | ||||
| 	playing := TRUE; | ||||
| 	while playing do | ||||
| 		begin | ||||
| 			game; | ||||
| 			writeln; | ||||
| 			writeln('Would you like another go?'); | ||||
| 			playing := question; | ||||
| 			 | ||||
| 			writeln; | ||||
| 			if playing then | ||||
| 				write('Excellent! ') | ||||
| 			else | ||||
| 				writeln('Thanks for playing --- goodbye!'); | ||||
| 		end; | ||||
| end. | ||||
							
								
								
									
										1047
									
								
								examples/paranoia.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1047
									
								
								examples/paranoia.c
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
		Loading…
	
	Add table
		
		Reference in a new issue