[comp.lang.modula2] MetCom

S703975@UWEC.BITNET (ANDREW TREMBLEY) (12/05/89)

Thanks for answering.  My problem is that whenever I try a NEW or a DISPOSE
the compiler barfs.  I assume that this is because it has to be imported
from a module, but I have no idea which one.

I'd much like to be able to use MetCom (it's editor is better than
VMS Edit (EDT) and it's more convenient than using Work and kermiting),
but without NEW and DISPOSE, my ADTs won't work.

Thanks much
Andy

GEROFSKY@UCF1VM.BITNET (Lara Gerofsky) (12/05/89)

The NEW and DISPOSE are supported by ALLOCATE and DEALLOCATE respectively
from the Storage library.

Your welcome,

Lara Gerofsky
GEROFSKY at UCF1VM

Bob_Campbell.ZORRO@GATEWAY.QM.APPLE.COM (Bob Campbell) (12/05/89)

I don't think that MetCom supports NEW or DISPOSE, N. Wirth choice to
delete these features from the language, as they required ALLOCATE, and
DEALLOCATE to be imported from another module.

NEW is ALLOCATE(x,SIZE(x))
DISPOSE is DEALLOCATE(x,SIZE(x))

Also note that TSIZE was also removed from the language, and SIZE was redefined
 to handle both variables and types (most compilers still support TSIZE).

It should not be too hard to write a script to convert them.
s/ALLOCATE(\([~,]*\),SIZE(.*))/NEW(\1)/gp
s/DEALLOCATE(\([~,]*\),SIZE(.*))/DISPOSE(\1)/gp

My vi is a little bit rusty, in MPW it would be:
replace /ALLOCATE\(([option-l,]*)option-r1,SIZE(option-x))/
"NEW(option-r1)"
replace /DEALLOCATE\(([option-l,]*)option-r1,SIZE(option-x))/
"DISPOSE(option-r1)"

(It is a little hard to some the MPW commands using Usenet which does not
have the full Macintosh Character Set).

I don't have a cute trailer**************
Applelink: BOBC (BOBC@Applelink.apple.com)
Quickmail: Bob Campbell@ZORRO (Bob_Campbell.ZORRO@gateway.qm.apple.com)

kherron@MS.UKY.EDU (12/05/89)

(ooh, finally an M-2 question I can answer!)

NEW and DISPOSE, if your compiler has them, will be part of the
SYSTEM module.

I say "if your compiler has them" because Wirth's latest M-2 spec
does not include NEW and DISPOSE--instead, use the ALLOCATE and
DEALLOCATE procedures from STORAGE, along with the SYSTEM procedure
TSIZE, which gives the size of a declared type.  The pseudo-declaration
for TSIZE looks like this:

PROCEDURE TSIZE(<any type>): INTEGER;

Like I said, it's in SYSTEM also.

Kenneth Herron

marti@ethz.UUCP (Robert Marti) (12/06/89)

In article <891204.13351181.096571@UWEC.CP6>, S703975@UWEC.BITNET
(ANDREW TREMBLEY) writes:
> My problem is that whenever I try a NEW or a DISPOSE the compiler barfs.
> I assume that this is because it has to be imported from a module, but
> I have no idea which one.

Let me start with a disclaimer:  I have never used MetCom Modula-2.
I do know a couple of other Modula-2 implementations though, in
particular the MacMETH system on which MetCom M-2 is based, I believe.

The book "Programming in Modula-2, 3rd Ed." by N.Wirth, p.76, states
(emphasis added by me):

    [Pointer variables] are created by a call of an allocation
    procedure, usually available from a library module.  TYPICALLY,
    a module Storage may provide a procedure Allocate.  The state-
    ment  Allocate(p0, SIZE(Node))  then creates a variable of type
    Node and assigns a pointer pointing to that variable to [...] p0.

and, a little later:

    In SOME implementations, the statement  Allocate(p0, SIZE(Node))
    can be abbreviated as  NEW(p0) .

Assuming that this is all true for MetCom M-2 -- which may well not
be the case -- you'd have to import Allocate from Storage anyway,
EVEN WHEN using the abbreviation NEW.  However, it is not entirely
clear which of the two solutions below work:

    FROM Storage IMPORT Allocate;
    VAR p: POINTER TO ... ;
    ...
    NEW(p);

or

    IMPORT Storage;
    VAR p: POINTER TO ... ; 
    ...   
    NEW(p);

Note that in the MacMETH system, there is a module Storage which exports
ALLOCATE (!) and DEALLOCATE and a module System (!) which exports Allocate
and Deallocate.  NEW is not supported.

So, check the documentation to figure out from which module either
Allocate or ALLOCATE is exported.  Then check if NEW is supported.
I hope this helps.
-- 
Robert Marti                      Phone:      +41 1 256 52 36
Institut fur Informationssysteme
ETH-Zentrum                       CSNET/ARPA: marti%inf.ethz.ch@relay.cs.net
CH-8092 Zurich, Switzerland       UUCP:       ...uunet!mcvax!ethz!marti

UK4H@DKAUNI2.BITNET (JAE) (12/12/89)

Hi all! I just wanted, no, I MUST add my 2 cents worth to this one.

Bob Campbell wrote:
> NEW is ALLOCATE(x,SIZE(x))
> DISPOSE is DEALLOCATE(x,SIZE(x))
THIS IS WRONG!!!!
Instead you have to write
  ALLOCATE(x,SIZE(x^));
  DEALLOCATE(x,SIZE(x^));
'^' is the caret (dereferencing 'operator').

I once forgot this little thing, and the error was REALLY hard to find
(I had to do without a debugger, though).

I'm quite sure that Bob just forgot this, because it wont run properly
if you do forget it.

Well, one final note on his vi and MPW scripts(?): I don't think
they are correct, either, 'cause I didn't notice a caret there!
Unfortunately, I have neither access to vi nor to MPW, sigh. So can
some kind soul out there (or you, Bob) please correct it (if anyone
needs it).

                                             bye, JAE

Juergen A. Erhard
eMail: uk4h@dkauni2.bitnet
phone: (GERMANY) 0721/591602
"You know that it's monday when you wake up and it's tuesday."
                                                    Garfield

cmp8118@sys.uea.ac.uk (D.S. Cartwright) (12/13/89)

UK4H@DKAUNI2.BITNET (JAE) writes:

>Bob Campbell wrote:
>> NEW is ALLOCATE(x,SIZE(x))
>> DISPOSE is DEALLOCATE(x,SIZE(x))
>THIS IS WRONG!!!!
>Instead you have to write
>  ALLOCATE(x,SIZE(x^));
>  DEALLOCATE(x,SIZE(x^));
>'^' is the caret (dereferencing 'operator').

The way I do it is thus: 

If you have a type declaration 

	TYPE
		Dave = POINTER TO Fred ;
		Fred = RECORD
			Field1 : ARRAY[0..30] OF INTEGER ;
			Field2 : ARRAY[0..9] OF CHAR ;
			Pointer : Dave ;
		       CEND ;

then	VAR
		Variable : Dave ;

	you can then do

	ALLOCATE (Variable,TSIZE(Fred))

	where ALLOCATE is IMPORTed from STORAGE and TSIZE from SYSTEM (or the
	other way round, I think this is the right way though).

One thing you have to make sure of is that you put TSIZE(Fred) and not, by a
slip of the keyboard, TSIZE(Dave), else you'll be trying to place something of
RECORD type in a space the size of a POINTER. Doesn't work, and it's a sod to
find out why (I know !!!!).

Just a thought .....

	Dave Cartwright,
	    School of Information Systems,
		University of East Anglia,
		    Norwich,
			England.

Note : I am only a student => I am pretty thick really => Don't shout at me too
							  much if I'm wrong.

Bob_Campbell.ZORRO@GATEWAY.QM.APPLE.COM (Bob Campbell) (12/14/89)

How embarrassingI, I should know better then to pass out code with out
testing it.  For the record I don't use MetCom's standalone product, but
instead use a MPW based Modula-2 product which was sold by TML systems and
will soon be licensed thru MetroWorks (the MetCom people).  I understand
that the MPW product (from Apple) is a little bit pricey, but I refuse to
use a development environment which can't be scripted, and does not
support regular expressions.

I don't have a cute trailer**************
Applelink: BOBC (BOBC@Applelink.apple.com)
Quickmail: Bob Campbell@ZORRO (Bob_Campbell.ZORRO@gateway.qm.apple.com)

rsutc@fornax.UUCP (Rick Sutcliffe) (12/14/89)

In article <"89-12-11-21:03:38.02*UK4H"@DKAUNI2.BITNET>, UK4H@DKAUNI2.BITNET (JAE) writes:
> Bob Campbell wrote:
> > NEW is ALLOCATE(x,SIZE(x))
> > DISPOSE is DEALLOCATE(x,SIZE(x))
> THIS IS WRONG!!!!
> Instead you have to write
>   ALLOCATE(x,SIZE(x^));
>   DEALLOCATE(x,SIZE(x^));

Actually, its still not right.  It is inadvisable to say the least to do
SIZE(x^) before the ALLOCATE has been done. What if x is NIL?
Do it this way:
TYPE
  MyThingy = whatever;
  MyPoint = POINTER TO MyThingy;
VAR
  x: MyPoint;

ALLOCATE (x, SIZE (MyThingy))

Rick Sutcliffe

Bob_Campbell.ZORRO@gateway.qm.apple.Com (Bob Campbell) (12/15/89)

For the record the following peice of code works.  I know I tested it this 
time.

TYPE
  MyThingy = RECORD x,y,z : INTEGER END;
  MyPoint = POINTER TO MyThingy;
VAR
  x: MyPoint;
BEGIN
ALLOCATE (x, SIZE (x^));

It is true that x^ might dereference the variable x, but because this
is only one level deep, the dereferencing code is not generated (by any
of the MetCom products).  A more complex expression like x^.y^.z^ might
cause more problems (the MPW compiler does not like this at all, I will
have to see if that can be fixed).   It could be argued that this method
should only be used when you need to convert between NEW and ALLOCATE,
but I can't think of can good reasons that require that to be done more
then once.

The bottom line is that if you don't write your code intending to use NEW, 
then you can write code that works using the actual type instead of 
dereferencing the variable to get it's size.  This code is portable and 
would work with almost any Modula-2 compiler.  As a side note I don't use 
ALLOCATE normally  I call the Macintosh routine NewPtr.

In the last message I said something about "Apple's MPW Product" I was 
talking about the MPW shell.  Apple does not have a Modula-2 product, and 
the only Macintosh Modula-2 compilers that I am aware of are SemperSoft, 
P1 and MetCom (aka MetroWorks).

Applelink: BOBC (BOBC@Applelink.apple.com)
Quickmail: Bob Campbell@ZORRO (Bob_Campbell.ZORRO@gateway.qm.apple.com)