Add more operator overloads + tests (#55)

* operator '-'
* operator '*'
* operator '/'
* operator '%'
* operator '&'
* operator '|'
* operator '^'
This commit is contained in:
Max Graey
2018-03-25 14:14:53 +03:00
committed by Daniel Wirtz
parent 710fcefd72
commit 70d2a0a425
6 changed files with 1988 additions and 7 deletions

View File

@ -2939,7 +2939,17 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.SubI32, 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.fnSubtract;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
}
case TypeKind.ISIZE: {
expr = module.createBinary(
this.options.isWasm64
@ -3020,7 +3030,17 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.MulI32, 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.fnMultiply;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
}
case TypeKind.ISIZE: {
expr = module.createBinary(
this.options.isWasm64
@ -3118,7 +3138,16 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.DivU32, 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.fnDivide;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
expr = module.createBinary(
this.options.isWasm64
? BinaryOp.DivU64
@ -3214,7 +3243,16 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.RemU32, 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.fnFractional;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
expr = module.createBinary(
this.options.isWasm64
? BinaryOp.RemU64
@ -3509,7 +3547,17 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.AndI64, 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.fnBitwiseAnd;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
}
case TypeKind.ISIZE: {
expr = module.createBinary(
this.options.isWasm64
@ -3581,7 +3629,17 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.OrI64, 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.fnBitwiseOr;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
}
case TypeKind.ISIZE: {
expr = module.createBinary(
this.options.isWasm64
@ -3653,7 +3711,17 @@ export class Compiler extends DiagnosticEmitter {
expr = module.createBinary(BinaryOp.XorI64, 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.fnBitwiseXor;
if (operatorName != null) {
expr = this.compileOperatorOverload(classInstance, operatorName, leftExpr, rightExpr);
break;
}
}
// fall-through
}
case TypeKind.ISIZE: {
expr = module.createBinary(
this.options.isWasm64

View File

@ -675,6 +675,34 @@ export class Program extends DiagnosticEmitter {
classPrototype.fnConcat = prototype.simpleName;
break;
}
case "-": {
classPrototype.fnSubtract = prototype.simpleName;
break;
}
case "*": {
classPrototype.fnMultiply = prototype.simpleName;
break;
}
case "/": {
classPrototype.fnDivide = prototype.simpleName;
break;
}
case "%": {
classPrototype.fnFractional = prototype.simpleName;
break;
}
case "&": {
classPrototype.fnBitwiseAnd = prototype.simpleName;
break;
}
case "|": {
classPrototype.fnBitwiseOr = prototype.simpleName;
break;
}
case "^": {
classPrototype.fnBitwiseXor = prototype.simpleName;
break;
}
case "==": {
classPrototype.fnEquals = prototype.simpleName;
break;
@ -2833,6 +2861,20 @@ export class ClassPrototype extends Element {
fnIndexedSet: string | null = null;
/** Overloaded concatenation method, if any. */
fnConcat: string | null = null;
/** Overloaded subtraction method, if any. */
fnSubtract: string | null = null;
/** Overloaded multiply method, if any. */
fnMultiply: string | null = null;
/** Overloaded divide method, if any. */
fnDivide: string | null = null;
/** Overloaded fractional method, if any. */
fnFractional: string | null = null;
/** Overloaded bitwise and method, if any. */
fnBitwiseAnd: string | null = null;
/** Overloaded bitwise or method, if any. */
fnBitwiseOr: string | null = null;
/** Overloaded bitwise xor method, if any. */
fnBitwiseXor: string | null = null;
/** Overloaded equality comparison method, if any. */
fnEquals: string | null = null;