public void ListInit() { var expression = LinqExpression.ListInit( LinqExpression.New( typeof(List <long>)), LinqExpression.ElementInit( typeof(List <long>).GetMethod("Add"), LinqExpression.Constant(0L))); ShouldRoundrip(expression); }
public void MemberInit_list_bind() { var expression = LinqExpression.MemberInit( LinqExpression.New( typeof(SampleClass)), LinqExpression.ListBind( typeof(SampleClass).GetProperty(nameof(SampleClass.ListProperty)), LinqExpression.ElementInit( typeof(List <long>).GetMethod("Add"), LinqExpression.Constant(0L)))); ShouldRoundrip(expression); }
Exp ParseUnit(List <Token> expr) { Token tok; if (expr.Count == 0) { return(NoneExp); } else if (expr.Count == 1) { tok = expr[0]; switch (tok.Type) { case TokenType.Identifier: // keywords switch (tok.Value) { case "None": return(NoneExp); case "True": return(TrueExp); case "False": return(FalseExp); } // user identifiers if (Local != null && Local.TryGetValue(tok.Value, out Exp value)) { return(value); } else { return(GlobalAccess(tok.Value)); } case TokenType.Number: if (int.TryParse(tok.Value, out int i)) { return(Exp.Constant(new Int(i), Any)); } else if (double.TryParse(tok.Value, out double d)) { return(Exp.Constant(new Float(d), Any)); } break; case TokenType.String: return(Exp.Constant(new String(tok.Value), Any)); case TokenType.Parenthesis: { var w = Split(tok.Subset, TokenType.Comma); if (w.Count == 1) // parenthesis { return(Parse(w[0])); } // tuple if (w.Count == 0) { return(Exp.New(typeof(Tuple))); } else { return(Exp.New(typeof(Tuple).GetConstructor(new[] { typeof(Object[]) }), Exp.NewArrayInit(typeof(Object), w.Select(Parse)))); } } case TokenType.Brackets: { var w = Split(tok.Subset, TokenType.Comma); if (w.Count == 0) { return(Exp.New(typeof(List))); } else { return(Exp.New(typeof(List).GetConstructor(new[] { typeof(Object[]) }), Exp.NewArrayInit(typeof(Object), w.Select(Parse)))); } } case TokenType.Braces: { var w = Split(tok.Subset, TokenType.Comma); if (Contains(w[0], TokenType.Colon)) { // dict if (w.Count == 0) { return(Exp.New(typeof(Dict))); } else { var items = new List <ElementInit>(); var adder = typeof(Dictionary <Object, Object>).GetMethod("Add"); foreach (var v in w) { var row = Split(v, TokenType.Colon); var left = Parse(row[0]); var right = Parse(row[1]); items.Add(Exp.ElementInit(adder, left, right)); } return(Exp.New(typeof(Dict).GetConstructor(new[] { typeof(Dictionary <Object, Object>) }), Exp.ListInit(Exp.New(typeof(Dictionary <Object, Object>)), items))); } } else { // set if (w.Count == 0) { return(Exp.New(typeof(Set))); } else { return(Exp.New(typeof(Set).GetConstructor(new[] { typeof(Object[]) }), Exp.NewArrayInit(typeof(Object), w.Select(Parse)))); } } } } } else // length > 1 { tok = expr[0]; // starts if (tok.Type == TokenType.Operator) { expr.RemoveAt(0); Exp obj = ParseUnit(expr); switch (tok.op) { case Op.Invert: return(Exp.Call(obj, typeof(Object).GetMethod("__invert__"))); case Op.Add: return(Exp.Call(obj, typeof(Object).GetMethod("__pos__"))); case Op.Subtract: return(Exp.Call(obj, typeof(Object).GetMethod("__neg__"))); case Op.Not: return(Exp.Call(obj, typeof(Object).GetMethod("__not__"))); default: throw new Exception("invalid unary operator"); } } tok = expr[^ 1]; // ends
private ElementInit VisitElementInitializer(ElementInit initializer) { var arguments = VisitExpressionList(initializer.Arguments); return(arguments != initializer.Arguments ? Expression.ElementInit(initializer.AddMethod, arguments) : initializer); }