use of kalang.antlr.KalangParser.TypeContext in project kalang by kasonyang.
the class AstBuilder method visitLocalVarDecl.
@Override
public Statement visitLocalVarDecl(LocalVarDeclContext ctx) {
MultiStmt ms = new MultiStmt();
for (VarDeclContext v : ctx.varDecl()) {
TypeContext varType = v.varType;
Type exceptedType = varType == null ? null : parseType(varType);
ExprNode initExpr = null;
ExpressionContext initExprContext = v.expression();
if (initExprContext != null) {
if (initExprContext instanceof LiteralExprContext) {
initExpr = this.parseLiteral(((LiteralExprContext) initExprContext).literal(), exceptedType);
} else {
initExpr = visitExpression(initExprContext);
}
}
VarInfo varInfo = varDecl(v, initExpr == null ? Types.getRootType() : initExpr.getType());
LocalVarNode localVar = this.declareLocalVar(varInfo.name, varInfo.type, varInfo.modifier, ctx);
if (localVar == null)
return null;
VarDeclStmt vds = new VarDeclStmt(localVar);
ms.statements.add(vds);
if (initExpr != null) {
AssignExpr assignExpr = new AssignExpr(new VarExpr(localVar), initExpr);
mapAst(assignExpr, v);
ms.statements.add(new ExprStmt(assignExpr));
}
mapAst(localVar, ctx);
}
return ms;
}
use of kalang.antlr.KalangParser.TypeContext 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.antlr.KalangParser.TypeContext in project kalang by kasonyang.
the class AstBuilder method varDecl.
protected VarInfo varDecl(VarDeclContext ctx, Type inferedType) {
VarInfo vds = new VarInfo();
String name = ctx.name.getText();
TypeContext type = null;
if (ctx.varType != null) {
type = ctx.varType;
} else if (ctx.type() != null) {
type = ctx.type();
}
Type declType = type != null ? parseType(type) : inferedType;
if (isDefindedId(name)) {
diagnosisReporter.report(Diagnosis.Kind.ERROR, "the name is definded:" + name, ctx);
}
vds.name = name;
vds.type = declType;
vds.modifier = ctx.valToken != null ? Modifier.FINAL : 0;
// }
if (vds.type == null) {
vds.type = Types.getRootType();
}
return vds;
}
Aggregations