public static bool ExpectValueType(ParseInfo parseInfo, IExpression expression, CodeType expectType, DocRange range)
 {
     if (!expression.Type().Implements(expectType))
     {
         parseInfo.Script.Diagnostics.Error("Expected a value of type '" + expectType.GetName() + "'", range);
         return(false);
     }
     return(true);
 }
        public string GetLabel(bool markdown)
        {
            string typeName = "define";

            if (CodeType != null)
            {
                typeName = CodeType.GetName();
            }
            return(new MarkupBuilder().StartCodeLine().Add(typeName + " " + Name).EndCodeLine().ToString(markdown));
        }
Exemple #3
0
        public string GetLabel(bool markdown)
        {
            string typeName = "define";

            if (CodeType != null)
            {
                typeName = CodeType.GetName();
            }
            return(HoverHandler.Sectioned(typeName + " " + Name, null));
        }
        public MarkupBuilder MakeVariableLabel(CodeType type, string name)
        {
            var builder = new MarkupBuilder().StartCodeLine();

            if (IncludeReturnType)
            {
                builder.Add(type.GetName()).Add(" ");
            }

            return(builder.Add(name).EndCodeLine());
        }
        public string GetLabel(bool markdown)
        {
            string name = (CodeType?.GetName() ?? "define") + " " + Name;

            if (markdown)
            {
                return(HoverHandler.Sectioned(name, null));
            }
            else
            {
                return(name);
            }
        }
Exemple #6
0
        public virtual string GetLabel(bool markdown)
        {
            string typeName = "define";

            if (CodeType != null)
            {
                typeName = CodeType.GetName();
            }
            if (markdown)
            {
                return(HoverHandler.Sectioned(typeName + " " + Name, Documentation));
            }
            else
            {
                return(typeName + " " + Name);
            }
        }
        public void TrySet(ParseInfo parseInfo, IExpression value, DocRange expressionRange)
        {
            // Check if the hook was already set.
            if (WasSet)
            {
                parseInfo.Script.Diagnostics.Error("Hooks cannot be set twice.", expressionRange);
            }
            // Check if the given value implements the expected value.
            else if (!value.Type().Implements(CodeType))
            {
                parseInfo.Script.Diagnostics.Error($"Expected a value of type {CodeType.GetName()}.", expressionRange);
            }
            // Set the hook.
            else
            {
                HookValue = value;
                SetHook?.Invoke(value);
            }

            WasSet = true;
        }
        public ArrayType(CodeType arrayOfType) : base((arrayOfType?.Name ?? "define") + "[]")
        {
            ArrayOfType           = arrayOfType;
            DebugVariableResolver = new Debugger.ArrayResolver(ArrayOfType?.DebugVariableResolver, ArrayOfType?.GetName(), ArrayOfType is ClassType);

            _length = new InternalVar("Length", CompletionItemKind.Property);
            _last   = new InternalVar("Last", ArrayOfType, CompletionItemKind.Property);
            _first  = new InternalVar("First", ArrayOfType, CompletionItemKind.Property);

            _scope.AddNativeVariable(_length);
            _scope.AddNativeVariable(_last);
            _scope.AddNativeVariable(_first);

            // Filtered Array
            new GenericSortFunction()
            {
                Name                   = "FilteredArray",
                Documentation          = "A copy of the specified array with any values that do not match the specified condition removed.",
                ReturnType             = this,
                ArrayOfType            = ArrayOfType,
                ParameterDocumentation = "The condition that is evaluated for each element of the copied array. If the condition is true, the element is kept in the copied array."
            }.Add <V_FilteredArray>(_scope);
            // Sorted Array
            new GenericSortFunction()
            {
                Name                   = "SortedArray",
                Documentation          = "A copy of the specified array with the values sorted according to the value rank that is evaluated for each element.",
                ReturnType             = this,
                ArrayOfType            = ArrayOfType,
                ParameterDocumentation = "The value that is evaluated for each element of the copied array. The array is sorted by this rank in ascending order."
            }.Add <V_SortedArray>(_scope);
            // Is True For Any
            new GenericSortFunction()
            {
                Name                   = "IsTrueForAny",
                Documentation          = "Whether the specified condition evaluates to true for any value in the specified array.",
                ArrayOfType            = ArrayOfType,
                ParameterDocumentation = "The condition that is evaluated for each element of the specified array."
            }.Add <V_IsTrueForAny>(_scope);
            // Is True For All
            new GenericSortFunction()
            {
                Name                   = "IsTrueForAll",
                Documentation          = "Whether the specified condition evaluates to true for every value in the specified array.",
                ArrayOfType            = ArrayOfType,
                ParameterDocumentation = "The condition that is evaluated for each element of the specified array."
            }.Add <V_IsTrueForAll>(_scope);
            // Mapped
            new GenericSortFunction()
            {
                Name                   = "Map",
                Documentation          = "Whether the specified condition evaluates to true for every value in the specified array.",
                ArrayOfType            = ArrayOfType,
                ParameterDocumentation = "The condition that is evaluated for each element of the specified array."
            }.Add <V_MappedArray>(_scope);
            // Contains
            Func(new FuncMethodBuilder()
            {
                Name            = "Contains",
                Documentation   = "Wether the array contains the specified value.",
                DoesReturnValue = true,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("value", "The value that is being looked for in the array.", ArrayOfType)
                },
                Action = (actionSet, methodCall) => Element.Part <V_ArrayContains>(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
            // Random
            Func(new FuncMethodBuilder()
            {
                Name            = "Random",
                Documentation   = "Gets a random value from the array.",
                DoesReturnValue = true,
                ReturnType      = ArrayOfType,
                Action          = (actionSet, methodCall) => Element.Part <V_RandomValueInArray>(actionSet.CurrentObject)
            });
            // Randomize
            Func(new FuncMethodBuilder()
            {
                Name            = "Randomize",
                Documentation   = "Returns a copy of the array that is randomized.",
                DoesReturnValue = true,
                ReturnType      = this,
                Action          = (actionSet, methodCall) => Element.Part <V_RandomizedArray>(actionSet.CurrentObject)
            });
            // Append
            Func(new FuncMethodBuilder()
            {
                Name            = "Append",
                Documentation   = "A copy of the array with the specified value appended to it.",
                DoesReturnValue = true,
                ReturnType      = this,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("value", "The value that is appended to the array. If the value is an array, it will be flattened.")
                },
                Action = (actionSet, methodCall) => Element.Part <V_Append>(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
            // Remove
            Func(new FuncMethodBuilder()
            {
                Name            = "Remove",
                Documentation   = "A copy of the array with the specified value removed from it.",
                DoesReturnValue = true,
                ReturnType      = this,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("value", "The value that is removed from the array.")
                },
                Action = (actionSet, methodCall) => Element.Part <V_RemoveFromArray>(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
            // Slice
            Func(new FuncMethodBuilder()
            {
                Name            = "Slice",
                Documentation   = "A copy of the array containing only values from a specified index range.",
                DoesReturnValue = true,
                ReturnType      = this,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("startIndex", "The first index of the range."),
                    new CodeParameter("count", "The number of elements in the resulting array. The resulting array will contain fewer elements if the specified range exceeds the bounds of the array.")
                },
                Action = (actionSet, methodCall) => Element.Part <V_ArraySlice>(actionSet.CurrentObject, methodCall.ParameterValues[0], methodCall.ParameterValues[1])
            });
            // Index Of
            Func(new FuncMethodBuilder()
            {
                Name            = "IndexOf",
                Documentation   = "The index of a value within an array or -1 if no such value can be found.",
                DoesReturnValue = true,
                Parameters      = new CodeParameter[] {
                    new CodeParameter("value", "The value for which to search.")
                },
                Action = (actionSet, methodCall) => Element.Part <V_IndexOfArrayValue>(actionSet.CurrentObject, methodCall.ParameterValues[0])
            });
        }
 public static CompletionItem GetTypeCompletion(CodeType type) => new CompletionItem()
 {
     Label = type.GetName(),
     Kind  = type.Kind == TypeKind.Class ? CompletionItemKind.Class : type.Kind == TypeKind.Constant ? CompletionItemKind.Constant : type.Kind == TypeKind.Enum ? CompletionItemKind.Enum : CompletionItemKind.Struct
 };
Exemple #10
0
        public ArrayType(ITypeSupplier supplier, CodeType arrayOfType) : base(arrayOfType.GetNameOrAny() + "[]")
        {
            ArrayOfType   = arrayOfType;
            ArrayHandler  = arrayOfType.ArrayHandler;
            Attributes    = arrayOfType.Attributes;
            TypeSemantics = arrayOfType.TypeSemantics;
            AsReferenceResetSettability = arrayOfType.AsReferenceResetSettability;
            DebugVariableResolver       = new Debugger.ArrayResolver(ArrayOfType?.DebugVariableResolver, ArrayOfType?.GetName(), ArrayOfType is ClassType);

            Generics = new[] { arrayOfType };

            _length = new InternalVar("Length", supplier.Number(), CompletionItemKind.Property)
            {
                Ambiguous = false
            };
            _last = new InternalVar("Last", ArrayOfType, CompletionItemKind.Property)
            {
                Ambiguous = false
            };
            _first = new InternalVar("First", ArrayOfType, CompletionItemKind.Property)
            {
                Ambiguous = false
            };
            _supplier = supplier;
        }
        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);
                        }
                    }
                }
            }
        }
        // Sets up the method's block.
        public override void SetupBlock()
        {
            Block = new BlockAction(parseInfo.SetCallInfo(CallInfo), methodScope.Child(), Context.Block);

            // Validate returns.
            BlockTreeScan validation = new BlockTreeScan(DoesReturnValue, parseInfo, this);

            validation.ValidateReturns();
            MultiplePaths = validation.MultiplePaths;

            // If there is only one return statement, set SingleReturnValue.
            if (validation.Returns.Length == 1)
            {
                SingleReturnValue = validation.Returns[0].ReturningValue;
            }

            // If the return type is a constant type...
            if (CodeType != null && CodeType.IsConstant())
            {
                // ... iterate through each return statement ...
                foreach (ReturnAction returnAction in validation.Returns)
                {
                    // ... If the current return statement returns a value and that value does not implement the return type ...
                    if (returnAction.ReturningValue != null && (returnAction.ReturningValue.Type() == null || !returnAction.ReturningValue.Type().Implements(CodeType)))
                    {
                        // ... then add a syntax error.
                        parseInfo.Script.Diagnostics.Error("Must return a value of type '" + CodeType.GetName() + "'.", returnAction.ErrorRange);
                    }
                }
            }

            WasApplied = true;
            foreach (var listener in listeners)
            {
                listener.Applied();
            }
        }