use of kalang.ast.ExprNode 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.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitArrayExpr.
@Override
public Object visitArrayExpr(KalangParser.ArrayExprContext ctx) {
ExprNode[] initExprs;
List<ExpressionContext> exprCtx = ctx.expression();
if (exprCtx != null) {
initExprs = new ExprNode[exprCtx.size()];
for (int i = 0; i < initExprs.length; i++) {
initExprs[i] = visitExpression(exprCtx.get(i));
}
} else {
initExprs = new ExprNode[0];
}
TypeContext typeCtx = ctx.type();
Type type;
if (typeCtx != null) {
type = parseType(typeCtx);
} else {
type = TypeUtil.getCommonType(AstUtil.getExprTypes(initExprs));
}
for (int i = 0; i < initExprs.length; i++) {
if (exprCtx == null)
throw Exceptions.unexceptedValue(exprCtx);
initExprs[i] = requireCastable(initExprs[i], initExprs[i].getType(), type, exprCtx.get(i).getStart());
if (initExprs[i] == null)
return null;
}
ExprNode arrExpr = createInitializedArray(type, initExprs);
mapAst(arrExpr, ctx);
return arrExpr;
}
use of kalang.ast.ExprNode 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.ExprNode in project kalang by kasonyang.
the class AstBuilder method getObjectFieldExpr.
@Nullable
protected ExprNode getObjectFieldExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
ExprNode ret;
Type type = expr.getType();
if (!(type instanceof ObjectType)) {
// AstBuilder.this.handleSyntaxError("unsupported type", rule==null ? ParserRuleContext.EMPTY : rule);
return null;
}
ObjectType exprType = (ObjectType) type;
if ((exprType instanceof ArrayType)) {
return null;
} else {
try {
ret = ObjectFieldExpr.create(expr, fieldName, exprType.getClassNode());
} catch (FieldNotFoundException ex) {
return null;
}
}
if (rule != null)
mapAst(ret, rule);
return ret;
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitNewArrayExpr.
@Override
public ExprNode visitNewArrayExpr(KalangParser.NewArrayExprContext ctx) {
Type type = parseType(ctx.type());
ExprNode ret;
if (ctx.size != null) {
ExprNode size = visitExpression(ctx.size);
ret = new NewArrayExpr(type, size);
} else {
ExprNode[] initExprs = new ExprNode[ctx.initExpr.size()];
for (int i = 0; i < initExprs.length; i++) {
initExprs[i] = visitExpression(ctx.initExpr.get(i));
}
ret = createInitializedArray(type, initExprs);
}
mapAst(ret, ctx);
return ret;
}
Aggregations