use of kalang.ast.AssignExpr in project kalang by kasonyang.
the class Ast2Class method visitIncrementExpr.
@Override
public Object visitIncrementExpr(IncrementExpr node) {
if (!node.isIsPrefix()) {
visit(node.getExpr());
}
Type exprType = node.getExpr().getType();
ConstExpr ce = getConstX(exprType, node.isIsDesc() ? -1 : 1);
BinaryExpr be = new MathExpr(node.getExpr(), ce, "+");
AssignExpr addOne = new AssignExpr(node.getExpr(), be);
visit(addOne);
pop(exprType);
if (node.isIsPrefix()) {
visit(node.getExpr());
}
return null;
}
use of kalang.ast.AssignExpr 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.ast.AssignExpr 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.AssignExpr 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.AssignExpr in project kalang by kasonyang.
the class AstBuilder method visitMapExpr.
@Override
public MultiStmtExpr visitMapExpr(KalangParser.MapExprContext ctx) {
Type keyType = ctx.keyType != null ? requireClassType(ctx.keyType) : Types.getRootType();
Type valueType = ctx.valueType != null ? requireClassType(ctx.valueType) : Types.getRootType();
if (keyType == null || valueType == null)
return null;
LocalVarNode vo = declareTempLocalVar(Types.getClassType(Types.getMapImplClassType().getClassNode(), new Type[] { keyType, valueType }));
VarDeclStmt vds = new VarDeclStmt(vo);
NewObjectExpr newExpr;
try {
newExpr = new NewObjectExpr(Types.getMapImplClassType());
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
List<Statement> stmts = new LinkedList<>();
stmts.add(vds);
stmts.add(new ExprStmt(new AssignExpr(new VarExpr(vo), newExpr)));
VarExpr ve = new VarExpr(vo);
List<TerminalNode> ids = ctx.Identifier();
for (int i = 0; i < ids.size(); i++) {
ExpressionContext e = ctx.expression(i);
ExprNode v = (ExprNode) visit(e);
ConstExpr k = new ConstExpr(ctx.Identifier(i).getText());
ExprNode[] args = new ExprNode[] { k, v };
InvocationExpr iv;
try {
iv = ObjectInvokeExpr.create(ve, "put", args);
} catch (MethodNotFoundException | AmbiguousMethodException ex) {
throw Exceptions.unexceptedException(ex);
}
ExprStmt es = new ExprStmt(iv);
stmts.add(es);
}
MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
mapAst(mse, ctx);
return mse;
}
Aggregations