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;
}
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;
}
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);
}
}
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);
}
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;
}
Aggregations