From nickle@nickle.org  Sat Mar  1 00:12:22 2003
From: nickle@nickle.org (Keith Packard)
Date: Fri, 28 Feb 2003 16:12:22 -0800
Subject: [Nickle] bugs
In-Reply-To: Your message of "Fri, 28 Feb 2003 21:54:32 GMT."
 <F15jT8S1M1GvE0uY2at0002121e@hotmail.com>
Message-ID: <E18ouc2-0005Ng-00@localhost>

Around 21 o'clock on Feb 28, "Ireland Peddlar" wrote:

> x!==x! is evaluated as x(!=)=x!
> According the the manual it should be evaluated (x!)==(x!)

> Also:
> It should be noted in the manual probably that
> ---x is evaluated as --(-x) instead of -(--x) and nickle chokes on it.
> especially since -++x is evaluated as -(++x) correctly.

These both stem from the separation of lexing from parsing.  I'll note that
GCC has the same trouble with ---x. The lexer picks off the leading '--'
as the decrement operator before finding the extra '-'.

K&R separate lexing from parsing in the C Reference Manual and state that
"the next token is taken to include the longest string of characters which
could possibly constitute a token". This means that the programmer must
ensure that each token is logically separate in the input stream, in this
case whitespace can be used to disambiguate between the two possible token 
streams.

-keith




From nickle@nickle.org  Mon Mar 17 06:41:01 2003
From: nickle@nickle.org (Keith Packard)
Date: Sun, 16 Mar 2003 22:41:01 -0800
Subject: [Nickle] Reference type modifiers
Message-ID: <E18uoIv-0003Sj-00@localhost>

I've added 'reference' types to nickle.  These are functionally identical 
to pointer types except that values of this type implicitly refer to the 
referent rather than to the refererence.  To refer to the reference it
self, prefix the expression with an ampersand (&).  

In other words, you use a value of reference type in the same situations
you would use a value of pointer type except that everywhere you would 
have typed '*p', you type 'r' and everywhere you would have typed 'p', you 
type '&r':

	> void foo (&int j) { j = 7; }
	> void bar (*int k) { *k = 7; }
	> int l;
	> foo (&l);
	> l
	7

These two functions are functionally identical; they accept the same 
values and perform the same function.

	> int i = 12;
	> int j = 100;
	> &int r = &i;			r is a 'reference to int'
	> r				r with no modifiers is the refernt
	12			
	> *int p = &j;			p is a 'pointer to int'
	> p				p with no modifiers is the reference
	&100
	> &r = p			&r is the reference
	&100
	> r = 10			store into r changes the referent
	10
	> *p				*p is the referent
	10

The hope for reference types is to make passing values by reference less
clumsy looking:

    /*
     * Quicksort with random pivot
     */
    public void function qsort (&poly[*] a, bool(poly, poly) gt)
    {
        void function quicksort (int p, int r)
        {
            if (p < r)
            {
                /* swap two array elements */
                void function exchange (int i, int j)
                {
                    poly t = a[i]; a[i] = a[j]; a[j] = t;
                }

                /* partition the array into two pieces and return the pivot */
                int function partition (int p, int r)
                {
                    /* select a random element to pivot */
                    int pivot = p + PRNG::randint(p-r);
                    exchange (pivot, r);
   
                    poly x = a[r];
                    int i = p;
                    for (int j = p; j < r; j++)
                    {
                        if (gt (x, a[j]))
                        {
                            exchange (i, j);
                            i++;
                        }
                    }
                    exchange (i, r);
                    return i;
                }
                
                int q = partition (p, r);
                quicksort (p, q-1);
                quicksort (q+1, r);
            }
        }
    
        quicksort (0, dim(a)-1);
    }

Without reference types, this code would be full of the (*a)[n] 
constructions, or worse the horrid *[ operator invented just for this 
purpose.

The intent is for this to look like reference types in ML or var 
parameters in Pascal.  Note that unlike C++, you don't implicitly get 
references constructed for arguments; this should avoid fun surprises that 
often crop up in C++ programs, but it does mean that you'll need to add & 
in front of arguments to reference-formaled functions.

-keith




