Skip to content

Commit

Permalink
Added <<, >>, and >>> 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 ae4c8f9 commit 6b14334
Showing 1 changed file with 102 additions and 4 deletions.
106 changes: 102 additions & 4 deletions core/ast/expr/expr_binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,66 @@ export default class ExprASTBinary implements ExpressionAST {
leftType.toString() + '%' + rightType.toString() + ']');
}

private visitLShr(
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.CreateLShr(
this.left.visit(builder, module),
this.right.visit(builder, module)
);

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

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

if((DataType.isOfIntType(leftType) || DataType.isOfUIntType(leftType)) &&
(DataType.isOfIntType(rightType) || DataType.isOfUIntType(rightType)))
return builder.CreateAShr(
this.left.visit(builder, module),
this.right.visit(builder, module)
);

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

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

if((DataType.isOfIntType(leftType) || DataType.isOfUIntType(leftType)) &&
(DataType.isOfIntType(rightType) || DataType.isOfUIntType(rightType)))
return builder.CreateShl(
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 All @@ -404,7 +464,7 @@ export default class ExprASTBinary implements ExpressionAST {
module
);
else if(this.operator == '*')
return this.visitDiv(
return this.visitMul(
builder,
module
);
Expand All @@ -413,6 +473,21 @@ export default class ExprASTBinary implements ExpressionAST {
builder,
module
);
else if(this.operator == '>>>')
return this.visitLShr(
builder,
module
);
else if(this.operator == '>>')
return this.visitAShr(
builder,
module
);
else if(this.operator == '<<')
return this.visitAShl(
builder,
module
);

throw new ASTError('Invalid operation.');
}
Expand All @@ -437,16 +512,39 @@ export default class ExprASTBinary implements ExpressionAST {
(a == DataType.STRING &&
b == DataType.STRING) ||
(a == DataType.STRING &&
DataType.isOfIntType(b)) ||
(DataType.isOfIntType(a) &&
(DataType.isOfIntType(b) || DataType.isOfUIntType(b))) ||
((DataType.isOfIntType(a) || DataType.isOfUIntType(a)) &&
b == DataType.STRING)) ||
(a == DataType.STRING &&
DataType.isOfFloatType(b)) ||
(DataType.isOfFloatType(a) &&
b == DataType.STRING))
return DataType.STRING;
else if(this.operator == '-' ||
this.operator == '/' ||
this.operator == '*' ||
this.operator == '%'
) {
if(DataType.isOfIntType(a) &&
DataType.isOfIntType(b))
return DataType.greaterIntegerType(a, b);
else if(DataType.isOfUIntType(a) &&
DataType.isOfUIntType(b))
return DataType.greaterUIntegerType(a, b);
else if(DataType.isOfFloatType(a) &&
DataType.isOfFloatType(b))
return DataType.greaterFloatType(a, b);
}
else if(this.operator == '>>>' ||
this.operator == '>>' ||
this.operator == '<<') {
if(DataType.isOfIntType(a) && DataType.isOfIntType(b))
return DataType.greaterIntegerType(a, b);
else if(DataType.isOfUIntType(a) && DataType.isOfUIntType(b))
return DataType.greaterUIntegerType(a, b);
}

return DataType.UNKNOWN;
throw new ASTError("Incompatible types for binary operation.");
}

private resolveAdd(
Expand Down

0 comments on commit 6b14334

Please sign in to comment.