Skip to content

Commit

Permalink
Closes #888 (lucky number?)
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Jul 8, 2015
1 parent 36ae73b commit 2cdd92e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
22 changes: 3 additions & 19 deletions source/rock/middle/BinaryOp.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ BinaryOp: class extends Expression {
// If the left side is an immutable function, fail immediately.
l := lRef as FuncType
if (!(l isClosure)) {
token module params errorHandler onError(InvalidBinaryOverload new(token,
token module params errorHandler onError(InvalidOperatorUse new(token,
"%s is an immutable function. You must not reassign it. (Perhaps you want to use a first-class function instead?)" format(left toString())))
}
}
Expand Down Expand Up @@ -752,20 +752,8 @@ BinaryOp: class extends Expression {
args add(0, fDecl owner getThisDecl())
}

if(args getSize() != 2) {
match (symbol) {
case "-" || "+" || "-=" || "+=" =>
if (args getSize() == 1) {
// correct, but not the right overload type - skip
return 0
} else {
token module params errorHandler onError(InvalidBinaryOverload new(op token,
"Overloads for '%s' operator require 1 or 2 arguments, not %d" format(symbol, args getSize())))
}
case =>
token module params errorHandler onError(InvalidBinaryOverload new(op token,
"Overloads for '%s' operator require 2 arguments, not %d" format(symbol, args getSize())))
}
if (args size < 2) {
return 0 // not the right overload type -- skip
}

opLeft := args get(0)
Expand Down Expand Up @@ -804,10 +792,6 @@ BinaryOp: class extends Expression {

}

InvalidBinaryOverload: class extends Error {
init: super func ~tokenMessage
}

InvalidOperatorUse: class extends Error {
init: super func ~tokenMessage
}
26 changes: 26 additions & 0 deletions source/rock/middle/OperatorDecl.ooc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,31 @@ OperatorDecl: class extends Expression {
}
}

if (symbol != "as") {
numArgs := fDecl args size
if (fDecl owner) {
numArgs += 1
}

if(numArgs != 2) {
match (symbol) {
case "-" || "+" =>
if (numArgs != 1) {
res throwError(InvalidOperatorOverload new(token,
"Overloading '%s' requires 1 or 2 arguments, not %d" format(symbol, numArgs)))

This comment has been minimized.

Copy link
@vendethiel

vendethiel Jul 8, 2015

1 or 2?

This comment has been minimized.

Copy link
@fasterthanlime

fasterthanlime Jul 9, 2015

Author Collaborator

Unary operators!

This comment has been minimized.

Copy link
@vendethiel

vendethiel Jul 9, 2015

Then []='s error should say "2 or 3", right?

This comment has been minimized.

Copy link
@fasterthanlime

fasterthanlime Jul 9, 2015

Author Collaborator

I don't think so, [] requires 2:

operator [] (coll: Collection, index: Int) -> Element {
  coll get(index)
}

And []= requires three:

operator []= (coll: Collection, index: Int, el: Element) {
  coll set(index, el)
}

Whereas - can be implemented for both unary and binary minus:

Vec2: class {
  x, y: Float
  init: func (=x, =y) {}
}

// unary
operator - (v: Vec2) {
  Vec2 new(-v x, - v y)
}

// binary
operator - (v, w: Vec2) {
  Vec2 new(v x - w x, v y - w y)
}

This comment has been minimized.

Copy link
@vendethiel

vendethiel Jul 9, 2015

Then why the outer numArgs!=2?

This comment has been minimized.

Copy link
@fasterthanlime

fasterthanlime Jul 9, 2015

Author Collaborator

Hmmm you're right, that would accept 2 arguments for []=, which is bad.

}
case "[]=" =>
if (numArgs != 3) {
res throwError(InvalidOperatorOverload new(token,
"Overloading '%s' requires 3 arguments, not %d" format(symbol, numArgs)))
}
case =>
res throwError(InvalidOperatorOverload new(token,
"Overloading '%s' requires 2 arguments, not %d" format(symbol, numArgs)))
}
}
}

Response OK
}

Expand Down Expand Up @@ -164,6 +189,7 @@ InvalidOperatorOverload: class extends Error {
init: super func ~tokenMessage
}


OverloadStatus: enum {
TRYAGAIN // operator usage waiting for something else to resolve
REPLACED // operator usage was replaced with a call to an overload
Expand Down
11 changes: 11 additions & 0 deletions test/compiler/operators/check-on-definition.ooc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

//! shouldfail

Vec2: class {
x, y: Int

operator * -> This {
this
}
}

2 changes: 2 additions & 0 deletions test/compiler/operators/overload-order.ooc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

use sam-assert

Point2D: class {
x, y: Int
init: func(=x, =y)
Expand Down

0 comments on commit 2cdd92e

Please sign in to comment.