<commit_before>package dyvil.tools.compiler.ast.access;

import dyvil.tools.compiler.ast.context.IContext;
import dyvil.tools.compiler.ast.expression.ArrayExpr;
import dyvil.tools.compiler.ast.expression.IValue;
import dyvil.tools.compiler.ast.parameter.IArguments;
import dyvil.tools.compiler.config.Formatting;
import dyvil.tools.compiler.transform.Names;
import dyvil.tools.compiler.transform.SideEffectHelper;
import dyvil.tools.parsing.Name;
import dyvil.tools.parsing.marker.MarkerList;
import dyvil.tools.parsing.position.ICodePosition;

public class SubscriptAccess extends AbstractCall
{
	public SubscriptAccess(ICodePosition position)
	{
		this.position = position;
	}

	public SubscriptAccess(ICodePosition position, IValue instance)
	{
		this.position = position;
		this.receiver = instance;
	}

	public SubscriptAccess(ICodePosition position, IValue instance, IArguments arguments)
	{
		this.position = position;
		this.receiver = instance;
		this.arguments = arguments;
	}

	@Override
	public int valueTag()
	{
		return SUBSCRIPT_GET;
	}

	@Override
	public Name getName()
	{
		return Names.subscript;
	}

	@Override
	protected Name getReferenceName()
	{
		return Names.subscript_$amp;
	}

	@Override
	public IValue toAssignment(IValue rhs, ICodePosition position)
	{
		return new SubscriptAssignment(this.position.to(position), this.receiver, this.arguments, rhs);
	}

	@Override
	public IValue toCompoundAssignment(IValue rhs, ICodePosition position, MarkerList markers, IContext context,
		                                  SideEffectHelper helper)
	{
		// x[y...] op= z
		// -> x[y...] = x[y...].op(z)
		// -> x.subscript_=(y..., x.subscript(y...).op(z))

		final IValue subscriptReceiver = helper.processValue(this.receiver);
		final IArguments subscriptArguments = helper.processArguments(this.arguments);

		this.receiver = subscriptReceiver;
		this.arguments = subscriptArguments;
		return new SubscriptAssignment(position, subscriptReceiver, subscriptArguments, rhs)
			       .resolveCall(markers, context, true);
	}

	@Override
	public IValue resolve(MarkerList markers, IContext context)
	{
		if (this.receiver instanceof ICall)
		// false if receiver == null
		{
			ICall call = (ICall) this.receiver;

			// Resolve Receiver if necessary
			call.resolveReceiver(markers, context);
			call.resolveArguments(markers, context);

			IArguments oldArgs = call.getArguments();

			ArrayExpr array = new ArrayExpr(this.position, this.arguments.size());
			for (IValue v : this.arguments)
			{
				array.addValue(v);
			}

			call.setArguments(oldArgs.withLastValue(Names.subscript, array));

			IValue resolvedCall = call.resolveCall(markers, context, false);
			if (resolvedCall != null)
			{
				return resolvedCall;
			}

			// Revert
			call.setArguments(oldArgs);

			this.receiver = call.resolveCall(markers, context, true);
			return this.resolveCall(markers, context, true);
		}

		return super.resolve(markers, context);
	}

	@Override
	public void toString(String prefix, StringBuilder buffer)
	{
		if (this.receiver != null)
		{
			this.receiver.toString(prefix, buffer);
		}

		Formatting.appendSeparator(buffer, "method.subscript.open_bracket", '[');

		int count = this.arguments.size();
		if (count > 0)
		{
			this.arguments.getValue(0, null).toString(prefix, buffer);<commit_msg>Remove Subscript Access Special Case<commit_after>package dyvil.tools.compiler.ast.access;

import dyvil.tools.compiler.ast.context.IContext;
import dyvil.tools.compiler.ast.expression.IValue;
import dyvil.tools.compiler.ast.parameter.IArguments;
import dyvil.tools.compiler.config.Formatting;
import dyvil.tools.compiler.transform.Names;
import dyvil.tools.compiler.transform.SideEffectHelper;
import dyvil.tools.parsing.Name;
import dyvil.tools.parsing.marker.MarkerList;
import dyvil.tools.parsing.position.ICodePosition;

public class SubscriptAccess extends AbstractCall
{
	public SubscriptAccess(ICodePosition position)
	{
		this.position = position;
	}

	public SubscriptAccess(ICodePosition position, IValue instance)
	{
		this.position = position;
		this.receiver = instance;
	}

	public SubscriptAccess(ICodePosition position, IValue instance, IArguments arguments)
	{
		this.position = position;
		this.receiver = instance;
		this.arguments = arguments;
	}

	@Override
	public int valueTag()
	{
		return SUBSCRIPT_GET;
	}

	@Override
	public Name getName()
	{
		return Names.subscript;
	}

	@Override
	protected Name getReferenceName()
	{
		return Names.subscript_$amp;
	}

	@Override
	public IValue toAssignment(IValue rhs, ICodePosition position)
	{
		return new SubscriptAssignment(this.position.to(position), this.receiver, this.arguments, rhs);
	}

	@Override
	public IValue toCompoundAssignment(IValue rhs, ICodePosition position, MarkerList markers, IContext context,
		                                  SideEffectHelper helper)
	{
		// x[y...] op= z
		// -> x[y...] = x[y...].op(z)
		// -> x.subscript_=(y..., x.subscript(y...).op(z))

		final IValue subscriptReceiver = helper.processValue(this.receiver);
		final IArguments subscriptArguments = helper.processArguments(this.arguments);

		this.receiver = subscriptReceiver;
		this.arguments = subscriptArguments;
		return new SubscriptAssignment(position, subscriptReceiver, subscriptArguments, rhs)
			       .resolveCall(markers, context, true);
	}

	@Override
	public void toString(String prefix, StringBuilder buffer)
	{
		if (this.receiver != null)
		{
			this.receiver.toString(prefix, buffer);
		}

		Formatting.appendSeparator(buffer, "method.subscript.open_bracket", '[');

		int count = this.arguments.size();
		if (count > 0)
		{
			this.arguments.getValue(0, null).toString(prefix, buffer);
{'fraction_non_alphanumeric': 0.11230228471001757, 'fraction_numerical': 0.00070298769771529, 'mean_word_length': 4.298882681564246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3435989', 'n_tokens_mistral': 1899, 'n_tokens_neox': 1748, 'n_words': 364}