Newsgroups: comp.lang.smalltalk
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!math.ohio-state.edu!cs.utexas.edu!utnut!nott!cunews!dbuck
From: dbuck@superior.carleton.ca (Dave Buck)
Subject: Re: Evaluation order
Message-ID: <D3B3H7.Fwv@cunews.carleton.ca>
Sender: news@cunews.carleton.ca (News Administrator)
Organization: Carleton University, Ottawa, Canada
References: <sf2cef6f.007@BYUH.EDU> <3gmobl$7hf@acsnews.uswc.uswest.com>
Date: Wed, 1 Feb 1995 05:34:18 GMT
Lines: 57

In article <3gmobl$7hf@acsnews.uswc.uswest.com>,
Martin McClure <martin@is.com> wrote:
>In article <sf2cef6f.007@BYUH.EDU>  Chris Jones writes:
>> Question:  What is the true order of expression evaluation in  
>Smalltalk/V?
>> 
>
><...> 
>
>> Example:  1 + 2 +3 factorial
>> Evaluates:  (1 + 2) + 3 factorial
>> 
>> Rather than:  1 + 2 + (3 factorial) as suggested in the Digitalk Manual.
>> 
>Interesting point. I don't have a running /V system handy, but in VW2.0 I  
>tried compiling
>
>   self halt.
>   1 + 2 + 3 factorial.
>
>and got this compiled method:
>[clipped]
>Or, in postfix (ignoring the halt, which was for convenience), 
>"1 2 + 3 factorial +"

I believe all the Smalltalk compilers work the same way.  They
evaluate left-to-right and only use precidence rules when they need
to.  I would hate to imagine the compiled bytecodes if this wasn't the
case.  Where would it put the results of the unary messages until it
got around to running the binary messages?  So, if you had:

   1 + 2 + 3 factorial between: 3 + 4 squared and: 5 + 6

The correct order of evaluation is:
   1 + 2 --> 3
   3 factorial   --> 6
   3 + 6 --> 9
   4 squared  --> 16
   3 + 16  --> 19
   5 + 6 --> 11
   9 between: 19 and: 11 --> false

The book may say that unary messages are performed first, but that's
not exactly correct.  Unary messages will only be performed before
binary messages when the compiler realizes that it has a choice to
make.  Until that time, evaluation proceeds left to right.

David Buck
dbuck@ccs.carleton.ca

_________________________________
| David K. Buck                 |
| dbuck@ccs.carleton.ca         |
| The Object People             |
|_______________________________|
 

