Exemple #1
0
        public void ParseMultipleOps_OperatorPrecedence_MulAdd()
        {
            var t      = new TermParse();
            var result = t.Parse("2 * 2 + 4");

            Assert.AreEqual(result.Eval(null), 8f);
        }
Exemple #2
0
        public void ParseMultipleOps_Brackets_EvaluatedCorrectly()
        {
            var t      = new TermParse();
            var result = t.Parse("2 * (2 + 4)");

            Assert.AreEqual(result.Eval(null), 12f);
        }
Exemple #3
0
        public void ParseJustAString_YieldsVariableLookupNode()
        {
            var t      = new TermParse();
            var result = t.Parse("ThisIsAVariable");

            Assert.IsTrue(result is VarRefNode);
        }
Exemple #4
0
        public void ParseNoOperators_YieldsStaticValue()
        {
            var t      = new TermParse();
            var result = t.Parse("11");

            Assert.IsTrue(result is StaticValueNode);
        }
Exemple #5
0
        public void ParseAdd_CreatesAdderNode()
        {
            var t      = new TermParse();
            var result = t.Parse("1 + 1");

            Assert.IsTrue(result is AddNode);
        }
Exemple #6
0
        static void Main(string[] args)
        {
            SetupEnvironment();

            TermParse prs = new TermParse();

            while (!done)
            {
                var cmd = Console.ReadLine();

                try
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine(">> " + prs.EvalString(cmd, environment));
                }
                catch (Exception e)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine(e.Message);
                }
                finally
                {
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }
        }
Exemple #7
0
        public void Parse_VarIsEvaluated()
        {
            var vt = new SymbolTable();

            vt.Register("x", 2.0f);
            var t      = new TermParse();
            var result = t.Parse("x * 2");

            Assert.AreEqual(result.Eval(vt), 4.0f);
        }
Exemple #8
0
        public void ParseMultipleOps_OperatorPrecedence_AddMul()
        {
            var t      = new TermParse();
            var result = t.Parse("1 + 1 * 4");

            Assert.IsTrue(result is AddNode);

            var n = result as AddNode;

            Assert.IsTrue(n.Right is MulNode);
            Assert.AreEqual(result.Eval(null), 5f);
        }
Exemple #9
0
        public void Parse_NestedFuncCall_YieldsFuncCallNode()
        {
            var result = new TermParse().Parse("fnord(fark())");

            Assert.IsInstanceOfType(result, typeof(FuncCallNode));
        }
Exemple #10
0
        public void Parse_List_YieldsListNode()
        {
            var lst = new TermParse().Parse("{a, 1, 2, 5}");

            Assert.IsInstanceOfType(lst, typeof(ListNode));
        }
Exemple #11
0
        public void Parse_MultipleParameters_YieldsFuncCallNode()
        {
            var result = new TermParse().Parse("fnord(1, 3, 5 + foo())");

            Assert.IsInstanceOfType(result, typeof(FuncCallNode));
        }