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>()); }
static void Main(string[] args) { var interpreter = new GramInterpreter(); interpreter.Execute("local import \"" + preludePath + "\";"); while (true) { Console.Write("$ "); var input = new StreamReader(Console.OpenStandardInput()); string text = input.ReadLine(); while (text.LastOrDefault() != ';') { Console.Write("...\t"); text += input.ReadLine(); } Console.WriteLine("| " + interpreter.GetParseTree(text)); Console.Write("> "); try { IValue result = interpreter.Execute(text); if (result.Type.RawTypeOf != ValueType.ANY) { Console.WriteLine(result + ": " + result.Type); } Console.WriteLine(); } catch (GramException ex) { Console.WriteLine(ex.GetType().FullName + ": " + ex.Message); } catch (Exception ex) { Console.WriteLine("Exception at runtime: " + ex.ToString()); } } }
public void IncorrectTypeInstantiation() { var i = new GramInterpreter(); i.Execute("var Rect = {Int; Int; Int; Int};"); i.Execute("var box: Rect = Rect{0; 0; 10};"); }
private Environment generateBuiltins() { return(new Environment() { new Binding("print", ValueFactory.make(x => { Console.WriteLine(x); return ValueFactory.make(); })), new Binding("typeof", ValueFactory.make(x => { var t = x.Type; return ValueFactory.make(t); })), new Binding("import", ValueFactory.make((IValue x) => { var path = x.Get <string>(); var script = File.ReadAllText(path); var interpreter = new GramInterpreter(); return interpreter.Execute(script); })), new Binding("Any", ValueFactory.make(Type.Of(ValueType.ANY))), new Binding("length", ValueFactory.make(x => { return ValueFactory.make(x.Get <IEnumerable <IValue> >().Count()); })), new Binding("Int", ValueFactory.make(new Type(ValueType.INTEGER))), new Binding("Type", ValueFactory.make(Type.Of(ValueType.TYPE))), new Binding("String", ValueFactory.make(Type.Of(ValueType.STRING))), new Binding("DEBUG", ValueFactory.make(0)), }); }
public void ReassignType() { var interpreter = new GramInterpreter(); interpreter.Execute("var x: Int = 3;"); interpreter.Execute("x = Int;"); }
public void StringType() { var i = new GramInterpreter(); i.Execute("val str: String = \"Hello, World!\";"); var stdout = new StringWriter(); Console.SetOut(stdout); i.Execute("print(str);"); Assert.AreEqual("Hello, World!\r\n", stdout.ToString()); i.Execute("val hello = \"Hello\"; val world = \"World\";"); stdout = new StringWriter(); Console.SetOut(stdout); i.Execute("print(hello + \", \" + world + \"!\");"); Assert.AreEqual("Hello, World!\r\n", stdout.ToString()); stdout = new StringWriter(); Console.SetOut(stdout); i.Execute("print(hello[3]);"); Assert.AreEqual("l\r\n", stdout.ToString()); }
public void FailedFunctionType() { var i = new GramInterpreter(); i.Execute("var Rect = {Int; Int; Int; Int};"); i.Execute("var embiggen: Rect->Rect = r:Rect->Int=>3;"); }
public void ListAsType() { var i = new GramInterpreter(); i.Execute("val Rect = {{Int; Int}; {Int; Int}};"); i.Execute("val r: Rect = {{0;0}; {10;10}};"); }
public void FunctionType() { var i = new GramInterpreter(); i.Execute("var f: Int->Int = x=>x;"); Assert.AreEqual(5, i.Execute("f 5").Get <int>(), "Base types not working"); }
public void FunctionCall() { var i = new GramInterpreter(); var result = i.Execute("(x=>x*2)(3);"); Assert.AreEqual(6, result.Get <int>(), "Function calls are broken"); }
public void ListDestructure() { var i = new GramInterpreter(); i.Execute("val f = {head; tail...} => head;"); Assert.AreEqual(5, i.Execute("f{5;6;7};").Get <int>()); }
public void Variable() { var i = new GramInterpreter(); i.Execute("val x = 6;"); Assert.AreEqual(6, i.Execute("x").Get <int>(), "The binding cannot hold"); }
public void FunctionLiteral() { var i = new GramInterpreter(); var func = i.Execute("anyVal => 0"); Assert.IsTrue(func.Type.Check(ANTLR2.ValueType.FUNCTION), "Function literal not defining."); }
public void UnderscoreBinding() { var i = new GramInterpreter(); i.Execute("val {x;_;_;_;y;_} = {2;3;4;5;3;6};"); Assert.AreEqual(5, i.Execute("x + y;").Get <int>()); }
public void MiniModule() { var i = new GramInterpreter(); i.Execute("var mod = module {val x = 5};"); Assert.AreEqual(5, i.Execute("mod::x").Get <int>()); }
public void FunctionLateType() { var i = new GramInterpreter(); i.Execute("var Rect = {Int; Int; Int; Int};"); i.Execute("var mkSquare = {x: Int; y: Int; length}=>{x;y;length;length};"); Assert.AreEqual(5, i.Execute("mkSquare({0;0;5})[3];").Get <int>()); }
public void FunctionDestrucuringType() { var i = new GramInterpreter(); i.Execute("var Rect = {Int; Int; Int; Int};"); i.Execute("var mkSquare: {Int; Int; Int}->Rect = {x: Int; y: Int; lengths: Int}=>{x;y;lengths;lengths};"); Assert.AreEqual(5, i.Execute("mkSquare({0;0;5})[3];").Get <int>()); }
public void ModuleValueInFunctionType() { var i = new GramInterpreter(); i.Execute("var mod = module val T = Int;"); i.Execute("var f: mod::T->mod::T = x=>x"); Assert.AreEqual(5, i.Execute("f 5").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 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 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 PatternMatch() { var i = new GramInterpreter(); Assert.AreEqual(5, i.Execute("5 match {x=>x};").Get <int>(), "Basic syntax failure"); Assert.AreEqual(5, i.Execute("5 match {x: Type=>3; x: Int => 5};").Get <int>(), "Typecheck failure"); Assert.AreEqual(5, i.Execute("2 match {x: Int<(x=>x<4)> => x+3; x=>0}").Get <int>(), "Constrained typecheck failure"); }
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 Arithmetic() { var i = new GramInterpreter(); Assert.AreEqual(4, i.Execute("2+2;").Get <int>(), "Addition is broken"); Assert.AreEqual(5, i.Execute("6-1;").Get <int>(), "Subtraction is broken"); Assert.AreEqual(5, i.Execute("10/2;").Get <int>(), "Division is broken"); Assert.AreEqual(9, i.Execute("3*3;").Get <int>(), "Multiplication is broken"); Assert.AreEqual(4, i.Execute("14%5;").Get <int>(), "Modulo is broken"); }
public void If() { var i = new GramInterpreter(); var trueVal = i.Execute("if (1) 5 else 10"); Assert.AreEqual(5, trueVal.Get <int>(), "If true path not followed"); var falseVal = i.Execute("if (1) 5 else 10"); Assert.AreEqual(5, falseVal.Get <int>(), "If false path not taken"); }
public void ListType() { var interpreter = new GramInterpreter(); try { interpreter.Execute("var x: {Int; Int} = {0;1};"); } catch (ANTLR2.TypeException) { Assert.Fail("Initial assignment failed"); } interpreter.Execute("x = {Int; Int};"); }
public void PredicatedType() { var i = new GramInterpreter(); try { i.Execute("var x: Int<x=>x>0> = 1;"); } catch (ANTLR2.TypeException) { Assert.Fail("Initial assignment had invalid type"); } i.Execute("x = -2"); }
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 LogicalOperators() { var i = new GramInterpreter(); var andTrue = i.Execute("1&&1"); var andFalse = i.Execute("1&&0"); var orTrue = i.Execute("1||0"); var orFalse = i.Execute("0||0"); Assert.AreEqual(1, andTrue.Get <int>()); Assert.AreEqual(0, andFalse.Get <int>()); Assert.AreEqual(1, orTrue.Get <int>()); Assert.AreEqual(0, orFalse.Get <int>()); }