private object LookupUniqueField(string name) { INamedValues uniqueRow = null; foreach (var row in _rows.Values) { if (row.Exists(name)) { if (uniqueRow == null) { uniqueRow = row; } else { throw LookupError("Field name '{0}' is not unique; use a qualified name", name); } } } if (uniqueRow == null) { throw LookupError("No such field or function: {0}", name); } return(uniqueRow.GetValue(name)); }
private static void Execute(Program program, INamedValues values, Stack <object> stack) { int count = program.Count; for (int i = 0; i < count; i++) { object item = program[i]; if (item is Instruction instruction) { object value; object other; object result; switch (instruction) { case Instruction.Nop: break; case Instruction.Mark: stack.Push(Instruction.Mark); break; case Instruction.Dup: value = stack.Peek(); stack.Push(value); break; case Instruction.Exch: value = stack.Pop(); other = stack.Pop(); stack.Push(value); stack.Push(other); break; case Instruction.Pop: stack.Pop(); break; case Instruction.Get: value = stack.Pop(); Assert.True(value is string, "Bug: Get: bad type on stack"); value = values.GetValue((string)value); stack.Push(value); break; case Instruction.IsNull: value = stack.Pop(); result = IsNull(value); stack.Push(result); break; case Instruction.NotNull: value = stack.Pop(); result = !IsNull(value); stack.Push(result); break; case Instruction.IsEq: case Instruction.NotEq: case Instruction.Gt: case Instruction.Ge: case Instruction.Lt: case Instruction.Le: value = stack.Pop(); other = stack.Pop(); // Notice the ordering of other and value! result = Compare(other, value, instruction); stack.Push(result); break; case Instruction.Neg: value = stack.Pop(); Assert.True(value is bool, "Bug: Neg: bad type on stack"); result = !((bool)value); stack.Push(result); break; case Instruction.And: result = true; value = stack.Pop(); while (!Equals(value, Instruction.Mark)) { Assert.True(value is bool, "Bug: And: bad type on stack"); if (!((bool)value)) { result = false; } value = stack.Pop(); } stack.Push(result); break; case Instruction.Or: result = false; value = stack.Pop(); while (!Equals(value, Instruction.Mark)) { Assert.True(value is bool, "Bug: Or: bad type on stack"); if ((bool)value) { result = true; } value = stack.Pop(); } stack.Push(result); break; default: throw new AssertionException( string.Format("Bug: invalid instruction: {0}", instruction)); } } else { stack.Push(item); } } }