use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class Desugar method visit.
public void visit(BLangPostIncrement postIncrement) {
BLangAssignment assignStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
assignStmt.pos = postIncrement.pos;
assignStmt.addVariable(rewriteExpr((BLangVariableReference) postIncrement.varRef));
assignStmt.expr = rewriteExpr(postIncrement.modifiedExpr);
result = assignStmt;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangTupleDestructure stmt) {
// var (a, b) = (tuple)
//
// desugar once
// any[] x = (tuple);
// a = x[0];
final BLangBlockStmt blockStmt = ASTBuilderUtil.createBlockStmt(stmt.pos);
BType runTimeType = new BArrayType(symTable.anyType);
final BLangVariable tuple = ASTBuilderUtil.createVariable(stmt.pos, "", runTimeType, null, new BVarSymbol(0, names.fromString("tuple"), this.env.scope.owner.pkgID, runTimeType, this.env.scope.owner));
tuple.expr = stmt.expr;
final BLangVariableDef variableDef = ASTBuilderUtil.createVariableDefStmt(stmt.pos, blockStmt);
variableDef.var = tuple;
for (int index = 0; index < stmt.varRefs.size(); index++) {
BLangExpression varRef = stmt.varRefs.get(index);
BLangLiteral indexExpr = ASTBuilderUtil.createLiteral(stmt.pos, symTable.intType, (long) index);
BLangIndexBasedAccess arrayAccess = ASTBuilderUtil.createIndexBasesAccessExpr(stmt.pos, symTable.anyType, tuple.symbol, indexExpr);
final BLangExpression assignmentExpr;
if (types.isValueType(varRef.type)) {
BLangTypeConversionExpr castExpr = (BLangTypeConversionExpr) TreeBuilder.createTypeConversionNode();
castExpr.expr = arrayAccess;
castExpr.conversionSymbol = Symbols.createUnboxValueTypeOpSymbol(symTable.anyType, varRef.type);
castExpr.type = varRef.type;
assignmentExpr = castExpr;
} else {
assignmentExpr = arrayAccess;
}
final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(stmt.pos, blockStmt);
assignmentStmt.declaredWithVar = stmt.declaredWithVar;
assignmentStmt.varRefs = Lists.of(varRef);
assignmentStmt.expr = assignmentExpr;
}
result = rewrite(blockStmt, env);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class Desugar method visit.
@Override
public void visit(BLangAssignment assignNode) {
if (assignNode.expr.type.tag == TypeTags.STREAM && assignNode.varRefs.get(0) instanceof BLangSimpleVarRef) {
((BLangRecordLiteral) assignNode.expr).name = ((BLangSimpleVarRef) assignNode.varRefs.get(0)).variableName;
}
assignNode.varRefs = rewriteExprs(assignNode.varRefs);
assignNode.expr = rewriteExpr(assignNode.expr);
result = assignNode;
if (!assignNode.safeAssignment) {
return;
}
// Desugar the =? operator with the match statement
//
// e.g.
// File f;
// .....
// f =? openFile("/tmp/foo.txt"); // openFile: () -> (File | error)
//
// {
// match openFile("/tmp/foo.txt") {
// File _$_f1 => f = _$_f1;
// error e => throw e | return e
// }
// }
BLangBlockStmt safeAssignmentBlock = ASTBuilderUtil.createBlockStmt(assignNode.pos, new ArrayList<>());
BLangExpression lhsExpr = assignNode.varRefs.get(0);
BLangMatchStmtPatternClause patternSuccessCase;
if (assignNode.declaredWithVar) {
BVarSymbol varSymbol = ((BLangSimpleVarRef) lhsExpr).symbol;
BLangVariable variable = ASTBuilderUtil.createVariable(assignNode.pos, "", lhsExpr.type, null, varSymbol);
BLangVariableDef variableDef = ASTBuilderUtil.createVariableDef(assignNode.pos, variable);
safeAssignmentBlock.stmts.add(variableDef);
patternSuccessCase = getSafeAssignSuccessPattern(assignNode.pos, lhsExpr.type, true, varSymbol, null);
} else {
patternSuccessCase = getSafeAssignSuccessPattern(assignNode.pos, lhsExpr.type, false, null, lhsExpr);
}
// Create the pattern to match the success case
BLangMatchStmtPatternClause patternErrorCase = getSafeAssignErrorPattern(assignNode.pos, this.env.enclInvokable.symbol);
// Create the match statement
BLangMatch matchStmt = ASTBuilderUtil.createMatchStatement(assignNode.pos, assignNode.expr, new ArrayList<BLangMatchStmtPatternClause>() {
{
add(patternSuccessCase);
add(patternErrorCase);
}
});
// var f =? foo() -> var f;
assignNode.expr = null;
assignNode.safeAssignment = false;
safeAssignmentBlock.stmts.add(matchStmt);
result = rewrite(safeAssignmentBlock, this.env);
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment in project ballerina by ballerina-lang.
the class EndpointDesugar method generateEndpointInit.
private BLangBlockStmt generateEndpointInit(BLangEndpoint endpoint, SymbolEnv env, BSymbol encSymbol) {
final String epName = endpoint.name.value;
final DiagnosticPos pos = endpoint.pos;
BLangBlockStmt temp = new BLangBlockStmt();
final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.symbol.type);
epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, names.fromString(epName), SymTag.VARIABLE);
final BLangExpression newExpr;
if (endpoint.configurationExpr != null && endpoint.configurationExpr.getKind() != NodeKind.RECORD_LITERAL_EXPR) {
// Handle Endpoint Assignment.
newExpr = endpoint.configurationExpr;
} else if (endpoint.configurationExpr != null && endpoint.configurationExpr.getKind() == NodeKind.RECORD_LITERAL_EXPR) {
// Handle Endpoint initialization.
newExpr = ASTBuilderUtil.createEmptyRecordLiteral(pos, endpoint.symbol.type);
} else {
newExpr = null;
}
// EPType ep_name = {};
if (env.enclInvokable != null) {
// In callable unit, endpoint is same scope variable.
final BLangVariableDef epNewStmt = ASTBuilderUtil.createVariableDefStmt(pos, temp);
epNewStmt.var = epVariable;
epNewStmt.var.expr = newExpr;
} else {
// This is an init function. ep variable is defined in outside.
if (env.enclService != null) {
// Add to endpoint variable to relevant location
final BLangVariableDef epVarDef = ASTBuilderUtil.createVariableDef(pos);
epVarDef.var = epVariable;
env.enclService.vars.add(epVarDef);
}
final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(pos, temp);
assignmentStmt.varRefs.add(ASTBuilderUtil.createVariableRef(pos, epVariable.symbol));
assignmentStmt.expr = newExpr;
}
return temp;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment 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;
}
Aggregations