Search in sources :

Example 1 with AstNode

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

the class AstBuilder method visitInstanceofExpr.

@Override
public Object visitInstanceofExpr(KalangParser.InstanceofExprContext ctx) {
    ExprNode expr = visitExpression(ctx.expression());
    Token ts = ctx.Identifier().getSymbol();
    AstNode tnode = getNodeById(ts.getText(), ts);
    if (tnode instanceof ClassReference) {
        InstanceOfExpr ie = new InstanceOfExpr(expr, (ClassReference) tnode);
        mapAst(ie, ctx);
        return ie;
    } else {
        AstBuilder.this.handleSyntaxError("unsupported type", ts);
        return null;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) Token(org.antlr.v4.runtime.Token) ClassReference(kalang.ast.ClassReference) InstanceOfExpr(kalang.ast.InstanceOfExpr) AstNode(kalang.ast.AstNode)

Example 2 with AstNode

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

the class AstBuilder method visitIdentifierExpr.

@Override
public AstNode visitIdentifierExpr(IdentifierExprContext ctx) {
    String name = ctx.Identifier().getText();
    AstNode expr = this.getNodeById(name, ctx.Identifier().getSymbol());
    if (expr == null) {
        this.handleSyntaxError(name + " is undefined!", ctx);
        return null;
    }
    mapAst(expr, ctx);
    return expr;
}
Also used : AstNode(kalang.ast.AstNode)

Example 3 with AstNode

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

the class AstOutputUtil method toString.

public static String toString(AstNode node) {
    if (node == null)
        return "null";
    String nodeStr = node.toString();
    StringBuilder sb = new StringBuilder();
    sb.append(nodeStr).append(":");
    for (AstNode c : node.getChildren()) {
        sb.append(LINE_DELIMITER);
        String cStr = toString(c);
        sb.append(StringUtil.indentLines(cStr, INDENT, LINE_DELIMITER));
    }
    return sb.toString();
}
Also used : AstNode(kalang.ast.AstNode)

Example 4 with AstNode

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

the class AstBuilder method visitSelfRefExpr.

@Override
public AstNode visitSelfRefExpr(SelfRefExprContext ctx) {
    String key = ctx.ref.getText();
    AstNode expr;
    if (key.equals("this")) {
        expr = new ThisExpr(getThisType());
    } else if (key.equals("super")) {
        expr = new SuperExpr(thisClazz);
    } else {
        throw Exceptions.unknownValue(key);
    }
    mapAst(expr, ctx);
    return expr;
}
Also used : SuperExpr(kalang.ast.SuperExpr) AstNode(kalang.ast.AstNode) ThisExpr(kalang.ast.ThisExpr)

Example 5 with AstNode

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

the class AstBuilder method visitExpression.

protected ExprNode visitExpression(ExpressionContext expression) {
    Object node = visit(expression);
    if (node instanceof ExprNode) {
        return (ExprNode) node;
    } else {
        ExprNode expr;
        if (node instanceof AstNode) {
            expr = new ErrorousExpr((AstNode) node);
        } else {
            expr = new ErrorousExpr();
        }
        this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "not an expression", expression);
        return expr;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) VarObject(kalang.ast.VarObject) ErrorousExpr(kalang.ast.ErrorousExpr) AstNode(kalang.ast.AstNode)

Aggregations

AstNode (kalang.ast.AstNode)5 ExprNode (kalang.ast.ExprNode)2 ClassReference (kalang.ast.ClassReference)1 ErrorousExpr (kalang.ast.ErrorousExpr)1 InstanceOfExpr (kalang.ast.InstanceOfExpr)1 SuperExpr (kalang.ast.SuperExpr)1 ThisExpr (kalang.ast.ThisExpr)1 VarObject (kalang.ast.VarObject)1 Token (org.antlr.v4.runtime.Token)1