Пример #1
0
        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));
        }
Пример #2
0
        //public bool Match([NotNull] IRowValues row)
        //{
        //	Assert.ArgumentNotNull(row, nameof(row));

        //	return Match(new NamedValues(row, _valueCache, _indexCache));
        //}

        public bool Match([NotNull] INamedValues values)
        {
            Assert.ArgumentNotNull(values, nameof(values));

            _stack.Clear();
            //_valueCache.Clear();
            // do NOT clear the index cache

            Execute(_program, values, _stack);

            Assert.AreEqual(1, _stack.Count,
                            "Bug: Stack has {0} item(s), expected 1", _stack.Count);

            object result = _stack.Pop();

            Assert.True(result is bool, "Bug: Result on stack is not of type bool");

            return((bool)result);
        }
Пример #3
0
 public ImplicitValue DefineFields([NotNull] INamedValues values,
                                   [CanBeNull] string qualifier = null)
 {
     _environment.DefineFields(values, qualifier);
     return(this);
 }
Пример #4
0
 /// <summary>
 /// Create bindings in this environment associating the given
 /// values with their names (optionally qualified with the
 /// given <paramref name="qualifier"/>.
 /// </summary>
 /// <param name="values">The name/value pairs (required)</param>
 /// <param name="qualifier">The qualifier (optional)</param>
 public void DefineFields(INamedValues values, string qualifier = null)
 {
     Assert.ArgumentNotNull(values, nameof(values));
     _rows[qualifier ?? string.Empty] = values;
 }
Пример #5
0
        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);
                }
            }
        }