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); }
public void ShouldGetNestedClosedTypesForWellKnownGenericTypes() { var nestedTypeName = TypeName.Vector( TypeName.Enumerable( TypeName.Nullable(TypeName.Integer))); var nestedType = typeRegistry.TypeOf(nestedTypeName); nestedType.ShouldEqual("Rook.Core.Collections.Vector", "Rook.Core.Collections.Vector<System.Collections.Generic.IEnumerable<Rook.Core.Nullable<int>>>", NamedType.Enumerable(NamedType.Nullable(NamedType.Integer))); }
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); }
public void ShouldGetMemberBindingsWhenGivenTheNamedTypeOfRegisteredClasses() { var math = "class Math { int Square(int x) {x*x} bool Zero(int x) {x==0} int Max(int* ints) {0} }".ParseClass(); var mathType = new NamedType(math); typeRegistry.Add(math); var members = typeRegistry.MembersOf(mathType); var square = members[0]; var zero = members[1]; var max = members[2]; square.Identifier.ShouldEqual("Square"); square.Type.ShouldEqual(NamedType.Function(new[] { NamedType.Integer }, NamedType.Integer)); zero.Identifier.ShouldEqual("Zero"); zero.Type.ShouldEqual(NamedType.Function(new[] { NamedType.Integer }, NamedType.Boolean)); max.Identifier.ShouldEqual("Max"); max.Type.ShouldEqual(NamedType.Function(new[] { NamedType.Enumerable(NamedType.Integer) }, NamedType.Integer)); }
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]); }