use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class Desugar method createPatternIfCondition.
private BLangExpression createPatternIfCondition(BLangMatchStmtPatternClause patternClause, BVarSymbol varSymbol) {
BLangExpression binaryExpr;
BType patternType = patternClause.variable.type;
BType[] memberTypes;
if (patternType.tag == TypeTags.UNION) {
BUnionType unionType = (BUnionType) patternType;
memberTypes = unionType.memberTypes.toArray(new BType[0]);
} else {
memberTypes = new BType[1];
memberTypes[0] = patternType;
}
if (memberTypes.length == 1) {
binaryExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[0]);
} else {
BLangExpression lhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[0]);
BLangExpression rhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[1]);
binaryExpr = ASTBuilderUtil.createBinaryExpr(patternClause.pos, lhsExpr, rhsExpr, symTable.booleanType, OperatorKind.OR, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.OR, lhsExpr.type, rhsExpr.type));
for (int i = 2; i < memberTypes.length; i++) {
lhsExpr = createPatternMatchBinaryExpr(patternClause.pos, varSymbol, memberTypes[i]);
rhsExpr = binaryExpr;
binaryExpr = ASTBuilderUtil.createBinaryExpr(patternClause.pos, lhsExpr, rhsExpr, symTable.booleanType, OperatorKind.OR, (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.OR, lhsExpr.type, rhsExpr.type));
}
}
return binaryExpr;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCalculateAverage.
/**
* Generates following.
*
* result = result / count
*
* @param blockStmt target
* @param ctx current context
*/
private void generateCalculateAverage(BLangBlockStmt blockStmt, IterableContext ctx) {
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr divide = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
divide.pos = pos;
divide.type = ctx.resultVar.symbol.type;
divide.opKind = OperatorKind.ADD;
divide.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
divide.rhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
divide.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.DIV, divide.type, ctx.countVar.symbol.type);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
countAdd.expr = divide;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateSumAggregator.
/**
* Generates following.
*
* result = result + value
*
* @param blockStmt target
* @param ctx current context
*/
private void generateSumAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
add.pos = pos;
add.type = ctx.resultVar.symbol.type;
add.opKind = OperatorKind.ADD;
add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
add.rhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, add.type, add.type);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
countAdd.expr = add;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCompareAggregator.
/**
* Generates following.
*
* result = result (Operator) value ? result : value
*
* @param blockStmt target
* @param ctx current context
* @param operator compare operator
*/
private void generateCompareAggregator(BLangBlockStmt blockStmt, IterableContext ctx, OperatorKind operator) {
final DiagnosticPos pos = blockStmt.pos;
final BLangSimpleVarRef resultVar = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
final BLangSimpleVarRef valueVar = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
final BLangBinaryExpr compare = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
compare.pos = pos;
compare.type = symTable.booleanType;
compare.opKind = operator;
compare.lhsExpr = resultVar;
compare.rhsExpr = valueVar;
compare.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(operator, resultVar.symbol.type, valueVar.symbol.type);
final BLangTernaryExpr ternaryExpr = (BLangTernaryExpr) TreeBuilder.createTernaryExpressionNode();
ternaryExpr.pos = pos;
ternaryExpr.expr = compare;
ternaryExpr.thenExpr = resultVar;
ternaryExpr.elseExpr = valueVar;
ternaryExpr.type = compare.type;
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(resultVar);
countAdd.expr = ternaryExpr;
}
use of org.wso2.ballerinalang.compiler.semantics.model.symbols.BOperatorSymbol in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangUnaryExpr unaryExpr) {
BType exprType = null;
BType actualType = symTable.errType;
if (OperatorKind.TYPEOF.equals(unaryExpr.operator)) {
// Handle typeof operator separately
if (unaryExpr.expr.getKind() == NodeKind.SIMPLE_VARIABLE_REF) {
BLangSimpleVarRef varRef = (BLangSimpleVarRef) unaryExpr.expr;
Name varRefName = names.fromIdNode((varRef).variableName);
Name pkgAlias = names.fromIdNode((varRef).pkgAlias);
// Resolve symbol for BLangSimpleVarRef
BSymbol varRefSybmol = symResolver.lookupSymbolInPackage(unaryExpr.pos, env, pkgAlias, varRefName, SymTag.VARIABLE);
if (varRefSybmol == symTable.notFoundSymbol) {
// Resolve symbol for User Defined Type ( converted from BLangSimpleVarRef )
BLangTypeofExpr typeAccessExpr = getTypeAccessExpression(varRef);
unaryExpr.expr = typeAccessExpr;
actualType = typeAccessExpr.type;
resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
return;
} else {
// Check type if resolved as BLangSimpleVarRef
exprType = checkExpr(unaryExpr.expr, env).get(0);
}
} else {
// Check type if resolved as non BLangSimpleVarRef Expression
exprType = checkExpr(unaryExpr.expr, env).get(0);
}
if (exprType != symTable.errType) {
unaryExpr.opSymbol = Symbols.createTypeofOperatorSymbol(exprType, types, symTable, names);
actualType = unaryExpr.opSymbol.type.getReturnTypes().get(0);
}
} else {
exprType = checkExpr(unaryExpr.expr, env).get(0);
if (exprType != symTable.errType) {
BSymbol symbol = symResolver.resolveUnaryOperator(unaryExpr.pos, unaryExpr.operator, exprType);
if (symbol == symTable.notFoundSymbol) {
dlog.error(unaryExpr.pos, DiagnosticCode.UNARY_OP_INCOMPATIBLE_TYPES, unaryExpr.operator, exprType);
} else {
unaryExpr.opSymbol = (BOperatorSymbol) symbol;
actualType = symbol.type.getReturnTypes().get(0);
}
}
}
resultTypes = types.checkTypes(unaryExpr, Lists.of(actualType), expTypes);
}
Aggregations