public BELArray SortBy(ExecutionContext ctx, Block block) { ArrayList answer = new ArrayList(Array); try { answer.Sort(new CustomSorter(block, ctx)); } catch (InvalidOperationException e) { throw e.InnerException; } return new BELArray(answer); }
public CustomSorter(Block b, ExecutionContext c) { block = b; ctx = c; }
public BELArray Collect(ExecutionContext ctx, Block block) { BELArray answer = new BELArray(); foreach (IBELObject each in Array) { ArrayList parms = new ArrayList(); parms.Add(each); answer.Add(block.Value(ctx, parms)); } return answer; }
public BELArray Select(ExecutionContext ctx, Block block) { BELArray answer = new BELArray(); foreach (IBELObject each in Array) { ArrayList parms = new ArrayList(); parms.Add(each); IBELObject objValue = block.Value(ctx, parms); BELBoolean test = objValue as BELBoolean; if (test == null) throw new ExecutionException("Select block must evaluate to a boolean. Got " + BELType.BELTypeForType(objValue.GetType()).ExternalTypeName + " instead."); if (test.Value) answer.Add(each); } return answer; }
public IBELObject WhileTrue(ExecutionContext ctx, Block block) { if (ParameterCount != 0) throw new ArgumentException("Conditional block for WhileTrue can't accept parameters."); return Loop(ctx, block, true); }
IBELObject Loop(ExecutionContext ctx, Block block, bool halt) { while (true) { IBELObject objValue = Value(ctx); BELBoolean test = objValue as BELBoolean; if (test == null) throw new ExecutionException(ctx.CurrentLocation, "WhileTrue/WhileFalse block must evaluate to a boolean. Got " + BELType.BELTypeForType(objValue.GetType()).ExternalTypeName + " instead."); if (test.Value == halt) break; } return BELString.Empty; }
public IBELObject IfNotNull(ExecutionContext ctx, Block block) { return Instance; }
public IBELObject IfNotNullElse(ExecutionContext ctx, Block notNullBlock, Block nullBlock) { return nullBlock.Value(ctx); }
public IBELObject IfNull(ExecutionContext ctx, Block block) { return block.Value(ctx); }
public BELArray SortBy(ExecutionContext ctx, Block block) { // This code formerly sorted the list by having the IComparer do the comparison. // This was resulting in lots of superficial comparisons, as the block was being // evaluated lots of extra times - for the left and the right item on every comparison. // Instead, I changed it to rip through the array, evaluate the block exaclty once // for each item, and then do the sort on just the values. List<BelArrayEntry> entries = new List<BelArrayEntry>(); try { foreach (IBELObject entry in Array) { ArrayList a1 = new ArrayList(); a1.Add(entry); IBELObject value = block.Value(ctx, a1); IComparable comparable = value as IComparable; if (comparable == null) { throw new ExecutionException(null, "Can not compare objects of type " + BELType.ExternalTypeNameForType(value.GetType())); } entries.Add(new BelArrayEntry(entry, comparable)); } entries.Sort(new BelArrayEntry.Comparer()); } catch (InvalidOperationException e) { throw e.InnerException; } BELArray results = new BELArray(); foreach (BelArrayEntry entry in entries) { results.Add(entry.Entry); } return results; }
public virtual IBELObject IfNull(ExecutionContext ctx, Block block) { return UndefinedObject.Instance; }
public IBELObject IfFalse(ExecutionContext ctx, Block block) { if (!Value) return block.Value(ctx); else return UndefinedObject.Instance; }
public IBELObject IfTrueIfFalse(ExecutionContext ctx, Block trueBlock, Block falseBlock) { if (Value) return trueBlock.Value(ctx); else return falseBlock.Value(ctx); }