Example #1
0
        public override IList <Executable> ResolveTypes(ParserContext context, VariableScope varScope)
        {
            this.TryCode = Executable.ResolveTypesForCode(this.TryCode, context, new VariableScope(varScope));
            foreach (CatchBlock cb in this.CatchBlocks)
            {
                ResolvedType exceptionType = this.Parent.DoTypeLookupFailSilently(cb.ExceptionType, context);
                if (exceptionType == null)
                {
                    throw new ParserException(cb.ExceptionType.FirstToken, "Exception type not found.");
                }
                if (!exceptionType.IsException(context))
                {
                    throw new ParserException(cb.ExceptionType.FirstToken, "This type does not extend from System.Exception");
                }
                VariableScope catchVarScope = new VariableScope(varScope);
                if (cb.ExceptionVariable != null)
                {
                    catchVarScope.DeclareVariable(cb.ExceptionVariable.Value, exceptionType);
                }
                cb.Code = Executable.ResolveTypesForCode(cb.Code, context, catchVarScope);
            }

            if (this.FinallyCode != null)
            {
                this.FinallyCode = Executable.ResolveTypesForCode(this.FinallyCode, context, new VariableScope(varScope));
            }
            return(Listify(this));
        }
Example #2
0
        public Expression ResolveTypesWithExteriorHint(
            ParserContext context,
            VariableScope varScope,
            ResolvedType[] expectedArgsAndReturnTypes)
        {
            VariableScope lambdaScope = new VariableScope(varScope);

            if (this.Args.Length != expectedArgsAndReturnTypes.Length - 1)
            {
                throw new ParserException(this.ArrowToken, "The expected number of args was " + (expectedArgsAndReturnTypes.Length - 1));
            }

            for (int i = 0; i < this.Args.Length; ++i)
            {
                lambdaScope.DeclareVariable(this.Args[i].Value, expectedArgsAndReturnTypes[i]);
            }

            this.Code = Executable.ResolveTypesForCode(this.Code, context, lambdaScope);
            ResolvedType returnType = null;

            if (this.Code.Length == 1 && this.Code[0] is ExpressionAsExecutable)
            {
                returnType = ((ExpressionAsExecutable)this.Code[0]).Expression.ResolvedType;
            }
            else if (this.Code.Length > 0 && this.Code[this.Code.Length - 1] is ReturnStatement)
            {
                ReturnStatement ret = (ReturnStatement)this.Code[this.Code.Length - 1];
                if (ret.Value == null)
                {
                    throw new ParserException(ret.FirstToken, "Return statement in lambda must have a value.");
                }
                returnType = ret.Value.ResolvedType;
            }
            else
            {
                throw new ParserException(this.FirstToken, "Not implemented: the return is hiding in this lambda.");
            }

            ResolvedType expectedReturnType = expectedArgsAndReturnTypes[expectedArgsAndReturnTypes.Length - 1];

            if (expectedReturnType == null)
            {
                expectedReturnType = returnType;
            }
            else
            {
                if (!returnType.CanBeAssignedTo(expectedReturnType, context))
                {
                    throw new ParserException(this.FirstToken, "This lambda does not seem to be returning the expected type.");
                }
            }

            List <ResolvedType> argTypes = new List <ResolvedType>(expectedArgsAndReturnTypes);

            argTypes.RemoveAt(argTypes.Count - 1);

            this.ResolvedType = ResolvedType.CreateFunction(expectedReturnType, argTypes.ToArray());
            return(this);
        }
Example #3
0
        public override void ResolveTypesInCode(ParserContext context)
        {
            VariableScope varScope = new VariableScope();

            for (int i = 0; i < this.ArgNames.Length; ++i)
            {
                varScope.DeclareVariable(this.ArgNames[i].Value, this.ResolvedArgTypes[i]);
            }
            this.Code = Executable.ResolveTypesForCode(this.Code, context, varScope);
        }
Example #4
0
        public override void ResolveTypesInCode(ParserContext context)
        {
            PropertyDefinition propertyDef = (PropertyDefinition)this.Parent;
            VariableScope      varScope    = new VariableScope();

            if (this.IsSetter)
            {
                varScope.DeclareVariable("value", propertyDef.ResolvedType);
            }

            if (propertyDef.IsIndex)
            {
                varScope.DeclareVariable(propertyDef.IndexVariableName.Value, propertyDef.IndexResolvedType);
            }

            if (this.Code != null)
            {
                this.Code = Executable.ResolveTypesForCode(this.Code, context, varScope);
            }
        }
Example #5
0
        public override IList <Executable> ResolveTypes(ParserContext context, VariableScope varScope)
        {
            this.ListExpression = this.ListExpression.ResolveTypes(context, varScope);
            VariableScope loopScope = new VariableScope(varScope);
            ResolvedType  itemType  = this.ListExpression.ResolvedType.GetEnumerableItemType();

            if (itemType == null)
            {
                throw new ParserException(this.ListExpression.FirstToken, "This expression is not enumerable.");
            }
            loopScope.DeclareVariable(this.VariableToken.Value, itemType);
            this.Code = Executable.ResolveTypesForCode(this.Code, context, loopScope);
            return(Listify(this));
        }
        public override IList <Executable> ResolveTypes(ParserContext context, VariableScope varScope)
        {
            this.ResolvedType = this.DoTypeLookup(this.Type, context);
            if (this.InitialValue != null)
            {
                this.InitialValue = this.InitialValue.ResolveTypes(context, varScope);
            }
            else
            {
                if (this.ResolvedType.FrameworkClass != null ||
                    this.ResolvedType.CustomType != null ||
                    this.ResolvedType.PrimitiveType == "string")
                {
                    this.InitialValue = new NullConstant(this.FirstToken, this.Parent);
                    this.InitialValue.ResolvedType = this.ResolvedType;
                }
                else if (this.ResolvedType.PrimitiveType != null)
                {
                    switch (this.ResolvedType.PrimitiveType)
                    {
                    case "int":
                        this.InitialValue = new IntegerConstant(this.FirstToken, 0, this.Parent)
                        {
                            ResolvedType = ResolvedType.Int()
                        };
                        break;

                    case "double":
                        this.InitialValue = new DoubleConstant(this.FirstToken, 0.0, this.Parent)
                        {
                            ResolvedType = ResolvedType.Double()
                        };
                        break;

                    default:
                        throw new System.NotImplementedException();
                    }
                }
                else
                {
                    throw new System.NotImplementedException();
                }
            }

            varScope.DeclareVariable(this.VariableName.Value, this.ResolvedType);

            return(Listify(this));
        }