Download The Grace Programming Language Draft Specification

Transcript
Revision 1261 committed on 2013-08-14 00:06:00Z by kim
17
(1 + 2) ∗ 3
// evaluates to 9
1+2∗3
// evaluates to 7
1 +∗+ 4 −∗− 4 // syntax error
Named method requests without arguments bind more tightly than operator method requests. The following examples show the Grace expressions
on the left, and the parse on the right.
Examples
1 + 2.i
(a ∗ a) + (b ∗ b).sqrt
((a ∗ a) + (b ∗ b)).sqrt
a∗a+b∗b
a+b+c
a−b−c
8.4
1 + (2.i)
(a ∗ a) + ((b ∗b).sqrt)
((a ∗ a) + (b ∗b)).sqrt
(a ∗ a) + (b ∗b)
(a + b) + c
(a − b) − c
Unary Prefix Operator Requests
Grace supports unary methods named by operator symbols that precede the
explicit receiver. (Since binary operator methods must also have an explicit
receiver, there is no syntactic ambiguity.)
Prefix operators bind less tightly than other method requests, and therefore need parenthesis to disambiguate.
Andrew IThis seems backwards — in
math prefix operators bind more tightly. The statement above is also a non sequitur.J
Examples
−3 + 4
(−b).squared
−(b.squared)
− b.squared // parses as −(b.squared)
status.ok := !engine.isOnFire && wings.areAttached && isOnCourse
Andrew IBy the above rule, we are OK only if the wings have fallen off and we are
off-course!J
8.5
Accessing Operator Requests
Grace supports operators [. . . ] and [. . . ]:=, which can be defined in libraries,
e.g., for indexing and modifying collections.