Example #1
0
        public override IEnumerable<PatternNameBinding> Resolve(Context ctx, Type expressionType)
        {
            if (!IsWildcard)
                yield return new PatternNameBinding(Name, expressionType);

            if (Type != null)
            {
                var specifiedType = ctx.ResolveType(Type);
                if(!specifiedType.IsExtendablyAssignableFrom(expressionType) && !expressionType.IsExtendablyAssignableFrom(specifiedType))
                    Error(CompilerMessages.PatternTypeMatchImpossible, specifiedType, expressionType);
            }
        }
Example #2
0
        public override IEnumerable<PatternNameBinding> Resolve(Context ctx, Type expressionType)
        {
            var typeEntity = ctx.FindType(Identifier.FullSignature);
            if(typeEntity == null || (!typeEntity.Kind.IsAnyOf(TypeEntityKind.Type, TypeEntityKind.TypeLabel)))
                Error(Identifier, CompilerMessages.PatternNotValidType, Identifier.FullSignature);

            Type = ctx.ResolveType(Identifier);
            if (!Type.IsExtendablyAssignableFrom(expressionType) && !expressionType.IsExtendablyAssignableFrom(Type))
                Error(CompilerMessages.PatternTypeMatchImpossible, Type, expressionType);

            try
            {
                var field = typeEntity.ResolveField("Tag");
                return LabelRule.Resolve(ctx, field.Type);
            }
            catch(KeyNotFoundException)
            {
                Error(CompilerMessages.PatternTypeNoTag, Identifier.FullSignature);
                return NoBindings();
            }
        }
Example #3
0
        public override IEnumerable<PatternNameBinding> Resolve(Context ctx, Type expressionType)
        {
            var typeEntity = ctx.FindType(Identifier.FullSignature);
            if (typeEntity == null || (!typeEntity.Kind.IsAnyOf(TypeEntityKind.Record)))
                Error(Identifier, CompilerMessages.PatternNotValidRecord, Identifier.FullSignature);

            Type = ctx.ResolveType(Identifier);
            if (!Type.IsExtendablyAssignableFrom(expressionType) && !expressionType.IsExtendablyAssignableFrom(Type))
                Error(CompilerMessages.PatternTypeMatchImpossible, Type, expressionType);

            var duplicate = FieldRules.GroupBy(x => x.Name).FirstOrDefault(x => x.Count() > 1);
            if(duplicate != null)
                Error(CompilerMessages.PatternRecordFieldDuplicated, duplicate.Key);

            var subBindings = new List<PatternNameBinding>();
            foreach (var fieldRule in FieldRules)
            {
                try
                {
                    var field = typeEntity.ResolveField(fieldRule.Name.FullSignature);
                    subBindings.AddRange(fieldRule.Rule.Resolve(ctx, field.Type));
                }
                catch (KeyNotFoundException)
                {
                    Error(fieldRule.Name, CompilerMessages.PatternRecordNoField, Identifier.FullSignature, fieldRule.Name.FullSignature);
                }
            }

            return subBindings;
        }