示例#1
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>());
        }
示例#2
0
        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());
                }
            }
        }
示例#3
0
        public void IncorrectTypeInstantiation()
        {
            var i = new GramInterpreter();

            i.Execute("var Rect = {Int; Int; Int; Int};");
            i.Execute("var box: Rect = Rect{0; 0; 10};");
        }
示例#4
0
 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)),
     });
 }
示例#5
0
        public void ReassignType()
        {
            var interpreter = new GramInterpreter();

            interpreter.Execute("var x: Int = 3;");
            interpreter.Execute("x = Int;");
        }
示例#6
0
        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());
        }
示例#7
0
        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;");
        }
示例#8
0
        public void ListAsType()
        {
            var i = new GramInterpreter();

            i.Execute("val Rect = {{Int; Int}; {Int; Int}};");
            i.Execute("val r: Rect = {{0;0}; {10;10}};");
        }
示例#9
0
        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");
        }
示例#10
0
        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");
        }
示例#11
0
        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>());
        }
示例#12
0
        public void Variable()
        {
            var i = new GramInterpreter();

            i.Execute("val x = 6;");
            Assert.AreEqual(6, i.Execute("x").Get <int>(), "The binding cannot hold");
        }
示例#13
0
        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.");
        }
示例#14
0
        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>());
        }
示例#15
0
        public void MiniModule()
        {
            var i = new GramInterpreter();

            i.Execute("var mod = module {val x = 5};");
            Assert.AreEqual(5, i.Execute("mod::x").Get <int>());
        }
示例#16
0
        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>());
        }
示例#17
0
        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>());
        }
示例#18
0
        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>());
        }
示例#19
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");
        }
示例#20
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");
        }
示例#21
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.");
        }
示例#22
0
        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");
        }
示例#23
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};");
        }
示例#24
0
        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");
        }
示例#25
0
        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");
        }
示例#26
0
        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};");
        }
示例#27
0
        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");
        }
示例#28
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) {
            }
        }
示例#29
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;
            }
        }
示例#30
0
        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>());
        }