Пример #1
0
        public void TestInfix()
        {
            IParseLineDelimiters pld = new TestDelims();
            IScope scope = new TestScope();

            {
                DelimiterList list = ParseLine.Do("3 + 8", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(11, value.AsInt);
            }
            {
                DelimiterList list = ParseLine.Do("4 + 2 + 7", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(13, value.AsInt);
            }
            {	// precedence should work as expected
                DelimiterList list = ParseLine.Do("4 + 2 * 7", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(18, value.AsInt);
            }
            {	// precedence comes into play
                DelimiterList list = ParseLine.Do("4 * 2 + 7", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(15, value.AsInt);
            }
            {
                DelimiterList list = ParseLine.Do("3 + x", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(9, value.AsInt);
            }
        }
Пример #2
0
        public void TestFunction()
        {
            IParseLineDelimiters pld = new TestDelims();
            IScope scope = new TestScope();

            {	// 2 + 4 * 5
                DelimiterList list = ParseLine.Do("2 + create-multiplier 4  5", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(22, value.AsInt);
            }
        }
Пример #3
0
        public void TestAllfix()
        {
            IParseLineDelimiters pld = new TestDelims();
            IScope scope = new TestScope();

            {
                DelimiterList list = ParseLine.Do("triple 2 + 3 doubled", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(12, value.AsInt);
            }
        }
Пример #4
0
        public void TestNested()
        {
            IParseLineDelimiters pld = new TestDelims();
            IScope scope = new TestScope();

            {
                DelimiterList list = ParseLine.Do("2 * ( 3 + 4 )", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(14, value.AsInt);
            }

            {
                DelimiterList list = ParseLine.Do("2 * ( 3 + ( 2 * 4 ) doubled + triple ( 2 + 3 ) )", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(68, value.AsInt);
            }
        }
Пример #5
0
        public void TestPostfix()
        {
            IParseLineDelimiters pld = new TestDelims();
            IScope scope = new TestScope();

            {
                DelimiterList list = ParseLine.Do("4 doubled", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(8, value.AsInt);
            }
            {	// last doubled runs first, asks for the previous value & triggers first doubled
                DelimiterList list = ParseLine.Do("4 doubled doubled", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(16, value.AsInt);
            }
        }
Пример #6
0
        public void TestPrefix()
        {
            IParseLineDelimiters pld = new TestDelims();
            IScope scope = new TestScope();

            {
                DelimiterList list = ParseLine.Do("triple 3", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(9, value.AsInt);
            }
            {
                DelimiterList list = ParseLine.Do("triple triple 2", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(18, value.AsInt);
            }
            {
                DelimiterList list = ParseLine.Do("triple x", pld);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(18, value.AsInt);
            }
        }