public ASTNode VisitBinExpr([NotNull] llangParser.ExprContext context) { if (context.multop() != null) { Expression lhs = (Expression)Visit(context.expr(0)); Expression rhs = (Expression)Visit(context.expr(1)); return(new BinaryExprNode(lhs, rhs, context.multop().GetText(), MakeSourceLoc(context))); } else if (context.addop() != null) { Expression lhs = (Expression)Visit(context.expr(0)); Expression rhs = (Expression)Visit(context.expr(1)); return(new BinaryExprNode(lhs, rhs, context.addop().GetText(), MakeSourceLoc(context))); } else if (context.shiftop() != null) { Expression lhs = (Expression)Visit(context.expr(0)); Expression rhs = (Expression)Visit(context.expr(1)); return(new BinaryExprNode(lhs, rhs, context.shiftop().GetText(), MakeSourceLoc(context))); } else if (context.compop() != null) { Expression lhs = (Expression)Visit(context.expr(0)); Expression rhs = (Expression)Visit(context.expr(1)); return(new BinaryExprNode(lhs, rhs, context.compop().GetText(), MakeSourceLoc(context))); } else if (context.eqop() != null) { Expression lhs = (Expression)Visit(context.expr(0)); Expression rhs = (Expression)Visit(context.expr(1)); return(new BinaryExprNode(lhs, rhs, context.eqop().GetText(), MakeSourceLoc(context))); } else if (context.bitwiseop() != null) { Expression lhs = (Expression)Visit(context.expr(0)); Expression rhs = (Expression)Visit(context.expr(1)); return(new BinaryExprNode(lhs, rhs, context.bitwiseop().GetText(), MakeSourceLoc(context))); } else if (context.logicop() != null) { Expression lhs = (Expression)Visit(context.expr(0)); Expression rhs = (Expression)Visit(context.expr(1)); return(new BinaryExprNode(lhs, rhs, context.logicop().GetText(), MakeSourceLoc(context))); } else { throw new NotImplementedException(); } }
public override ASTNode VisitExpr([NotNull] llangParser.ExprContext context) { if (context.Iden() != null && context.expr().Length == 0) { string name = context.Iden().GetText(); if (name == "null") { return(new NullNode(MakeSourceLoc(context))); } return(new IdenExprNode(name, TypeSymbol.INFER_SYMOBOL("__var__"), MakeSourceLoc(context))); } else if (context.Number() != null) { if (context.Number().GetText().Contains(".")) { return(new FloatExprNode(float.Parse(context.Number().GetText()), MakeSourceLoc(context))); } return(new IntExprNode(int.Parse(context.Number().GetText()), MakeSourceLoc(context))); } else if (context.String() != null) { return(new StringExprNode(context.String().GetText().Trim('\"'), MakeSourceLoc(context))); } else if (context.varlist() != null) { Expression l = (Expression)Visit(context.varlist().expr(0)); Expression u = (Expression)Visit(context.varlist().expr(1)); Expression s = context.varlist().expr().Length == 2 ? new IntExprNode(1, MakeSourceLoc(context)) : (Expression)Visit(context.varlist().expr(2)); return(new VarListExprNode(l, u, s, MakeSourceLoc(context))); } else if (context.elementlist() != null) { List <Expression> elems = new List <Expression>(); if (context.elementlist() != null) { for (int i = 0; i < context.elementlist().expr().Length; i++) { elems.Add((Expression)Visit(context.elementlist().expr(i))); } } return(new ConstListExprNode(elems, MakeSourceLoc(context))); } else if (context.unaryop() != null) { Expression operand = (Expression)Visit(context.expr(0)); return(new UnaryExprNode(operand, context.unaryop().GetText(), MakeSourceLoc(context))); } else if (context.multop() != null || context.addop() != null || context.shiftop() != null || context.compop() != null || context.eqop() != null || context.bitwiseop() != null || context.logicop() != null) { return(VisitBinExpr(context)); } else if (context.Iden() != null && context.expr().Length == 1 && context.argslist() == null) { Expression basestruct = (Expression)Visit(context.expr(0)); string fieldname = context.Iden().GetText(); return(new FieldAccessNode(basestruct, fieldname, MakeSourceLoc(context))); } else if (context.Iden() != null && context.expr().Length == 1 && context.argslist() != null) { Expression e = (Expression)Visit(context.expr(0)); List <Expression> args = new List <Expression>() { (Expression)Visit(context.expr(0)) }; if (context.argslist() != null) { for (int i = 0; i < context.argslist().expr().Length; i++) { args.Add((Expression)Visit(context.argslist().expr(i))); } } return(new ImplicitFunCallExprNode(context.Iden().GetText(), e, args, MakeSourceLoc(context))); } else if (context.argslist() != null) { Expression e = (Expression)Visit(context.expr(0)); List <Expression> args = new List <Expression>(); if (context.argslist() != null) { for (int i = 0; i < context.argslist().expr().Length; i++) { args.Add((Expression)Visit(context.argslist().expr(i))); } } return(new FunCallExprNode(e, args, MakeSourceLoc(context))); } else if (context.expr().Length == 2) { Expression array = (Expression)Visit(context.expr(0)); Expression idx = (Expression)Visit(context.expr(1)); return(new ArrayIndexNode(array, idx, MakeSourceLoc(context))); } else if (context.expr().Length == 1) { Expression e = (Expression)Visit(context.expr(0)); return(e); } else if (context.typename() != null) { return(new NewStructNode(context.typename().GetText(), MakeSourceLoc(context))); } else { throw new NotImplementedException(); } }