fix type annotation for comparison expressions

This commit is contained in:
2020-01-27 16:57:09 +01:00
parent 86468ea740
commit fa8904e0f5
3 changed files with 48 additions and 18 deletions

View File

@@ -225,8 +225,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
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<Node> {
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<Node> {
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<Node> {
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<Node> {
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<Node> {
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;
}

View File

@@ -1,11 +1,11 @@
#include <stdbool.h>
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);

View File

@@ -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);
}