Esempio n. 1
0
File: Scope.cs Progetto: plioi/rook
        public GlobalScope()
        {
            globals = new BindingDictionary();

            DataType @int  = NamedType.Integer;
            DataType @bool = NamedType.Boolean;

            DataType integerOperation  = NamedType.Function(new[] { @int, @int }, @int);
            DataType integerComparison = NamedType.Function(new[] { @int, @int }, @bool);
            DataType booleanOperation  = NamedType.Function(new[] { @bool, @bool }, @bool);

            globals["<"]  = integerComparison;
            globals["<="] = integerComparison;
            globals[">"]  = integerComparison;
            globals[">="] = integerComparison;
            globals["=="] = integerComparison;
            globals["!="] = integerComparison;

            globals["+"] = integerOperation;
            globals["*"] = integerOperation;
            globals["/"] = integerOperation;
            globals["-"] = integerOperation;

            globals["&&"] = booleanOperation;
            globals["||"] = booleanOperation;
            globals["!"]  = NamedType.Function(new[] { @bool }, @bool);

            var T = TypeVariable.CreateGeneric(); //TypeVariable 0
            var S = TypeVariable.CreateGeneric(); //TypeVariable 1

            var enumerableT = NamedType.Enumerable(T);
            var nullableT   = NamedType.Nullable(T);
            var vectorT     = NamedType.Vector(T);
            var enumerableS = NamedType.Enumerable(S);

            globals["??"]                   = NamedType.Function(new DataType[] { nullableT, T }, T);
            globals["Print"]                = NamedType.Function(new[] { T }, NamedType.Void);
            globals["Nullable"]             = NamedType.Function(new[] { T }, nullableT);
            globals["First"]                = NamedType.Function(new[] { enumerableT }, T);
            globals["Take"]                 = NamedType.Function(new[] { enumerableT, @int }, enumerableT);
            globals["Skip"]                 = NamedType.Function(new[] { enumerableT, @int }, enumerableT);
            globals["Any"]                  = NamedType.Function(new[] { enumerableT }, @bool);
            globals["Count"]                = NamedType.Function(new[] { enumerableT }, @int);
            globals["Select"]               = NamedType.Function(new[] { enumerableT, NamedType.Function(new[] { T }, S) }, enumerableS);
            globals["Where"]                = NamedType.Function(new[] { enumerableT, NamedType.Function(new[] { T }, @bool) }, enumerableT);
            globals["Each"]                 = NamedType.Function(new[] { vectorT }, enumerableT);
            globals[ReservedName.__index__] = NamedType.Function(new[] { vectorT, @int }, T);
            globals[ReservedName.__slice__] = NamedType.Function(new[] { vectorT, @int, @int }, vectorT);
            globals["Append"]               = NamedType.Function(new DataType[] { vectorT, T }, vectorT);
            globals["With"]                 = NamedType.Function(new[] { vectorT, @int, T }, vectorT);
        }
Esempio n. 2
0
        public void HasATypeInWhichOnlyGenericTypeVariablesAreFreshenedOnEachScopeLookup()
        {
            using (TypeVariable.TestFactory())
            {
                //Prevent type '1' from being freshened on type lookup by marking it as non-generic:
                var typeVariable0 = TypeVariable.CreateGeneric();
                var typeVariable1 = TypeVariable.CreateNonGeneric();

                var expectedTypeAfterLookup = new NamedType("A", new TypeVariable(8), typeVariable1, new NamedType("B", new TypeVariable(8), typeVariable1));
                var definedType             = new NamedType("A", typeVariable0, typeVariable1, new NamedType("B", typeVariable0, typeVariable1));

                var typeChecker = new TypeChecker();
                var globalScope = new GlobalScope();
                var localScope  = new LocalScope(globalScope);
                localScope.Bind("foo", definedType);

                Type("foo", localScope, typeChecker).ShouldEqual(expectedTypeAfterLookup);
            }
        }
Esempio n. 3
0
        public Expression TypeCheck(Null nullLiteral, Scope scope)
        {
            var Position = nullLiteral.Position;

            return(new Null(Position, NamedType.Nullable(TypeVariable.CreateGeneric())));
        }