Skip to content

Commit

Permalink
Added logical || and && operator to binary expression AST.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Apr 27, 2024
1 parent 6b14334 commit 2ce60c5
Showing 1 changed file with 62 additions and 6 deletions.
68 changes: 62 additions & 6 deletions core/ast/expr/expr_binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,44 @@ export default class ExprASTBinary implements ExpressionAST {
leftType.toString() + '<<' + rightType.toString() + ']');
}

private visitOr(
builder: IRBuilder,
module: Module
): Value {
const leftType: DataType =
this.left.type();
const rightType: DataType =
this.right.type();

if(leftType == DataType.BOOL && rightType == DataType.BOOL)
return builder.CreateOr(
this.left.visit(builder, module),
this.right.visit(builder, module)
);

throw new ASTError('Invalid binary operation [' +
leftType.toString() + '||' + rightType.toString() + ']');
}

private visitAnd(
builder: IRBuilder,
module: Module
): Value {
const leftType: DataType =
this.left.type();
const rightType: DataType =
this.right.type();

if(leftType == DataType.BOOL && rightType == DataType.BOOL)
return builder.CreateAnd(
this.left.visit(builder, module),
this.right.visit(builder, module)
);

throw new ASTError('Invalid binary operation [' +
leftType.toString() + '&&' + rightType.toString() + ']');
}

public visit(
builder: IRBuilder,
module: Module
Expand Down Expand Up @@ -488,6 +526,16 @@ export default class ExprASTBinary implements ExpressionAST {
builder,
module
);
else if(this.operator == '||')
return this.visitOr(
builder,
module
);
else if(this.operator == '&&')
return this.visitAnd(
builder,
module
);

throw new ASTError('Invalid operation.');
}
Expand Down Expand Up @@ -543,6 +591,10 @@ export default class ExprASTBinary implements ExpressionAST {
else if(DataType.isOfUIntType(a) && DataType.isOfUIntType(b))
return DataType.greaterUIntegerType(a, b);
}
else if((this.operator == '||' ||
this.operator == '&&') &&
a == DataType.BOOL && b == DataType.BOOL)
return DataType.BOOL;

throw new ASTError("Incompatible types for binary operation.");
}
Expand Down Expand Up @@ -728,12 +780,16 @@ export default class ExprASTBinary implements ExpressionAST {
') operation with ' + leftType.toString() +
' and ' + rightType.toString()
);

this.resolveExpressions(
results,
returnType,
unsafe
);
else if((this.operator == '||' ||
this.operator == '&&') &&
!(leftType == DataType.BOOL &&
rightType == DataType.BOOL))
results.errors.set(
this.marker(),
'Invalid logic operator (' + this.operator +
') operation with ' + leftType.toString() +
' and ' + rightType.toString()
);
}

public marker(): Token {
Expand Down

0 comments on commit 2ce60c5

Please sign in to comment.