use of kalang.ast.LocalVarNode in project kalang by kasonyang.
the class AstBuilder method visitInvokeExpr.
@Override
public AstNode visitInvokeExpr(InvokeExprContext ctx) {
Object target = visit(ctx.target);
if (target == null)
return null;
String mdName = ctx.Identifier().getText();
String refKey = ctx.refKey.getText();
if (refKey.equals(".")) {
if (target instanceof ClassReference) {
return getStaticInvokeExpr((ClassReference) target, mdName, ctx.params, ctx);
} else if (target instanceof ExprNode) {
return getObjectInvokeExpr((ExprNode) target, mdName, ctx.params, ctx);
} else {
throw Exceptions.unexceptedValue(target);
}
} else if (refKey.equals("->")) {
ExprNode[] invokeArgs = new ExprNode[3];
ExprNode[] params = new ExprNode[ctx.params.size()];
if (target instanceof ClassReference) {
invokeArgs[0] = new ConstExpr(null);
} else if (target instanceof ExprNode) {
invokeArgs[0] = ((ExprNode) target);
}
invokeArgs[1] = new ConstExpr(mdName);
for (int i = 0; i < params.length; i++) {
params[i] = visitExpression(ctx.params.get(i));
}
invokeArgs[2] = createInitializedArray(Types.getRootType(), params);
ClassNode dispatcherAst = getAst("kalang.runtime.dynamic.MethodDispatcher");
if (dispatcherAst == null) {
throw Exceptions.unexceptedException("Runtime library is required!");
}
return getStaticInvokeExpr(new ClassReference(dispatcherAst), "invokeMethod", invokeArgs, ctx);
} else if (refKey.equals("*.")) {
if (!(target instanceof ExprNode)) {
handleSyntaxError("expression required", ctx.expression);
return null;
}
ExprNode targetExpr = (ExprNode) target;
Type targetType = targetExpr.getType();
if (!(targetType instanceof ArrayType)) {
handleSyntaxError("array required", ctx.expression);
return null;
}
List<Statement> stats = new LinkedList();
LocalVarNode varArrLen = this.declareTempLocalVar(Types.INT_TYPE);
LocalVarNode varCounter = this.declareTempLocalVar(Types.INT_TYPE);
stats.add(new VarDeclStmt(Arrays.asList(varArrLen, varCounter)));
VarExpr varArrLenExpr = new VarExpr(varArrLen);
VarExpr varCounterExpr = new VarExpr(varCounter);
stats.add(new ExprStmt(new AssignExpr(varArrLenExpr, new ArrayLengthExpr(targetExpr))));
stats.add(new ExprStmt(new AssignExpr(varCounterExpr, new ConstExpr(0))));
CompareExpr conditionExpr = new CompareExpr(varCounterExpr, varArrLenExpr, CompareExpr.OP_LT);
ExprNode targetEleExpr = new ElementExpr(targetExpr, varCounterExpr);
ExprNode invokeExpr = getObjectInvokeExpr(targetEleExpr, mdName, ctx.params, ctx);
if (invokeExpr == null)
return null;
LocalVarNode varRet = this.declareTempLocalVar(Types.getArrayType(invokeExpr.getType()));
VarExpr varRetExpr = new VarExpr(varRet);
stats.add(new VarDeclStmt(varRet));
stats.add(new ExprStmt(new AssignExpr(varRetExpr, new NewArrayExpr(invokeExpr.getType(), varArrLenExpr))));
BlockStmt loopBody = this.newBlock();
loopBody.statements.add(new ExprStmt(new AssignExpr(new ElementExpr(varRetExpr, varCounterExpr), invokeExpr)));
popBlock();
BlockStmt updateBs = newBlock();
updateBs.statements.add(new ExprStmt(new AssignExpr(varCounterExpr, new MathExpr(varCounterExpr, new ConstExpr(1), MathExpr.OP_ADD))));
this.popBlock();
LoopStmt loopStmt = new LoopStmt(conditionExpr, null, loopBody, updateBs);
stats.add(loopStmt);
return new MultiStmtExpr(stats, varRetExpr);
} else {
throw Exceptions.unexceptedException(refKey);
}
}
use of kalang.ast.LocalVarNode in project kalang by kasonyang.
the class AstBuilder method createInitializedArray.
public ExprNode createInitializedArray(Type type, ExprNode[] exprs) {
NewArrayExpr ae = new NewArrayExpr(type, new ConstExpr(exprs.length));
if (exprs.length > 0) {
Statement[] initStmts = new Statement[exprs.length + 2];
// TODO create a method for temp var creation
// TODO localVarNode should add a type parameter
LocalVarNode local = this.declareTempLocalVar(ae.getType());
initStmts[0] = new VarDeclStmt(local);
VarExpr arrVar = new VarExpr(local);
initStmts[1] = new ExprStmt(new AssignExpr(arrVar, ae));
for (int i = 0; i < exprs.length; i++) {
initStmts[i + 2] = new ExprStmt(new AssignExpr(new ElementExpr(arrVar, new ConstExpr(i)), exprs[i]));
}
return new MultiStmtExpr(Arrays.asList(initStmts), arrVar);
} else {
return ae;
}
}
use of kalang.ast.LocalVarNode in project kalang by kasonyang.
the class Ast2Class method popFrame.
private void popFrame() {
for (LocalVarNode v : this.varTables.values()) {
this.destroyLocalVarNode(v);
}
int startVarIdx = this.varStartIndexOfFrame.pop();
this.varIdCounter = startVarIdx;
this.varTables = this.varTables.popStack();
}
use of kalang.ast.LocalVarNode in project kalang by kasonyang.
the class AstBuilder method declareLocalVar.
@Nullable
private LocalVarNode declareLocalVar(String name, Type type, int modifier, ParserRuleContext ctx) {
LocalVarNode localVarNode = new LocalVarNode(type, name, modifier);
ParameterNode param = this.getNamedParameter(name);
LocalVarNode var = this.getNamedLocalVar(name);
if (param != null || var != null) {
handleSyntaxError("variable is defined", ctx);
return null;
}
if (name != null) {
this.varTables.put(name, localVarNode);
}
return localVarNode;
}
use of kalang.ast.LocalVarNode in project kalang by kasonyang.
the class AstBuilder method visitQuestionExpr.
@Override
public AstNode visitQuestionExpr(KalangParser.QuestionExprContext ctx) {
List<Statement> stmts = new LinkedList<>();
ExprNode conditionExpr = (ExprNode) visit(ctx.expression(0));
ExprNode trueExpr = (ExprNode) visit(ctx.expression(1));
ExprNode falseExpr = (ExprNode) visit(ctx.expression(2));
Type trueType = trueExpr.getType();
Type falseType = falseExpr.getType();
Type type;
if (trueType.equals(falseType)) {
type = trueType;
} else {
type = TypeUtil.getCommonType(trueType, falseType);
}
LocalVarNode vo = this.declareTempLocalVar(type);
VarDeclStmt vds = new VarDeclStmt(vo);
stmts.add(vds);
VarExpr ve = new VarExpr(vo);
IfStmt is = new IfStmt(conditionExpr);
is.getTrueBody().statements.add(new ExprStmt(new AssignExpr(ve, trueExpr)));
is.getFalseBody().statements.add(new ExprStmt(new AssignExpr(ve, falseExpr)));
stmts.add(is);
MultiStmtExpr mse = new MultiStmtExpr(stmts, ve);
mapAst(ve, ctx);
return mse;
}
Aggregations