Skip to content

Commit

Permalink
making update operators (e.g. +=) first-class functions. for issue #249
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Dec 8, 2011
1 parent dde9519 commit e99215c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
19 changes: 19 additions & 0 deletions j/operators.j
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ min(x, y) = x < y ? x : y

## definitions providing basic traits of arithmetic operators ##

+=(x,y) = x+y
-=(x,y) = x-y
*=(x,y) = x*y
.*=(x,y) = x.*y
/=(x,y) = x/y
./=(x,y) = x./y
//=(x,y) = x//y
\=(x,y) = x\y
.\=(x,y) = x.\y
^=(x,y) = x^y
.^=(x,y) = x.^y
%=(x,y) = x%y
|=(x,y) = x|y
&=(x,y) = x&y
$=(x,y) = x$y
<<=(x,y) = x<<y
>>=(x,y) = x>>y
>>>=(x,y) = x>>>y

+() = 0
*() = 1
&() = error("zero-argument & is ambiguous")
Expand Down
41 changes: 21 additions & 20 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@

(define (expand-update-operator op lhs rhs)
(let ((e (remove-argument-side-effects lhs)))
`(block ,@(cdr e) (= ,(car e) (call ,op ,(car e) ,rhs)))))
`(block ,@(cdr e)
(= ,(car e) (call ,op ,(car e) ,rhs)))))

; (a > b > c) => (&& (call > a b) (call > b c))
(define (expand-compare-chain e)
Expand Down Expand Up @@ -866,25 +867,25 @@
,body))))))

; update operators
(pattern-lambda (+= a b) (expand-update-operator '+ a b))
(pattern-lambda (-= a b) (expand-update-operator '- a b))
(pattern-lambda (*= a b) (expand-update-operator '* a b))
(pattern-lambda (.*= a b) (expand-update-operator '.* a b))
(pattern-lambda (/= a b) (expand-update-operator '/ a b))
(pattern-lambda (./= a b) (expand-update-operator './ a b))
(pattern-lambda (//= a b) (expand-update-operator '// a b))
(pattern-lambda (.//= a b) (expand-update-operator '.// a b))
(pattern-lambda (|\\=| a b) (expand-update-operator '|\\| a b))
(pattern-lambda (|.\\=| a b) (expand-update-operator '|.\\| a b))
(pattern-lambda (^= a b) (expand-update-operator '^ a b))
(pattern-lambda (.^= a b) (expand-update-operator '.^ a b))
(pattern-lambda (%= a b) (expand-update-operator '% a b))
(pattern-lambda (|\|=| a b) (expand-update-operator '|\|| a b))
(pattern-lambda (&= a b) (expand-update-operator '& a b))
(pattern-lambda ($= a b) (expand-update-operator '$ a b))
(pattern-lambda (<<= a b) (expand-update-operator '<< a b))
(pattern-lambda (>>= a b) (expand-update-operator '>> a b))
(pattern-lambda (>>>= a b) (expand-update-operator '>>> a b))
(pattern-lambda (+= a b) (expand-update-operator '+= a b))
(pattern-lambda (-= a b) (expand-update-operator '-= a b))
(pattern-lambda (*= a b) (expand-update-operator '*= a b))
(pattern-lambda (.*= a b) (expand-update-operator '.*= a b))
(pattern-lambda (/= a b) (expand-update-operator '/= a b))
(pattern-lambda (./= a b) (expand-update-operator './= a b))
(pattern-lambda (//= a b) (expand-update-operator '//= a b))
(pattern-lambda (.//= a b) (expand-update-operator './/= a b))
(pattern-lambda (|\\=| a b) (expand-update-operator '|\\=| a b))
(pattern-lambda (|.\\=| a b) (expand-update-operator '|.\\=| a b))
(pattern-lambda (^= a b) (expand-update-operator '^= a b))
(pattern-lambda (.^= a b) (expand-update-operator '.^= a b))
(pattern-lambda (%= a b) (expand-update-operator '%= a b))
(pattern-lambda (|\|=| a b) (expand-update-operator '|\|=| a b))
(pattern-lambda (&= a b) (expand-update-operator '&= a b))
(pattern-lambda ($= a b) (expand-update-operator '$= a b))
(pattern-lambda (<<= a b) (expand-update-operator '<<= a b))
(pattern-lambda (>>= a b) (expand-update-operator '>>= a b))
(pattern-lambda (>>>= a b) (expand-update-operator '>>>= a b))

;; colon
;;(pattern-lambda (: a (-/ :)) `(call (top RangeFrom) ,a 1))
Expand Down

0 comments on commit e99215c

Please sign in to comment.