gm2
[Top][All Lists]
Advanced

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

Re: [Gm2] Use of functions in expressions


From: Andreas Fischlin
Subject: Re: [Gm2] Use of functions in expressions
Date: Sun, 16 Nov 2008 16:12:10 +0100
User-agent: Thunderbird 2.0.0.17 (Macintosh/20080914)

Hi Scott,

Exactly because of such rather messy code did Wirth in his original language defintion NOT allow for such constructs. That some compilers accept such code is actually only due to the ISO standard of Modula-2. Prior to this your first formulation was not legal either and since I am not an adept with the ISO standard, I have no strong opinions about the pros and cons of this ISO extension of the original Modula-2. I'm wary it might complicate matters unnecessarily, something fundamentally at odds with Modual-2. Remember also that the ISO standard is not without difficulties, which makes people like me rather sceptical with respect to its benefits, except for settling on a standard library (but that has little to do with the language itself). In this context see also 10 little Modulans... http://www.scifac.ru.ac.za/cspt/sc22wg13.htm#Ten .

More specifically:

First, please note that I see very little reason to write in Modula-2 such code as you wrote. Function procedures should be used only if they return a well defined object, e.g. a real variable, if they have no side effects and if they can be used in place of a variable in an _expression_ (e.g. think of it as a factor http://cui.unige.ch/db-research/Enseignement/analyseinfo/Modula2/factor.html). Therefore my first question to you whould be: Why do you want to have string expressions? Unless you would have some applications where you really want to multiply a string meaningfully with a number - or something similarly exotic - I see little sense in that. Modula-2 is helping you to stay away from such applications. Conclusion: use a procedure, NOT a function procedure.

Second, when you comment some code by "this works", "this does not work" then it is not clear what you mean from that. You should in general always distinguish between, code that can be compiled, because the compiler accepts it as legal, or whether the code does fail during execution, typically causing a run-time error. From what you write further down I guess that you mean "can be compiled" vs. "can't be compiled".

Thirdly,  Write(tester()[4]);  is a phantasy code clearly in conflict with any Modula-2 syntax I know of. It conflicts with the BNF for a procedure call (see http://cui.unige.ch/db-research/Enseignement/analyseinfo/Modula2/procedure_call.html). Therefore any Modula-2 compiler should reject it as syntactically wrong.

A correct solution to what you try to do would be:

PROCEDURE Tester(VAR s: ARRAY OF CHAR);
BEGIN
    Assign("testing",s);
END Tester;

BEGIN
   Tester(temp); WriteString(temp);
   ...

That is all not very complicated and IMHO quite elegant code. Why trying something different that gives you and compiler builders only trouble?

I recommend you give up on your idea of using a function procedure in relation with strings as illustrated above. Of course, there are some rather rare cases in which it may make sense to work with function procedures with respect to strings. E.g. if such a function returns an opaque string object or a single CHAR. See this module from the 'Dialog Machine' for such procedures:

    http://se-server.ethz.ch/RAMSES/Objects/DM/DMStrings.html#DMStrings         (with Comments)
    http://se-server.ethz.ch/RAMSES/Objects/DM/DM_Quick_Reference.html#DMStrings      (without Comments)

The case of a single CHAR may be interesting in a conversion function such as

    PROCEDURE Upper(ch: CHAR): CHAR;
    BEGIN
        RETURN CAP(ch)  (*  should of course be something more meaningful *)
    END Upper;

and its application

    PROCEDURE StringToUpper(VAR s: ARRAY OF CHAR);
        VAR i,n: INTEGER;
    BEGIN
        n := HIGH(s); WHILE (i<=n) AND (s[i]<>0C) DO s[i] := Upper(s[i]); INC(i) END;
    END StringToUpper;

all not very useful, but I hope illustrative.

In general, please note also, Modula-2 is NOT C and arrays, including strings, should be considered what they are, a structured data type that has first of all nothing to do with  dynamic data as passed by function procedures via the stack or pointers referencing data in the heap. An array is a static data structure with a finite size allocated by the compiler at compile time. It faciliates repetitive operation on its element, which, BTW, can be very complex, i.e. it can be an entire RECORD of a very complex type.

Hope this helps to solve your problems.

Sincerely yours,
Andreas Fischlin
 


Scott Robinson wrote:
Hi all,

I have another Modula-2 language question about function results and using them in expressions.  Specifically, it seems that the language allows you to return an array or a pointer but apparently (at least according to the GM2 compiler) does not allow you to turn around and use an array subscript to access a particular element of the array within an _expression_.  The pointer dereference operator '^' also appears to be illegal after a call to a function.

A specific example:

MODULE test1;

FROM InOut IMPORT WriteString, Write, WriteLn;

TYPE
 tArr = ARRAY [0..9] OF CHAR;

VAR
 temp : tArr;

PROCEDURE tester() : tArr;
BEGIN
 RETURN "testing";
END tester;

BEGIN

 (* This works *)
 WriteString("The 5th character returned is ");
 temp := tester();
 Write(temp[4]);
 WriteLn;

 (* This does not work *)
 WriteString("The 5th character returned is ");
 Write(tester()[4]);
 WriteLn;

END test1.

GM2 gives the following for the above:

# gm2 -c test1.mod
test1.mod:26:3: error: syntax error, found `['
test1.mod:30:1: error: compilation failed

I'm wondering if the Modula-2 ISO standard specifically disallows that syntax?  And, if anyone has access to other Modula-2 compilers, do those compilers accept the above?

Thanks,
Scott



_______________________________________________
gm2 mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/gm2

--
________________________________________________________________________
Dr. Andreas Fischlin, Ph.D., Group Director

Terrestrial Systems Ecology
Institute of Integrative Biology: Ecology, Evolution, Infectious Disease
Department of Environmental Sciences, ETH Zurich

Address:
ETH Zurich, CHN E21.1
8092 Zurich, Switzerland

Phone: +41 44 633-6090 / Fax: +41 44 633-1136
http://www.sysecol.ethz.ch/Staff/af/
http://www.sysecol.ethz.ch/

     _/_/_/ _/_/_/ _/  _/
    _/       _/   _/  _/   Eidgenoessische Technische Hochschule Zuerich
   _/_/_/   _/   _/_/_/   Swiss Federal Institute of Technology Zurich
  _/       _/   _/  _/   Ecole polytechnique federale de Zurich
 _/_/_/   _/   _/  _/   Politecnico federale de Zurigo

             Make it as simple as possible, but distrust it!
________________________________________________________________________
 

reply via email to

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