示例#1
0
        public void Assignment()
        {
            var interpreter = new GramInterpreter();

            interpreter.Execute("var x = 5;");
            Assert.AreEqual(5, interpreter.GetVariable("x").Get <int>(), "Basic assignment is broken.");
            interpreter.Execute("x = 6");
            Assert.AreEqual(6, interpreter.GetVariable("x").Get <int>(), "Reassigning variables is broken");
        }
示例#2
0
        public void LocalImport()
        {
            var i = new GramInterpreter();

            i.Execute("local module val x = 5;");
            Assert.AreEqual(4, i.Execute("y where local module val y = 4;").Get <int>());
            Assert.AreEqual(5, i.GetVariable("x").Get <int>());
            try {
                Assert.AreNotEqual(4, i.GetVariable("y").Get <int>());
                Assert.Fail();
            } catch (ANTLR2.GramException) {
            }
        }
示例#3
0
        public void Assignment_Readonly()
        {
            var interpreter = new GramInterpreter();

            interpreter.Execute("val x = 5;");
            Assert.AreEqual(5, interpreter.GetVariable("x").Get <int>(), "Readonly assignment broken.");
            try {
                interpreter.Execute("x = 6;");
            } catch (ANTLR2.GramException e) {
                Assert.AreEqual(5, interpreter.GetVariable("x").Get <int>(), "Readonly variable was reassigned!");
                throw e;
            }
        }
示例#4
0
        public void Comment()
        {
            var i = new GramInterpreter();

            i.Execute("var x = 0; #{set x to 5}# x = 5;");
            Assert.AreEqual(5, i.GetVariable("x").Get <int>());
        }
示例#5
0
        public void For()
        {
            var i = new GramInterpreter();

            i.Execute("var total = 0;");
            i.Execute("for (num : {0;1;2;3}) total = total + num;");
            Assert.AreEqual(6, i.GetVariable("total").Get <int>(), "For loop not iterating correctly");
        }
示例#6
0
        public void While()
        {
            var i = new GramInterpreter();

            i.Execute("var total = 0;");
            i.Execute("var count = 3;");
            i.Execute("while (count > 0) {total = total + count; count = count - 1;}");
            Assert.AreEqual(6, i.GetVariable("total").Get <int>(), "While loop not iterating correctly.");
        }
示例#7
0
        public void TypeInstantiation()
        {
            var i = new GramInterpreter();

            i.Execute("var x = Int(5);");
            Assert.AreEqual(5, i.GetVariable("x").Get <int>());

            i.Execute("var Rect = {Int; Int; Int; Int};");
            i.Execute("var box: Rect = Rect{0; 0; 10; 10};");
        }
示例#8
0
        public void DestructuringAssignment()
        {
            var i = new GramInterpreter();

            i.Execute("var {a; b; {c; {d; e}; {f; g}}} = {1; 2; {3; {4; 5}; {6; 7}}};");
            var resultMap = new Dictionary <char, int> {
                { 'a', 1 },
                { 'b', 2 },
                { 'c', 3 },
                { 'd', 4 },
                { 'e', 5 },
                { 'f', 6 },
                { 'g', 7 },
            };

            Assert.IsTrue(resultMap.All(kvP => i.GetVariable(kvP.Key.ToString()).Get <int>() == kvP.Value), "Destructuring assignment doesn't work");
        }