Re: Expressions versus the value they represent

From: Neville Dempsey <nevillednz_at_gmail.com>
Date: Wed, 21 Apr 2010 07:25:03 -0700 (PDT)
Message-ID: <80500607-388d-432d-a6fd-decb085eceda_at_m25g2000prj.googlegroups.com>


On Apr 21, 8:39 am, r..._at_raampje.lan (Reinier Post) wrote:
> David BL wrote:
> >E.g. In C++
> > int x = 10;
> > int* p = &x;

In C: There is the necessity of sprinkling "salt" and "pepper" over C code in the form of the "*" and "&" operators in order to get the dereferencing "just right". See code specimens included below for a comparison....

Algol68 has endeavored to minimise the condiments in the actual code, and - typically - automatically dereferences "pointers" to the actual data.

But A68 still needs some condiments, eg Compare the specimen code extracts where a "pointer" is dereferenced for assignment. C: ***r = 666; /* C needs to be instructed how to dereference to the get the right outcome. */ A68: REF INT(r):=666; ¢ In this case Algol68 needs to be told what outcome "type" is required for the LHS ¢

[ Imagine doing this in C: *(int*)r = 666; /* a big contrast with no warning msg! */ ]

Ironically GCC actually knows what type is required and warns the coder if the "***r=10" is incorrectly coded as "r=10". But things can get spicy in C if there is not enough salt. :-) eg:

> is.c:9: warning: assignment makes pointer from integer without a cast;
THEN "Segmentation fault" when binary is run.

In A68, if the "REF INT(" condiment is missing on the assignment then the coder would get the following "type syntax error" message, eg:   r := 666;
> 8: INT cannot be coerced to REF REF REF INT in a strong-unit

Code specimens with output:
code: - - - - 8>< - - - - - - - -
$ cat is.c
#include <stdio.h>
main(){
  int x;

  int *p = &x;
  int **q = &p;
  int ***r = &q;

  x=10;
  ***r = 666;

  if(*p   == x  ) printf("*p == x\n");
  if(**q  == x  ) printf("**q == x\n");
  if(***r == x  ) printf("***r == x\n");
  if(**r  == &x ) printf("**r is x\n");

}

output: - - - - 8>< - - - - - - - -
$ gcc is.c -o is; ./is

*p == x
**q == x
***r == x
**r is x

code: - - - - 8>< - - - - - - - -
$ cat ./is.a68
main:(
  INT x;
  REF INT p := x;
  REF REF INT q := p;
  REF REF REF INT r := q;

  x:=10; ¢ OK ¢
  REF INT(r) := 666; ¢ dereference required ¢

  IF p = x  THEN printf($"p = x"l$) FI;
  IF q = x  THEN printf($"q = x"l$) FI;
  IF r = x  THEN printf($"r = x"l$) FI;

  IF r IS x THEN printf($"r IS x"l$) FI
)

output: - - - - 8>< - - - - - - - -
$ a68g ./is.a68

p = x
q = x
r = x

r IS x
--
To download Linux's Algol68 Compiler, Interpreter & Runtime:
* http://sourceforge.net/projects/algol68
Received on Wed Apr 21 2010 - 16:25:03 CEST

Original text of this message