private object EvaluteMultiIndexingExpression(MultiIndexingExpression e, VariableContext context) { object value = Evaluate(e.Operand, context); int[] indexers = e.Indexers.Select(i => Types.ConvertValue <int>(Evaluate(i, context))).ToArray(); if (e.Operand.Type.IsArray) { Array array = value as Array; return(array.GetValue(indexers[0])); } else if (e.Operand.Type.Equals(typeof(Matrix))) { Matrix m = value as Matrix; if (indexers.Length == 1) { return(m.GetRow(indexers[0] - 1)); } else { return(m[indexers[0] - 1, indexers[1] - 1]); } } else { Vector v = value as Vector; return(v[indexers[0] - 1]); } }
protected override Expression CompileIndexingExpression(Expression operand, int lambdaLevel) { PushPosition(); Move(); List <Expression> indexers = new List <Expression>(); while (true) { indexers.Add(Compile(lambdaLevel)); if (IsPunctuationOf(",")) { Move(); } else { SkipPunctuation("]"); break; } } Expression result = new MultiIndexingExpression(operand, indexers.ToArray(), PeekPos(), Pos); PopPosition(); return(result); }
private void CheckMultiIndexingExpression(MultiIndexingExpression e, TypeCheckingContext context) { PerformTypeChecking(e.Operand, context); foreach (Expression indexer in e.Indexers) { PerformTypeChecking(indexer, context); } if (!e.Indexers.All(i => Types.IsNumberType(i.Type))) { context.ErrorProvider.ThrowException("Matrix multi indexer parameters must be of number types.", e); } if (e.Operand.Type.IsArray) { if (e.Indexers.Length != 1) { context.ErrorProvider.ThrowException("Array multi indexer must be of 1 parameter.", e); } else { e.Type = e.Operand.Type.GetElementType(); } } else if (e.Operand.Type == typeof(Matrix)) { if (e.Indexers.Length == 1) { e.Type = typeof(double[]); } else if (e.Indexers.Length == 2) { e.Type = typeof(double); } else { context.ErrorProvider.ThrowException("Matrix multi indexer must be of 1 or 2 parameters.", e); } } else if (e.Operand.Type == typeof(Vector)) { if (e.Indexers.Length == 1) { e.Type = typeof(double); } else { context.ErrorProvider.ThrowException("Vector multi indexer must be of 1 parameter.", e); } } else { context.ErrorProvider.ThrowException("Only matrix can use multi indexer.", e); } }