Skip to content

TM006 Binary Operators

Daniel Patterson edited this page May 30, 2013 · 2 revisions

TM006 - Binary Operators

Motivation:

Writing arithmetic like: 4.plus(5.plus(6.times(4))).minus(2) is really unpleasant - much less readable than Racket, and since many of the first programming exercises we do are with arithmetic, this could leave a bad taste early (plus it's just silly). Similarly, it would be nice to be able to write 4 < 5 instead of 4.lessthan(5), for conciseness.

Examples:

(4 + 4 + (6 * 4)) - 2
3 * 4 * 5
4 + (3 - 2)
2 / 4 / 6
3 >= 2
6 == 7

Semantics:

Our intention is to keep the implementation very simple, and for this reason our (potentially controversion) proposal is not to build in precedence to our grammar. We will support homogenous mathematical expressions of any arity, but to combine operators of different types, parenthesis must be used to group the subterms.

We propose a grammar like the folowing (some variants skipped for brevity)

expr: non-binop-expr | plus-binop-expr | minus-binop-expr | equals-binop-expr
non-binop-expr: all expressions in current grammar

plus-binop-expr: non-binop-expr + plus-binop-expr-rest | (expr) + plus-binop-expr-rest
plus-binop-expr-rest: non-binop-expr | (expr) | plus-binop-expr
minus-binop-expr: non-binop-expr + minus-binop-expr-rest | (expr) + minus-binop-expr-rest
minus-binop-expr-rest: non-binop-expr | (expr) | minus-binop-expr

equals-binop-expr: equals-binop-subexpr == equals-binop-subexpr
equals-binop-subexpr: non-binop-expr | (expr)

Each of these would desugar into the existing method calls:

4 + 5 + 6 ===> 4.plus(5).plus(6)

(5 + 5) == 10 ===> 5.plus(5).equals(10)

We will support the following operators in this way:

+  ==> plus
-  ==> minus
*  ==> times
/  ==> divide
>  ==> greaterthan
<  ==> lessthan
>= ==> greaterequal
<= ==> lessequal
== ==> equals
!= ==> not.equals

This proposal does not cover boolean operators (and and or), because they require laziness, and we are still thinking about a general mechanism that (power) users could utilize.

Resolution:

This TM implemented almost as described as of 524d5382ed7f880156c8bb18d4e7b7c1e851265e, with a few parsing bugs related to parenthesis worked out a few commits later. the only significant change was to use <> as not.equals.