From fa8904e0f5a0a8b07ccdebabfa4b744b28363922 Mon Sep 17 00:00:00 2001 From: nitrix Date: Mon, 27 Jan 2020 16:57:09 +0100 Subject: [PATCH] fix type annotation for comparison expressions --- .../hsrm/compiler/Klang/ContextAnalysis.java | 42 ++++++++++++++++--- src/test/comparison/comparison.h | 12 +++--- src/test/test.k | 12 +++--- 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java b/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java index 7ab4084..85deb94 100644 --- a/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java +++ b/src/main/java/de/hsrm/compiler/Klang/ContextAnalysis.java @@ -225,8 +225,13 @@ public class ContextAnalysis extends KlangBaseVisitor { public Node visitEqualityExpression(KlangParser.EqualityExpressionContext ctx) { Node lhs = this.visit(ctx.lhs); Node rhs = this.visit(ctx.rhs); + + if (lhs.type != Type.getIntegerType() || rhs.type != Type.getIntegerType()) { + throw new RuntimeException("Both operants of this expression have to be a number"); + } + EqualityExpression result = new EqualityExpression((Expression) lhs, (Expression) rhs); - result.type = lhs.type.combine(rhs.type); + result.type = Type.getBooleanType(); return result; } @@ -234,8 +239,13 @@ public class ContextAnalysis extends KlangBaseVisitor { public Node visitNotEqualityExpression(KlangParser.NotEqualityExpressionContext ctx) { Node lhs = this.visit(ctx.lhs); Node rhs = this.visit(ctx.rhs); + + if (lhs.type != Type.getIntegerType() || rhs.type != Type.getIntegerType()) { + throw new RuntimeException("Both operants of this expression have to be a number"); + } + NotEqualityExpression result = new NotEqualityExpression((Expression) lhs, (Expression) rhs); - result.type = lhs.type.combine(rhs.type); + result.type = Type.getBooleanType(); return result; } @@ -243,8 +253,13 @@ public class ContextAnalysis extends KlangBaseVisitor { public Node visitLessThanExpression(KlangParser.LessThanExpressionContext ctx) { Node lhs = this.visit(ctx.lhs); Node rhs = this.visit(ctx.rhs); + + if (lhs.type != Type.getIntegerType() || rhs.type != Type.getIntegerType()) { + throw new RuntimeException("Both operants of this expression have to be a number"); + } + LTExpression result = new LTExpression((Expression) lhs, (Expression) rhs); - result.type = lhs.type.combine(rhs.type); + result.type = Type.getBooleanType(); return result; } @@ -252,8 +267,13 @@ public class ContextAnalysis extends KlangBaseVisitor { public Node visitGreaterThanExpression(KlangParser.GreaterThanExpressionContext ctx) { Node lhs = this.visit(ctx.lhs); Node rhs = this.visit(ctx.rhs); + + if (lhs.type != Type.getIntegerType() || rhs.type != Type.getIntegerType()) { + throw new RuntimeException("Both operants of this expression have to be a number"); + } + GTExpression result = new GTExpression((Expression) lhs, (Expression) rhs); - result.type = lhs.type.combine(rhs.type); + result.type = Type.getBooleanType(); return result; } @@ -261,8 +281,13 @@ public class ContextAnalysis extends KlangBaseVisitor { public Node visitLessThanOrEqualToExpression(KlangParser.LessThanOrEqualToExpressionContext ctx) { Node lhs = this.visit(ctx.lhs); Node rhs = this.visit(ctx.rhs); + + if (lhs.type != Type.getIntegerType() || rhs.type != Type.getIntegerType()) { + throw new RuntimeException("Both operants of this expression have to be a number"); + } + LTEExpression result = new LTEExpression((Expression) lhs, (Expression) rhs); - result.type = lhs.type.combine(rhs.type); + result.type = Type.getBooleanType(); return result; } @@ -270,8 +295,13 @@ public class ContextAnalysis extends KlangBaseVisitor { public Node visitGreaterThanOrEqualToExpression(KlangParser.GreaterThanOrEqualToExpressionContext ctx) { Node lhs = this.visit(ctx.lhs); Node rhs = this.visit(ctx.rhs); + + if (lhs.type != Type.getIntegerType() || rhs.type != Type.getIntegerType()) { + throw new RuntimeException("Both operants of this expression have to be a number"); + } + GTEExpression result = new GTEExpression((Expression) lhs, (Expression) rhs); - result.type = lhs.type.combine(rhs.type); + result.type = Type.getBooleanType(); return result; } diff --git a/src/test/comparison/comparison.h b/src/test/comparison/comparison.h index f5a9566..2030b9f 100644 --- a/src/test/comparison/comparison.h +++ b/src/test/comparison/comparison.h @@ -1,11 +1,11 @@ #include -int eq(int x, int y); -int neq(int x, int y); -int lt(int x, int y); -int lte(int x, int y); -int gt(int x, int y); -int gte(int x, int y); +bool eq(int x, int y); +bool neq(int x, int y); +bool lt(int x, int y); +bool lte(int x, int y); +bool gt(int x, int y); +bool gte(int x, int y); bool and(bool a, bool b); bool or(bool a, bool b); diff --git a/src/test/test.k b/src/test/test.k index bdc5c93..a7d1603 100644 --- a/src/test/test.k +++ b/src/test/test.k @@ -109,27 +109,27 @@ function fac(x: int): int { return 1; } -function eq(x: int, y: int): int { +function eq(x: int, y: int): bool { return (x == y); } -function neq(x: int, y: int): int { +function neq(x: int, y: int): bool { return (x != y); } -function lt(x: int, y: int): int { +function lt(x: int, y: int): bool { return (x < y); } -function lte(x: int, y: int): int { +function lte(x: int, y: int): bool { return (x <= y); } -function gt(x: int, y: int): int { +function gt(x: int, y: int): bool { return (x > y); } -function gte(x: int, y: int): int { +function gte(x: int, y: int): bool { return (x >= y); }