using System;
using System.Collections.Generic;
using System.Diagnostics;
using Robust.Shared.Maths;
using Robust.Shared.Toolshed.Errors;
using Robust.Shared.Utility;
namespace Robust.Shared.Toolshed.Syntax;
///
/// This class is used represent toolshed command arguments that are either a reference to a Toolshed variable
/// (), a block of commands that need to be evaluated (), or simply some
/// specific value that has already been parsed/evaluated ().
///
public abstract class ValueRef
{
public abstract T? Evaluate(IInvocationContext ctx);
// Internal method used when invoking commands to evaluate & cast command parameters.
// Mainly exists for convenience, as expression trees don't support 'is' pattern matching or null propagation.
// Also makes makes for much nicer debugging of command parameter parsing
internal static T? EvaluateParameter(object? obj, IInvocationContext ctx)
{
return obj switch
{
null => default,
T cast => cast,
ValueRef @ref => @ref.Evaluate(ctx),
_ => throw new Exception(
$"Failed to parse command parameter. This likely is a toolshed bug and should be reported.\n" +
$"Target type: {typeof(T).PrettyName()}.\n" +
$"Input type: {obj.GetType()}.\n" +
$"Input: {obj}")
};
}
internal static T[] EvaluateParamsCollection(object? obj, IInvocationContext ctx)
{
if (obj is not List