🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #4754

CONST structure containing a pointer to a PROCEDURE?

#4754
From: Daniel A. Glasser
Date: Thu, 06 Apr 1989 04:30
65 lines
2313 bytes
I need to do the following in Modula-2 (JPI on the PC, specificly) --

/* Begin C code example */

typedef struct entry_str {
    unsigned char es_tag;
    int (*es_action)();
    short es_speed;
} entry_t;

extern int go_up(), go_down(), go_back(), go_forward(), go_right(),
    go_left(), go_home(), stay_put(), default_action;

static entry_t things_to_do[] = {
	{ 'U', go_up, 10 },
	{ 'u', go_up, 1 },
	{ 'D', go_down, 10 },
	{ 'd', go_down, 1 },
	{ 'B', go_back, 10 },
	{ 'b', go_back, 1 },
	{ 'F', go_forward, 10 },
	{ 'f', go_forward, 1 },
	{ 'R', go_right, 10 },
	{ 'r', go_right, 1 },
	{ 'L', go_left, 10 },
	{ 'l', go_left, 1 },
	{ 'H', go_home, 0 },
	{ 'h', go_home, 0 },
	{ 'S', stay_put, 0 },
	{ 's', stay_put, 0 },
	{ '\0', default_action, 0 }
    };

int activate(c)
char c;
{
    entry_t *table = things_to_do;

    while ((table->es_tag != c) && (table->es_tag != '\0'))
	++table;

    return (*table->es_action)(table->es_speed);
}

/* End C code example */

I think I need to set up a structured CONST array with the procedure addresses.
It may be that I really need a readonly VAR which is pre-initialized.
I don't know Modula-2 that well, and can't get the Wirth book locally in
less than about 3 weeks.

None of the tutorials on M2 that I've come across deal with this.

The above example is, in fact, simplified greatly.  It does not come
from my code at all, I just typed it in to this message.  However it
shows what I need to do.  Can anyone supply equivilent Modula-2 code?
I need this information soon.

Thanks in advance --
				Daniel A. Glasser
-- 
 _____________________________________________________________________________
    Daniel A. Glasser                           One of those things that goes
    uwvax!per2!dag                              "BUMP!!!(ouch)" in the night. 
 ---Persoft, Inc.---------465 Science Drive-------Madison, WI 53711-----------
Technical Details
Message-ID: <838@per2.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!ames!ncar!tank!uwvax!per2!dag

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!lll-winken!ames!ncar!tank!uwvax!per2!dag
From: dag@per2.UUCP (Daniel A. Glasser)
Newsgroups: comp.lang.modula2
Subject: CONST structure containing a pointer to a PROCEDURE?
Keywords: CONST initializers PROCEDURE
Message-ID: <838@per2.UUCP>
Date: 6 Apr 89 04:30:52 GMT
Organization: Persoft Inc., Madison, WI
Lines: 65