Search in sources :

Example 1 with AssignableExpr

use of kalang.ast.AssignableExpr in project kalang by kasonyang.

the class AstBuilder method visitPostIfStmt.

@Override
public AstNode visitPostIfStmt(KalangParser.PostIfStmtContext ctx) {
    ExprNode leftExpr = visitExpression(ctx.expression(0));
    if (!(leftExpr instanceof AssignExpr)) {
        diagnosisReporter.report(Diagnosis.Kind.ERROR, "AssignExpr required", ctx);
    }
    AssignExpr assignExpr = (AssignExpr) leftExpr;
    AssignableExpr to = assignExpr.getTo();
    ExprNode from = assignExpr.getFrom();
    ExprNode cond = visitExpression(ctx.expression(1));
    Token op = ctx.op;
    if (op != null) {
        String opStr = op.getText();
        BinaryExpr be = createBinaryExpr(to, cond, opStr);
        cond = be;
    }
    AssignExpr as = new AssignExpr(to, from);
    IfStmt is = new IfStmt(cond);
    is.getTrueBody().statements.add(new ExprStmt(as));
    mapAst(is, ctx);
    return is;
}
Also used : ExprNode(kalang.ast.ExprNode) IfStmt(kalang.ast.IfStmt) ExprStmt(kalang.ast.ExprStmt) AssignableExpr(kalang.ast.AssignableExpr) BinaryExpr(kalang.ast.BinaryExpr) Token(org.antlr.v4.runtime.Token) AssignExpr(kalang.ast.AssignExpr)

Example 2 with AssignableExpr

use of kalang.ast.AssignableExpr 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)

Example 3 with AssignableExpr

use of kalang.ast.AssignableExpr in project kalang by kasonyang.

the class AstBuilder method createFieldExpr.

protected ExprNode createFieldExpr(GetFieldExprContext to, @Nullable ExpressionContext fromCtx, OffsetRange offsetRange) {
    // TODO support iterating syntax
    String refKey = to.refKey.getText();
    ExpressionContext exp = to.expression();
    String fname = to.Identifier().getText();
    AssignableExpr toExpr;
    Object expr = visit(exp);
    if (refKey.equals(".")) {
        ExprNode fieldExpr;
        if (expr instanceof ExprNode) {
            ExprNode exprNode = (ExprNode) expr;
            fieldExpr = getObjectFieldLikeExpr(exprNode, fname, to);
        } else if (expr instanceof ClassReference) {
            fieldExpr = getStaticFieldExpr((ClassReference) expr, fname, to);
        } else {
            throw new UnknownError("unknown node:" + expr);
        }
        if (fromCtx == null) {
            return fieldExpr;
        } else {
            if (fieldExpr instanceof AssignableExpr) {
                toExpr = (AssignableExpr) fieldExpr;
            } else {
                AstBuilder.this.handleSyntaxError("unsupported", to);
                return null;
            }
            ExprNode fromExpr = visitExpression(fromCtx);
            if (!this.semanticAnalyzer.validateAssign(toExpr, fromExpr, offsetRange)) {
                return null;
            }
            return new AssignExpr(toExpr, fromExpr);
        }
    } else if (refKey.equals("->")) {
        ExprNode[] params;
        String methodName;
        if (fromCtx == null) {
            params = new ExprNode[0];
            methodName = "get" + NameUtil.firstCharToUpperCase(fname);
        } else {
            params = new ExprNode[1];
            methodName = "set" + NameUtil.firstCharToUpperCase(fname);
        }
        if (expr instanceof ExprNode) {
            if (fromCtx != null)
                params[0] = visitExpression(fromCtx);
            return getObjectInvokeExpr((ExprNode) expr, methodName, params, to);
        } else {
            // don't support static property
            handleSyntaxError("object expression required.", to);
            return null;
        }
    } else {
        throw Exceptions.unknownValue(refKey);
    }
}
Also used : ExprNode(kalang.ast.ExprNode) ExpressionContext(kalang.antlr.KalangParser.ExpressionContext) AssignableExpr(kalang.ast.AssignableExpr) VarObject(kalang.ast.VarObject) ClassReference(kalang.ast.ClassReference) AssignExpr(kalang.ast.AssignExpr)

Example 4 with AssignableExpr

use of kalang.ast.AssignableExpr in project kalang by kasonyang.

the class AstBuilder method getIncrementExpr.

public IncrementExpr getIncrementExpr(ExpressionContext expressionContext, String op, boolean isPrefix) {
    ExprNode expr = visitExpression(expressionContext);
    if (!(expr instanceof AssignableExpr)) {
        AstBuilder.this.handleSyntaxError("require assignable expression", expressionContext);
        return null;
    }
    boolean isDesc = op.equals("--");
    return new IncrementExpr((AssignableExpr) expr, isDesc, isPrefix);
}
Also used : ExprNode(kalang.ast.ExprNode) IncrementExpr(kalang.ast.IncrementExpr) AssignableExpr(kalang.ast.AssignableExpr)

Example 5 with AssignableExpr

use of kalang.ast.AssignableExpr in project kalang by kasonyang.

the class AstBuilder method getStaticFieldExpr.

protected AssignableExpr getStaticFieldExpr(ClassReference clazz, String fieldName, ParserRuleContext rule) {
    AssignableExpr ret;
    try {
        ret = StaticFieldExpr.create(clazz, fieldName, thisClazz);
    } catch (FieldNotFoundException ex) {
        // ret = new UnknownFieldExpr(clazz, clazz.getReferencedClassNode(), fieldName);
        return null;
    }
    mapAst(ret, rule);
    return ret;
}
Also used : AssignableExpr(kalang.ast.AssignableExpr) FieldNotFoundException(kalang.FieldNotFoundException)

Aggregations

AssignableExpr (kalang.ast.AssignableExpr)5 ExprNode (kalang.ast.ExprNode)4 AssignExpr (kalang.ast.AssignExpr)3 ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)2 FieldNotFoundException (kalang.FieldNotFoundException)1 GetFieldExprContext (kalang.antlr.KalangParser.GetFieldExprContext)1 BinaryExpr (kalang.ast.BinaryExpr)1 ClassReference (kalang.ast.ClassReference)1 ExprStmt (kalang.ast.ExprStmt)1 IfStmt (kalang.ast.IfStmt)1 IncrementExpr (kalang.ast.IncrementExpr)1 VarObject (kalang.ast.VarObject)1 Token (org.antlr.v4.runtime.Token)1