Search in sources :

Example 1 with GetFieldExprContext

use of kalang.antlr.KalangParser.GetFieldExprContext in project kalang by kasonyang.

the class AstBuilder method visitAssignExpr.

@Override
public ExprNode visitAssignExpr(AssignExprContext ctx) {
    ExprNode expr;
    String assignOp = ctx.getChild(1).getText();
    ExpressionContext toCtx = ctx.expression(0);
    ExpressionContext fromCtx = ctx.expression(1);
    if (toCtx instanceof GetFieldExprContext) {
        // TODO check readonly
        expr = createFieldExpr((GetFieldExprContext) toCtx, fromCtx, OffsetRangeHelper.getOffsetRange(ctx));
    } else {
        ExprNode to = visitExpression(toCtx);
        ExprNode from = visitExpression(fromCtx);
        if (assignOp.length() > 1) {
            String op = assignOp.substring(0, assignOp.length() - 1);
            from = createBinaryExpr(to, from, op);
        }
        AssignableExpr toExpr;
        if (to instanceof AssignableExpr) {
            toExpr = (AssignableExpr) to;
            if (!this.semanticAnalyzer.validateAssign(toExpr, from, OffsetRangeHelper.getOffsetRange(ctx))) {
                return null;
            }
            AssignExpr aexpr = new AssignExpr(toExpr, from);
            mapAst(aexpr, ctx);
            // TODO remove override information before assign
            onAssign(toExpr, from);
            expr = aexpr;
        } else {
            AstBuilder.this.handleSyntaxError("unsupported assign statement", ctx);
            return null;
        }
    }
    return expr;
}
Also used : ExprNode(kalang.ast.ExprNode) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) AssignableExpr(kalang.ast.AssignableExpr) GetFieldExprContext(kalang.antlr.KalangParser.GetFieldExprContext) AssignExpr(kalang.ast.AssignExpr)

Aggregations

ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)1 GetFieldExprContext (kalang.antlr.KalangParser.GetFieldExprContext)1 AssignExpr (kalang.ast.AssignExpr)1 AssignableExpr (kalang.ast.AssignableExpr)1 ExprNode (kalang.ast.ExprNode)1