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"); }
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) { } }
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; } }
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>()); }
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"); }
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."); }
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};"); }
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"); }