Implement comparision operator overloads (#63)

This commit is contained in:
Max Graey
2018-04-04 01:01:59 +03:00
committed by Daniel Wirtz
parent 5823e35f37
commit 37825fc84d
5 changed files with 844 additions and 34 deletions

View File

@ -2400,7 +2400,16 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.LtU32, leftExpr, rightExpr);
break;
}
case TypeKind.USIZE: { // TODO: check operator overload
case TypeKind.USIZE: { // check operator overload
if (this.currentType.is(TypeFlags.REFERENCE)) {
let classInstance = assert(this.currentType.classReference);
let operatorName = classInstance.prototype.fnLessThan;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
expr = module.createBinary(
this.options.isWasm64
? BinaryOp.LtU64
@ -2479,7 +2488,16 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.GtU32, leftExpr, rightExpr);
break;
}
case TypeKind.USIZE: { // TODO: check operator overload
case TypeKind.USIZE: { // check operator overload
if (this.currentType.is(TypeFlags.REFERENCE)) {
let classInstance = assert(this.currentType.classReference);
let operatorName = classInstance.prototype.fnGreaterThan;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
expr = module.createBinary(
this.options.isWasm64
? BinaryOp.GtU64
@ -2558,7 +2576,16 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.LeU32, leftExpr, rightExpr);
break;
}
case TypeKind.USIZE: { // TODO: check operator overload
case TypeKind.USIZE: { // check operator overload
if (this.currentType.is(TypeFlags.REFERENCE)) {
let classInstance = assert(this.currentType.classReference);
let operatorName = classInstance.prototype.fnLessThanEquals;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
expr = module.createBinary(
this.options.isWasm64
? BinaryOp.LeU64
@ -2637,7 +2664,16 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.GeU32, leftExpr, rightExpr);
break;
}
case TypeKind.USIZE: { // TODO: check operator overload
case TypeKind.USIZE: { // check operator overload
if (this.currentType.is(TypeFlags.REFERENCE)) {
let classInstance = assert(this.currentType.classReference);
let operatorName = classInstance.prototype.fnGreaterThanEquals;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
expr = module.createBinary(
this.options.isWasm64
? BinaryOp.GeU64
@ -2784,7 +2820,17 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.NeI32, leftExpr, rightExpr);
break;
}
case TypeKind.USIZE: // TODO: check operator overload
case TypeKind.USIZE: {// check operator overload
if (this.currentType.is(TypeFlags.REFERENCE)) {
let classInstance = assert(this.currentType.classReference);
let operatorName = classInstance.prototype.fnNotEquals;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
}
case TypeKind.ISIZE: {
expr = module.createBinary(
this.options.isWasm64

View File

@ -733,6 +733,26 @@ export class Program extends DiagnosticEmitter {
classPrototype.fnEquals = prototype.simpleName;
break;
}
case "!=": {
classPrototype.fnNotEquals = prototype.simpleName;
break;
}
case ">": {
classPrototype.fnGreaterThan = prototype.simpleName;
break;
}
case ">=": {
classPrototype.fnGreaterThanEquals = prototype.simpleName;
break;
}
case "<": {
classPrototype.fnLessThan = prototype.simpleName;
break;
}
case "<=": {
classPrototype.fnLessThanEquals = prototype.simpleName;
break;
}
default: {
this.error(
DiagnosticCode.Operation_not_supported,
@ -2944,6 +2964,16 @@ export class ClassPrototype extends Element {
fnBitwiseXor: string | null = null;
/** Overloaded equality comparison method, if any. */
fnEquals: string | null = null;
/** Overloaded non-equality comparison method, if any. */
fnNotEquals: string | null = null;
/** Overloaded greater comparison method, if any. */
fnGreaterThan: string | null = null;
/** Overloaded greater or equal comparison method, if any. */
fnGreaterThanEquals: string | null = null;
/** Overloaded less comparison method, if any. */
fnLessThan: string | null = null;
/** Overloaded less or equal comparison method, if any. */
fnLessThanEquals: string | null = null;
constructor(
program: Program,