<commit_before>package openmods.calc.command;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import openmods.calc.Calculator;
import openmods.calc.ExprType;
import openmods.calc.Frame;
import openmods.calc.ICallable;
import openmods.calc.IGettable;
import openmods.calc.IValuePrinter;
import openmods.calc.types.bigint.BigIntCalculatorFactory;
import openmods.calc.types.bool.BoolCalculatorFactory;
import openmods.calc.types.fp.DoubleCalculatorFactory;
import openmods.calc.types.fraction.FractionCalculatorFactory;
import openmods.calc.types.multi.TypedValue;
import openmods.calc.types.multi.TypedValueCalculatorFactory;
import openmods.utils.OptionalInt;
import openmods.utils.Stack;
import openmods.utils.StackValidationException;
import org.apache.commons.lang3.math.Fraction;

public class CalcState {

	public static class NoSuchNameException extends RuntimeException {
		private static final long serialVersionUID = 1L;

		public NoSuchNameException(String message) {
			super(message);
		}
	}

	private static interface IFunction<E> {
		public E call();
	}

	private static class SenderHolder {
		private ICommandSender sender;

		public int getX() {
			Preconditions.checkNotNull(sender, "DERP");
			return sender.getPlayerCoordinates().posX;
		}

		public int getY() {
			Preconditions.checkNotNull(sender, "DERP");
			return sender.getPlayerCoordinates().posY;
		}

		public int getZ() {
			Preconditions.checkNotNull(sender, "DERP");
			return sender.getPlayerCoordinates().posZ;
		}

		public <E> Calculator<E, ExprType> addPrinter(Calculator<E, ExprType> calculator) {
			final IValuePrinter<E> printer = calculator.printer;
			calculator.environment.setGlobalSymbol("p", new ICallable<E>() {
				@Override
				public void call(Frame<E> frame, OptionalInt argumentsCount, OptionalInt returnsCount) {
					Preconditions.checkNotNull(sender, "DERP");

					if (!returnsCount.compareIfPresent(0)) throw new StackValidationException("This function does not return any values");

					final Stack<E> stack = frame.stack();
					final int in = argumentsCount.or(1);

					final List<String> results = Lists.newArrayListWithExpectedSize(in);
					for (int i = 0; i < in; i++) {
						final E value = stack.pop();
						results.add(printer.repr(value));
					}

					final String result = ": " + Joiner.on(" ").join(results);
					sender.addChatMessage(new ChatComponentText(result));
				}
			});
			return calculator;
		}

		public synchronized <E> E call(ICommandSender sender, IFunction<E> function) {
			this.sender = sender;
			final E result = function.call();
			this.sender = null;
			return result;
		}
	}

	public enum CalculatorType {
		DOUBLE {
			@Override
			public Calculator<?, ExprType> newCalculator(final SenderHolder holder) {
				final Calculator<Double, ExprType> calculator = DoubleCalculatorFactory.createDefault();

				calculator.environment.setGlobalSymbol("_x", new IGettable<Double>() {
					@Override
					public Double get() {
						return Double.valueOf(holder.getX());
					}
				});

				calculator.environment.setGlobalSymbol("_y", new IGettable<Double>() {
					@Override
					public Double get() {
						return Double.valueOf(holder.getY());
					}
				});<commit_msg>'print' function usable in infix<commit_after>package openmods.calc.command;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import openmods.calc.Calculator;
import openmods.calc.ExprType;
import openmods.calc.Frame;
import openmods.calc.ICallable;
import openmods.calc.IGettable;
import openmods.calc.IValuePrinter;
import openmods.calc.UnaryFunction;
import openmods.calc.types.bigint.BigIntCalculatorFactory;
import openmods.calc.types.bool.BoolCalculatorFactory;
import openmods.calc.types.fp.DoubleCalculatorFactory;
import openmods.calc.types.fraction.FractionCalculatorFactory;
import openmods.calc.types.multi.TypedValue;
import openmods.calc.types.multi.TypedValueCalculatorFactory;
import openmods.utils.OptionalInt;
import openmods.utils.Stack;
import openmods.utils.StackValidationException;
import org.apache.commons.lang3.math.Fraction;

public class CalcState {

	public static class NoSuchNameException extends RuntimeException {
		private static final long serialVersionUID = 1L;

		public NoSuchNameException(String message) {
			super(message);
		}
	}

	private static interface IFunction<E> {
		public E call();
	}

	private static class SenderHolder {
		private ICommandSender sender;

		public int getX() {
			Preconditions.checkNotNull(sender, "DERP");
			return sender.getPlayerCoordinates().posX;
		}

		public int getY() {
			Preconditions.checkNotNull(sender, "DERP");
			return sender.getPlayerCoordinates().posY;
		}

		public int getZ() {
			Preconditions.checkNotNull(sender, "DERP");
			return sender.getPlayerCoordinates().posZ;
		}

		public <E> Calculator<E, ExprType> addPrinter(Calculator<E, ExprType> calculator) {
			final IValuePrinter<E> printer = calculator.printer;
			calculator.environment.setGlobalSymbol("p", new ICallable<E>() {
				@Override
				public void call(Frame<E> frame, OptionalInt argumentsCount, OptionalInt returnsCount) {
					Preconditions.checkNotNull(sender, "DERP");

					if (!returnsCount.compareIfPresent(0)) throw new StackValidationException("This function does not return any values");

					final Stack<E> stack = frame.stack();
					final int in = argumentsCount.or(1);

					final List<String> results = Lists.newArrayListWithExpectedSize(in);
					for (int i = 0; i < in; i++) {
						final E value = stack.pop();
						results.add(printer.repr(value));
					}

					final String result = ": " + Joiner.on(" ").join(results);
					sender.addChatMessage(new ChatComponentText(result));
				}
			});

			calculator.environment.setGlobalSymbol("print", new UnaryFunction.Direct<E>() {
				@Override
				protected E call(E value) {
					sender.addChatMessage(new ChatComponentText(printer.str(value)));
					return value;
				}
			});
			return calculator;
		}

		public synchronized <E> E call(ICommandSender sender, IFunction<E> function) {
			this.sender = sender;
			final E result = function.call();
			this.sender = null;
			return result;
		}
	}

	public enum CalculatorType {
		DOUBLE {
			@Override
			public Calculator<?, ExprType> newCalculator(final SenderHolder holder) {
				final Calculator<Double, ExprType> calculator = DoubleCalculatorFactory.createDefault();

				calculator.environment.setGlobalSymbol("_x", new IGettable<Double>() {
					@Override
					public Double get() {
						return Double.valueOf(holder.getX());
					}
				});

				calculator.environment.setGlobalSymbol("_y", new IGettable<Double>() {
					@Override
					public Double get() {
						return Double.valueOf(holder.getY());
					}
				});
{'fraction_non_alphanumeric': 0.11124661246612466, 'fraction_numerical': 0.0013550135501355014, 'mean_word_length': 4.537134283570893, 'pattern_counts': {'":': 2, '<': 36, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '249196', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2224, 'n_words': 480}