use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitInstanceofExpr.
@Override
public Object visitInstanceofExpr(KalangParser.InstanceofExprContext ctx) {
ExprNode expr = visitExpression(ctx.expression());
Token ts = ctx.Identifier().getSymbol();
AstNode tnode = getNodeById(ts.getText(), ts);
if (tnode instanceof ClassReference) {
InstanceOfExpr ie = new InstanceOfExpr(expr, (ClassReference) tnode);
mapAst(ie, ctx);
return ie;
} else {
AstBuilder.this.handleSyntaxError("unsupported type", ts);
return null;
}
}
use of kalang.ast.ExprNode 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.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitInterpolationExpr.
@Override
public Object visitInterpolationExpr(KalangParser.InterpolationExprContext ctx) {
List<ParseTree> children = ctx.children;
ExprNode[] exprs = new ExprNode[children.size()];
Token[] exprTokens = new Token[children.size()];
for (int i = 0; i < exprs.length; i++) {
ParseTree c = children.get(i);
if (c instanceof TerminalNode) {
Token token = ((TerminalNode) c).getSymbol();
int t = token.getType();
String rawText = c.getText();
String text;
switch(t) {
case KalangLexer.InterpolationPreffixString:
text = rawText.substring(1, rawText.length() - 2);
break;
case KalangLexer.INTERPOLATION_STRING:
text = rawText;
break;
case KalangLexer.RBRACE:
case KalangLexer.INTERPOLATION_END:
case KalangLexer.INTERPOLATION_INTERUPT:
// TODO optimize empty string
text = "";
break;
default:
throw Exceptions.unexceptedValue(t);
}
exprs[i] = new ConstExpr(StringLiteralUtil.parse(text));
exprTokens[i] = token;
} else if (c instanceof ExpressionContext) {
ExprNode expr = this.visitExpression((ExpressionContext) c);
if (expr == null)
return null;
exprs[i] = expr;
exprTokens[i] = ((ExpressionContext) c).getStart();
} else {
throw Exceptions.unexceptedValue(c);
}
}
return this.concatExpressionsToStringExpr(exprs, exprTokens);
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method visitWhileStat.
@Override
public AstNode visitWhileStat(WhileStatContext ctx) {
ExprNode preConditionExpr = visitExpression(ctx.expression());
BlockStmt loopBody = null;
if (ctx.stat() != null) {
loopBody = requireBlock(ctx.stat());
}
LoopStmt ws = new LoopStmt(preConditionExpr, null, loopBody, null);
mapAst(ws, ctx);
return ws;
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class AstBuilder method getNodeById.
@Nullable
private AstNode getNodeById(@Nonnull String name, @Nullable Token token) {
// find local var
LocalVarNode var = this.getNamedLocalVar(name);
if (var != null) {
VarExpr ve = new VarExpr(var, getVarObjectType(var));
if (token != null)
mapAst(ve, token);
return ve;
}
// find parameters
ParameterNode paramNode = this.getNamedParameter(name);
if (paramNode != null) {
ParameterExpr ve = new ParameterExpr(paramNode, this.getVarObjectType(paramNode));
if (token != null)
mapAst(ve, token);
return ve;
}
// find field
ExprNode fieldExpr = this.getObjectFieldExpr(new ThisExpr(this.getThisType()), name, ParserRuleContext.EMPTY);
if (fieldExpr == null)
fieldExpr = this.getStaticFieldExpr(new ClassReference(thisClazz), name, ParserRuleContext.EMPTY);
if (fieldExpr != null)
return fieldExpr;
ExprNode outerClassInstanceExpr = this.getOuterClassInstanceExpr(new ThisExpr(this.getThisType()));
while (outerClassInstanceExpr != null) {
ExprNode fe = this.getObjectFieldExpr(outerClassInstanceExpr, name, ParserRuleContext.EMPTY);
if (fe == null)
fe = this.getStaticFieldExpr(new ClassReference(thisClazz), name, ParserRuleContext.EMPTY);
if (fe != null)
return fe;
outerClassInstanceExpr = this.getOuterClassInstanceExpr(outerClassInstanceExpr);
}
String resolvedTypeName = this.typeNameResolver.resolve(name, topClass, thisClazz);
if (resolvedTypeName != null) {
ClassReference clsRef = new ClassReference(requireAst(resolvedTypeName, token));
if (token != null)
mapAst(clsRef, token);
return clsRef;
}
return null;
}
Aggregations