Example #1
0
        public void HasATypeEqualToTheInferredReturnTypeOfGenericCallableObjects()
        {
            var x = new TypeVariable(123456);

            Type("func([1, 2, 3])", func => Function(new[] { NamedType.Vector(x) }, x)).ShouldEqual(Integer);
            Type("func([true, false])", func => Function(new[] { NamedType.Vector(x) }, x)).ShouldEqual(Boolean);
        }
Example #2
0
        public void ShouldGetDeclaredFuncTypeForFunctionDeclaration()
        {
            var function = @"bool FunctionWithIrrelevantBody(int* a, int[] b) {false}".ParseFunction();

            typeRegistry.DeclaredType(function)
            .ShouldEqual("System.Func",
                         "System.Func<System.Collections.Generic.IEnumerable<int>, Rook.Core.Collections.Vector<int>, bool>",
                         NamedType.Enumerable(NamedType.Integer),
                         NamedType.Vector(NamedType.Integer),
                         NamedType.Boolean);
        }
Example #3
0
        public void CanCreateFullyTypedInstance()
        {
            var vector = (VectorLiteral)Parse("[foo, bar]");

            vector.Items.ShouldHaveTypes(Unknown, Unknown);
            vector.Type.ShouldEqual(Unknown);

            var typedVector = WithTypes(vector, foo => Boolean, bar => Boolean);

            typedVector.Items.ShouldHaveTypes(Boolean, Boolean);
            typedVector.Type.ShouldEqual(NamedType.Vector(Boolean));
        }
Example #4
0
File: Scope.cs Project: 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);
        }
Example #5
0
        public Expression TypeCheck(VectorLiteral vectorLiteral, Scope scope)
        {
            var position = vectorLiteral.Position;
            var items    = vectorLiteral.Items;

            var typedItems = TypeCheck(items, scope);

            var firstItemType = typedItems.First().Type;

            foreach (var typedItem in typedItems)
            {
                Unify(typedItem.Position, firstItemType, typedItem.Type);
            }

            return(new VectorLiteral(position, typedItems, NamedType.Vector(firstItemType)));
        }
Example #6
0
        public NamedType TypeOf(TypeName name)
        {
            if (!types.ContainsKey(name))
            {
                if (name.Name == typeof(IEnumerable <>).QualifiedName())
                {
                    var itemType = TypeOf(name.GenericArguments.Single());
                    if (itemType == null)
                    {
                        return(null);
                    }
                    types.Add(name, NamedType.Enumerable(itemType));
                }
                else if (name.Name == typeof(Vector <>).QualifiedName())
                {
                    var itemType = TypeOf(name.GenericArguments.Single());
                    if (itemType == null)
                    {
                        return(null);
                    }
                    types.Add(name, NamedType.Vector(itemType));
                }
                else if (name.Name == typeof(Nullable <>).QualifiedName())
                {
                    var itemType = TypeOf(name.GenericArguments.Single());
                    if (itemType == null)
                    {
                        return(null);
                    }
                    types.Add(name, NamedType.Nullable(itemType));
                }
                else
                {
                    return(null);
                }
            }

            return(types[name]);
        }
Example #7
0
 public void HasVectorTypeBasedOnTheTypeOfItsItemExpressions()
 {
     Type("[0, 1, 2]").ShouldEqual(NamedType.Vector(Integer));
     Type("[true, false, true]").ShouldEqual(NamedType.Vector(Boolean));
 }