private void GetInitialValue()
        {
            // Get the initial value.
            if (_initalValueContext != null)
            {
                ParseInfo parseInfo = this.parseInfo;

                // Store the initial value's restricted calls.
                RestrictedCallList restrictedCalls = null;
                if (_handleRestrictedCalls)
                {
                    restrictedCalls = new RestrictedCallList();
                    parseInfo       = parseInfo.SetRestrictedCallHandler(restrictedCalls);
                }

                // Parse the initial value.
                InitialValue = parseInfo.SetExpectingLambda(CodeType).GetExpression(_operationalScope, _initalValueContext);

                // If the initial value's type is constant, make sure the constant type's implements the variable's type.
                if (InitialValue?.Type() != null && InitialValue.Type().IsConstant() && !InitialValue.Type().Implements(CodeType))
                {
                    parseInfo.Script.Diagnostics.Error($"The type '{InitialValue.Type().Name}' cannot be stored.", _initalValueContext.Range);
                }

                // If the variable's type is constant, make sure the value's type matches.
                else if (CodeType != null && CodeType.IsConstant() && (InitialValue.Type() == null || !InitialValue.Type().Implements(CodeType)))
                {
                    parseInfo.Script.Diagnostics.Error($"Expected a value of type '" + CodeType.GetName() + "'", _initalValueContext.Range);
                }

                // Check restricted calls.
                if (_handleRestrictedCalls)
                {
                    foreach (RestrictedCall call in restrictedCalls)
                    {
                        // If the variable type is global, or the variable type is player and the restricted call type is not player...
                        if (VariableType == VariableType.Global ||
                            (VariableType == VariableType.Player && call.CallType != RestrictedCallType.EventPlayer))
                        {
                            // ... then add the error.
                            parseInfo.Script.Diagnostics.Error(call.Message, call.CallRange.range);
                        }
                    }
                }
            }
        }
        public static ParameterParseResult GetParameters(ParseInfo parseInfo, Scope methodScope, DeltinScriptParser.SetParametersContext context, bool subroutineParameter)
        {
            if (context == null)
            {
                return(new ParameterParseResult(new CodeParameter[0], new Var[0]));
            }

            var parameters = new CodeParameter[context.define().Length];
            var vars       = new Var[parameters.Length];

            for (int i = 0; i < context.define().Length; i++)
            {
                Var newVar;

                CodeParameter parameter = new CodeParameter(context.define(i).name.Text);

                // Set up the context handler.
                IVarContextHandler contextHandler = new DefineContextHandler(parseInfo.SetRestrictedCallHandler(parameter), context.define(i));

                // Normal parameter
                if (!subroutineParameter)
                {
                    newVar = new ParameterVariable(methodScope, contextHandler, parameter.Invoked);
                }
                // Subroutine parameter.
                else
                {
                    newVar = new SubroutineParameterVariable(methodScope, contextHandler);
                }

                vars[i]        = newVar;
                parameter.Type = newVar.CodeType;

                if (newVar.InitialValue != null)
                {
                    parameter.DefaultValue = new ExpressionOrWorkshopValue(newVar.InitialValue);
                }

                parameters[i] = parameter;
            }

            return(new ParameterParseResult(parameters, vars));
        }
Esempio n. 3
0
        public static ParameterParseResult GetParameters(ParseInfo parseInfo, Scope methodScope, List <VariableDeclaration> context, bool subroutineParameter)
        {
            if (context == null)
            {
                return(new ParameterParseResult(new CodeParameter[0], new Var[0]));
            }

            var parameters = new CodeParameter[context.Count];
            var vars       = new Var[parameters.Length];

            for (int i = 0; i < parameters.Length; i++)
            {
                Var newVar;

                CodeParameter parameter = new CodeParameter(context[i].Identifier.GetText());

                // Set up the context handler.
                IVarContextHandler contextHandler = new DefineContextHandler(parseInfo.SetRestrictedCallHandler(parameter), context[i]);

                // Normal parameter
                if (!subroutineParameter)
                {
                    newVar = (Var) new ParameterVariable(methodScope, contextHandler, parameter.Invoked).GetVar();
                }
                // Subroutine parameter.
                else
                {
                    newVar = (Var) new SubroutineParameterVariable(methodScope, contextHandler).GetVar();
                }

                vars[i]         = newVar;
                parameter._type = newVar.CodeType;

                if (newVar.InitialValue != null)
                {
                    parameter.DefaultValue = new ExpressionOrWorkshopValue(newVar.InitialValue);
                }

                parameters[i] = parameter;
            }

            return(new ParameterParseResult(parameters, vars));
        }
        private void GetInitialValue()
        {
            // Get the initial value.
            if (_initialValueContext != null)
            {
                ParseInfo parseInfo = this._parseInfo;

                // Store the initial value's restricted calls.
                RestrictedCallList restrictedCalls = null;
                if (_handleRestrictedCalls)
                {
                    restrictedCalls = new RestrictedCallList();
                    parseInfo       = parseInfo.SetRestrictedCallHandler(restrictedCalls);
                }

                ParseInfo initialValueParseInfo = parseInfo.SetExpectType(CodeType);
                if (parseInfo.CurrentCallInfo == null)
                {
                    CallInfo callInfo = new CallInfo(parseInfo.Script);
                    initialValueParseInfo = initialValueParseInfo.SetCallInfo(callInfo);
                }

                // Parse the initial value.
                InitialValue = initialValueParseInfo.GetExpression(_operationalScope, _initialValueContext);

                // Get the inferred type.
                if (_inferType)
                {
                    CodeType = InitialValue.Type();
                    _variableTypeHandler.SetType(CodeType);
                    AddScriptData();
                }

                // If the initial value's type is constant, make sure the constant type's implements the variable's type.
                if (InitialValue?.Type() != null && InitialValue.Type().IsConstant() && !InitialValue.Type().Implements(CodeType))
                {
                    parseInfo.Script.Diagnostics.Error($"The type '{InitialValue.Type().Name}' cannot be stored.", _initialValueContext.Range);
                }

                // If the variable's type is constant, make sure the value's type matches.
                else
                {
                    SemanticsHelper.ExpectValueType(parseInfo, InitialValue, CodeType, _initialValueContext.Range);
                }

                // Check restricted calls.
                if (_handleRestrictedCalls)
                {
                    foreach (RestrictedCall call in restrictedCalls)
                    {
                        // If the variable type is global, or the variable type is player and the restricted call type is not player...
                        if (VariableType == VariableType.Global ||
                            (VariableType == VariableType.Player && call.CallType != RestrictedCallType.EventPlayer))
                        {
                            // ... then add the error.
                            call.AddDiagnostic(parseInfo.Script.Diagnostics);
                        }
                    }
                }
            }

            ValueReady.Set();
        }