fix type annotation for comparison expressions
This commit is contained in:
@@ -225,8 +225,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
|||||||
public Node visitEqualityExpression(KlangParser.EqualityExpressionContext ctx) {
|
public Node visitEqualityExpression(KlangParser.EqualityExpressionContext ctx) {
|
||||||
Node lhs = this.visit(ctx.lhs);
|
Node lhs = this.visit(ctx.lhs);
|
||||||
Node rhs = this.visit(ctx.rhs);
|
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);
|
EqualityExpression result = new EqualityExpression((Expression) lhs, (Expression) rhs);
|
||||||
result.type = lhs.type.combine(rhs.type);
|
result.type = Type.getBooleanType();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,8 +239,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
|||||||
public Node visitNotEqualityExpression(KlangParser.NotEqualityExpressionContext ctx) {
|
public Node visitNotEqualityExpression(KlangParser.NotEqualityExpressionContext ctx) {
|
||||||
Node lhs = this.visit(ctx.lhs);
|
Node lhs = this.visit(ctx.lhs);
|
||||||
Node rhs = this.visit(ctx.rhs);
|
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);
|
NotEqualityExpression result = new NotEqualityExpression((Expression) lhs, (Expression) rhs);
|
||||||
result.type = lhs.type.combine(rhs.type);
|
result.type = Type.getBooleanType();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,8 +253,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
|||||||
public Node visitLessThanExpression(KlangParser.LessThanExpressionContext ctx) {
|
public Node visitLessThanExpression(KlangParser.LessThanExpressionContext ctx) {
|
||||||
Node lhs = this.visit(ctx.lhs);
|
Node lhs = this.visit(ctx.lhs);
|
||||||
Node rhs = this.visit(ctx.rhs);
|
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);
|
LTExpression result = new LTExpression((Expression) lhs, (Expression) rhs);
|
||||||
result.type = lhs.type.combine(rhs.type);
|
result.type = Type.getBooleanType();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -252,8 +267,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
|||||||
public Node visitGreaterThanExpression(KlangParser.GreaterThanExpressionContext ctx) {
|
public Node visitGreaterThanExpression(KlangParser.GreaterThanExpressionContext ctx) {
|
||||||
Node lhs = this.visit(ctx.lhs);
|
Node lhs = this.visit(ctx.lhs);
|
||||||
Node rhs = this.visit(ctx.rhs);
|
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);
|
GTExpression result = new GTExpression((Expression) lhs, (Expression) rhs);
|
||||||
result.type = lhs.type.combine(rhs.type);
|
result.type = Type.getBooleanType();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -261,8 +281,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
|||||||
public Node visitLessThanOrEqualToExpression(KlangParser.LessThanOrEqualToExpressionContext ctx) {
|
public Node visitLessThanOrEqualToExpression(KlangParser.LessThanOrEqualToExpressionContext ctx) {
|
||||||
Node lhs = this.visit(ctx.lhs);
|
Node lhs = this.visit(ctx.lhs);
|
||||||
Node rhs = this.visit(ctx.rhs);
|
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);
|
LTEExpression result = new LTEExpression((Expression) lhs, (Expression) rhs);
|
||||||
result.type = lhs.type.combine(rhs.type);
|
result.type = Type.getBooleanType();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,8 +295,13 @@ public class ContextAnalysis extends KlangBaseVisitor<Node> {
|
|||||||
public Node visitGreaterThanOrEqualToExpression(KlangParser.GreaterThanOrEqualToExpressionContext ctx) {
|
public Node visitGreaterThanOrEqualToExpression(KlangParser.GreaterThanOrEqualToExpressionContext ctx) {
|
||||||
Node lhs = this.visit(ctx.lhs);
|
Node lhs = this.visit(ctx.lhs);
|
||||||
Node rhs = this.visit(ctx.rhs);
|
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);
|
GTEExpression result = new GTEExpression((Expression) lhs, (Expression) rhs);
|
||||||
result.type = lhs.type.combine(rhs.type);
|
result.type = Type.getBooleanType();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
int eq(int x, int y);
|
bool eq(int x, int y);
|
||||||
int neq(int x, int y);
|
bool neq(int x, int y);
|
||||||
int lt(int x, int y);
|
bool lt(int x, int y);
|
||||||
int lte(int x, int y);
|
bool lte(int x, int y);
|
||||||
int gt(int x, int y);
|
bool gt(int x, int y);
|
||||||
int gte(int x, int y);
|
bool gte(int x, int y);
|
||||||
|
|
||||||
bool and(bool a, bool b);
|
bool and(bool a, bool b);
|
||||||
bool or(bool a, bool b);
|
bool or(bool a, bool b);
|
||||||
|
|||||||
@@ -109,27 +109,27 @@ function fac(x: int): int {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
function eq(x: int, y: int): int {
|
function eq(x: int, y: int): bool {
|
||||||
return (x == y);
|
return (x == y);
|
||||||
}
|
}
|
||||||
|
|
||||||
function neq(x: int, y: int): int {
|
function neq(x: int, y: int): bool {
|
||||||
return (x != y);
|
return (x != y);
|
||||||
}
|
}
|
||||||
|
|
||||||
function lt(x: int, y: int): int {
|
function lt(x: int, y: int): bool {
|
||||||
return (x < y);
|
return (x < y);
|
||||||
}
|
}
|
||||||
|
|
||||||
function lte(x: int, y: int): int {
|
function lte(x: int, y: int): bool {
|
||||||
return (x <= y);
|
return (x <= y);
|
||||||
}
|
}
|
||||||
|
|
||||||
function gt(x: int, y: int): int {
|
function gt(x: int, y: int): bool {
|
||||||
return (x > y);
|
return (x > y);
|
||||||
}
|
}
|
||||||
|
|
||||||
function gte(x: int, y: int): int {
|
function gte(x: int, y: int): bool {
|
||||||
return (x >= y);
|
return (x >= y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user