gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gm2] (no subject)


From: Abi Lover
Subject: [Gm2] (no subject)
Date: Fri, 03 Jan 2003 12:13:08 +0000

Hi everyone,

I am new to this mailing list. I joined in order to ask a specific question. Is anyone here an expert on C/C++ as well as on Modula-2? My question requirtes a knowledge of both.

In Modula-2 there are a couple of language constructs which enable you set up several co-routines and make them run (or appear to run)concurrently: they are NEWPROCES and TRANSFER. They are also very useful to enable you to mimic some real life situations. Is there anything equivalent to this in C or C++? As far as I know there didn't used to be, but GCC has added many new extensions to C and C++, and I don't know enough about them to know whether this is included. If it is not, how would you implement such a code in C or C++?

I have attached a small program which illustrates the use of this construct on Modula-2. This code was written using the old JPI TopSpeed Modula-2, running on DOS. It was written by our lecturer to illustrate this principle more than 10 years ago, when I was taught M2 at colege for the first time. It is a simple program which draws three boxes on the screen: in the first a star keeps moving back and forth, in the second the text, "Have a nice day," is constantly scrolling, and in the third you can enter text from the keyboard. All three events appear to be hapenning at the same time. Suppose you wanted to translate this code into C or C++, how would you do it?

Thanks for your help


_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE* http://join.msn.com/?page=features/junkmail

MODULE CoDemo;
 
   (* Program to demonstrate the use of Coroutines *)
 
   FROM IO IMPORT KeyPressed, RdKey, WrChar, WrStr;
   FROM Screen IMPORT Clear, Cursor;
   FROM SYSTEM IMPORT NEWPROCESS, TRANSFER;
 
   VAR TimeToGoHome: BOOLEAN;
       WorkSpace1, WorkSpace2, WorkSpace3: ARRAY[0..999] OF BYTE;
       Main, Coroutine1, Coroutine2, Coroutine3: ADDRESS;
 
   PROCEDURE Box(line, column: CARDINAL);
      (* Outputs a box at specified position *)
      VAR count: [0..20];
   BEGIN
      Cursor(line, column);
      WrChar(332C);
      FOR count := 0 TO 19 DO
         WrChar(304C)
      END (*FOR*);
      WrChar(277C);
      Cursor(line+1, column);
      WrChar(263C);
      FOR count := 0 TO 19 DO
         WrChar(' ')
      END (*FOR*);
      WrChar(263C);
      Cursor(line+2, column);
      WrChar(300C);
      FOR count := 0 TO 19 DO
         WrChar(304C)
      END(*FOR*);
      WrChar(331C)
   END Box;
 
   PROCEDURE Cycle;
      (* Cycle a message around in the top box *)
   VAR CyclePos, count: [0..20];
       Message: ARRAY[0..20] OF CHAR;
   BEGIN
      Box(4, 29);
      CyclePos := 0;
      Message := "* HAVE A NICE DAY ! ";
      LOOP
         FOR count := 0 TO 19 DO
            Cursor(5, 30 + count);
            WrChar(Message[ (CyclePos + count) MOD 20 ]);
            TRANSFER(Coroutine1, Main)
         END(*FOR*);
         CyclePos := (CyclePos + 1) MOD 20
         END(*LOOP*)
   END Cycle;
 
   PROCEDURE Star;
      (* Move a star backwards and forwards *)
   VAR StarPos, delay: [0..20];
   BEGIN
      Box(12, 29);
      LOOP
         FOR StarPos := 0 TO 19 DO
            Cursor(13, 30 + StarPos);
            WrChar('*');
            FOR delay := 1 TO 10 DO
               TRANSFER(Coroutine2, Main)
            END(*FOR*);
            Cursor(13, 30 + StarPos);
            WrChar(' ')
         END(*FOR*);
         FOR StarPos := 18 TO 1 BY -1 DO
            Cursor(13, 30 + StarPos);
            WrChar('*');
            FOR delay := 1 TO 10 DO
               TRANSFER(Coroutine2, Main)
            END(*FOR*);
            Cursor(13, 30 + StarPos);
            WrChar(' ')
         END(*FOR*)
      END(*LOOP*)
   END Star;
 
   PROCEDURE Input;
   (* Allow text to be entered and displayed *)
   VAR InputPos: [0..20];
       InKey: CHAR;
   BEGIN
      Cursor(19, 29);
      WrStr("Enter Text (# to End):");
      Box(20, 29);
      InputPos := 0;
      LOOP
         Cursor(21, 30 + InputPos);
         IF KeyPressed() THEN
            InKey := RdKey();
            IF (InKey >= ' ') AND (InKey <= '~')
               AND (InputPos < 19) THEN
               Cursor(21, 30 + InputPos);
               WrChar(InKey);
               INC(InputPos);
 
            ELSE
               Cursor(21, 30);
               WrStr("                    ");
               InputPos := 0
            END(*IF*);
            IF (InKey = '#') THEN
               TimeToGoHome := TRUE
            END(*IF*);
         END(*IF*);
         TRANSFER(Coroutine3, Main)
      END(*LOOP*)
   END Input;
 
 
BEGIN
   TimeToGoHome := FALSE;
   Clear;
   (*process initialisation*)
   NEWPROCESS(Cycle, ADR(WorkSpace1), 1000, Coroutine1);
   NEWPROCESS(Star,  ADR(WorkSpace2), 1000, Coroutine2);
   NEWPROCESS(Input, ADR(WorkSpace3),1000, Coroutine3);
   REPEAT
      TRANSFER(Main, Coroutine1);
      TRANSFER(Main, Coroutine2);
      TRANSFER(Main, Coroutine3);
   UNTIL TimeToGoHome;
END CoDemo.
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]