Пример #1
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();
            }
        }
Пример #2
0
        protected override Type resolve(Context ctx, bool mustReturn)
        {
            if(Identifier == "_")
                error(CompilerMessages.UnderscoreNameUsed);

            // local variable
            var local = Local ?? ctx.Scope.FindLocal(Identifier);
            if (local != null)
            {
                // only local constants are cached
                // because mutable variables could be closured later on
                if (local.IsConstant && local.IsImmutable && ctx.Options.UnrollConstants)
                    _LocalConstant = local;

                return local.Type;
            }

            // static function declared in the script
            try
            {
                var methods = ctx.MainType.ResolveMethodGroup(Identifier);
                if (methods.Length > 1)
                    error(CompilerMessages.FunctionInvocationAmbiguous, Identifier);

                _Method = methods[0];
                return FunctionalHelper.CreateFuncType(_Method.ReturnType, _Method.GetArgumentTypes(ctx));
            }
            catch (KeyNotFoundException) { }

            // algebraic type without a constructor
            var type = ctx.FindType(Identifier);
            if (type != null && type.Kind == TypeEntityKind.TypeLabel)
            {
                try
                {
                    type.ResolveConstructor(new Type[0]);
                    _Type = type;
                    return _Type.TypeInfo;
                }
                catch (KeyNotFoundException) { }
            }

            // global property
            try
            {
                _Property = ctx.ResolveGlobalProperty(Identifier);
                return _Property.PropertyType;
            }
            catch (KeyNotFoundException)
            {
                error(CompilerMessages.IdentifierNotFound, Identifier);
            }

            return typeof (UnitType);
        }
Пример #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;
        }