This site will look much better in a browser that supports web standards, but it is accessible to any browser or Internet device.
The Smalltalk system evaluates messages in an order determined by a set of precedence rules.
These are:
The precedence rules do not always accurately reflect the order of evaluation as implemented by the Smalltalk parser, the software that 'reads' the code. The parser works strictly left to right, but before evaluating a message it looks ahead to the next message to check if it is of a higher precedence than the current message. If the next message is of higher precedence then it gets evaluated before the current message. The precedence tool provided in LearningWorks uses the same rules as the parser and works well as long as brackets are not used. It works reliably for evaluating expressions that are combinations of binary, unary and keywords, but you should avoid using it with bracketed expressions.
Consider the following:
aFrog right position:(aFrog position)
If you follow the precedence rules you will evaluate the bracketed message first. This will lead you to a wrong evaluation. The parser evaluates aFrog right first, since parsing left to right will determine right as the first unary message. If aFrog is a newly created instance with position of 1 then the above expression will set its position to 2 not 1.
Now consider this code:
10 + (10 negated) negated
The precedence tool finds + realises it must look ahead one for a message of higher precedence. In the look ahead it instead finds the brackets and correctly evaluates the contents of those brackets. It then 'forgets' that it still hasn't completed the original search for a message of higher precedence and incorrectly evaluates the binary message (10 + -10) before sending negated to 0. The tool leads you to an incorrect answer of 0. The correct result of the message expression should of course be 20.
Consider some other examples.
(25 + 3 * 4 ) printString
What happens if you try and evaluate 25 + 3 * 4 printString
?
This will cause a system error.
Here is another example.
'The ' , ((25 plus: 3 * 4) + 2) printString , ' Steps'
Go here for some precedence questions and answers.
Next page » Dialog
Previous page « Print-strings