Class ExpressionEditor

java.lang.Object
org.cyclops.integrateddynamics.core.logicprogrammer.ExpressionEditor

public class ExpressionEditor extends Object
The editing state of an expression in the logic programmer, together with the edit actions that are applied identically on client and server.
  • Field Details

  • Constructor Details

    • ExpressionEditor

      public ExpressionEditor(IOperator rootOperator)
  • Method Details

    • getExpression

      public Expression getExpression()
    • getText

      @Nullable public String getText()
      Returns:
      The text the expression was typed as, or null if it is printed. It is only shown while it still reads as the expression.
    • getKnownRefTypes

      public Map<Integer, IValueType<?>> getKnownRefTypes()
      Returns:
      All known types of referenced variables, also of variables that are no longer referenced.
    • setExpression

      public void setExpression(Expression expression)
    • setExpression

      public void setExpression(Expression expression, @Nullable String text)
      Parameters:
      expression - The expression.
      text - The text it was typed as, or null if it is printed.
    • getEditPath

      public int[] getEditPath()
    • getCurrentNode

      public ExpressionNode.Operator getCurrentNode()
      Returns:
      The operator node that is currently being edited.
    • getCurrentOperator

      public IOperator getCurrentOperator()
    • getChild

      public ExpressionNode getChild(int slot)
    • getSlotCount

      public int getSlotCount()
    • getExpectedType

      public IValueType<?> getExpectedType(int slot)
      Parameters:
      slot - A slot of the current node.
      Returns:
      The input type of the current operator at the given slot.
    • setRefType

      public void setRefType(int variableId, @Nullable IValueType<?> type)
    • hasRefType

      public boolean hasRefType(int variableId)
    • getRefType

      @Nullable public IValueType<?> getRefType(int variableId)
    • getRefTypes

      public Map<Integer, IValueType<?>> getRefTypes()
      Returns:
      The known types of the variables referenced in the expression.
    • inferMissingRefTypes

      public void inferMissingRefTypes()
      For references of unknown type, take the type of the operator input they are in when that type is not a category. This holds for cards that were written without errors.
    • check

      Returns:
      The type checking result of the current expression, cached until the next change.
    • getOperatorsAlongPath

      public List<IOperator> getOperatorsAlongPath(int[] path)
      Parameters:
      path - A path.
      Returns:
      The names of the operators along the given path, starting at the root.
    • setChild

      public void setChild(int slot, ExpressionNode node)
      Set a child of the current node.
      Parameters:
      slot - The slot.
      node - The new child.
    • addParameter

      public int addParameter()
    • getNewParameterName

      public static String getNewParameterName(List<String> names)
      Parameters:
      names - The names of the parameters.
      Returns:
      The name for a new parameter.
    • addParameterToText

      public static String addParameterToText(String text, ExpressionParser.Context context)
      Add a parameter to the text of an expression, such as text that is still being typed.
      Parameters:
      text - The text.
      context - What the parser needs to know about the world.
      Returns:
      The text with a new parameter after its parameters, or in front of it if it has none.
    • removeParameter

      public void removeParameter(int index)
    • renameParameter

      public void renameParameter(int index, String name)
    • setParameterType

      public void setParameterType(int index, IValueType<?> type)
    • isPickedAsValue

      public static boolean isPickedAsValue(IValueType<?> expectedType, IOperator operator, boolean forceValue)
      Parameters:
      expectedType - The type of the slot the operator is picked for.
      operator - The picked operator.
      forceValue - If the player asked to insert the operator as a value.
      Returns:
      If the operator is inserted as an operator value instead of as a nested call.
    • isParameterType

      public static boolean isParameterType(IValueType<?> type)
      Parameters:
      type - A value type.
      Returns:
      If parameters can have this type: Any, Number, or any registered type that is not a category, also of add-ons.
    • getParameterTypes

      public static List<IValueType<?>> getParameterTypes()
      Returns:
      The types parameters can have, in cycling order: Any and Number, then the types in the order of the logic programmer, then those without an element in it by name.
    • isEditable

      public static boolean isEditable(IOperator operator)
      Parameters:
      operator - An operator.
      Returns:
      If the operator can be edited in the logic programmer, which needs a slot for each input.
    • isEditable

      public static boolean isEditable(Expression expression)
      Parameters:
      expression - An expression.
      Returns:
      If the expression can be edited in the logic programmer.
    • getParserContext

      public static ExpressionParser.Context getParserContext(ValueDeseralizationContext values, IntFunction<IValueType<?>> refTypes, Function<String, it.unimi.dsi.fastutil.ints.IntList> labelIds)
      Parameters:
      values - For deserializing values that are written with their type.
      refTypes - Resolves the type of a variable id, may return null when unknown.
      labelIds - Resolves the ids of the variables with a label.
      Returns:
      The context for parsing expressions that can be edited in the logic programmer.
    • parse

      public static Expression parse(String text, ExpressionParser.Context context) throws ExpressionParseException
      Parse the text of an expression that can be edited in the logic programmer.
      Parameters:
      text - The text.
      context - The context, see getParserContext(ValueDeseralizationContext, IntFunction, Function).
      Returns:
      The expression.
      Throws:
      ExpressionParseException - If the text is not a valid expression, or one that is too large to edit.
    • sanitizeParameterName

      public static String sanitizeParameterName(String name)
    • actionConst

      public static net.minecraft.nbt.CompoundTag actionConst(ValueDeseralizationContext context, int[] path, int slot, IValue value)
    • actionOperator

      public static net.minecraft.nbt.CompoundTag actionOperator(int[] path, int slot, IOperator operator)
    • actionParam

      public static net.minecraft.nbt.CompoundTag actionParam(int[] path, int slot, int index)
    • actionClear

      public static net.minecraft.nbt.CompoundTag actionClear(int[] path, int slot)
    • actionNavigate

      public static net.minecraft.nbt.CompoundTag actionNavigate(int[] path)
    • actionAddParam

      public static net.minecraft.nbt.CompoundTag actionAddParam()
    • actionRemoveParam

      public static net.minecraft.nbt.CompoundTag actionRemoveParam(int index)
    • actionRenameParam

      public static net.minecraft.nbt.CompoundTag actionRenameParam(int index, String name)
    • actionSet

      public static net.minecraft.nbt.CompoundTag actionSet(ValueDeseralizationContext context, Expression expression, @Nullable String text)
      An action that replaces the whole expression, which can have another root operator.
      Parameters:
      context - The serialization context.
      expression - The new expression.
      text - The text it was typed as, to show it as typed, or null to print it.
      Returns:
      The action.
    • readSetActionText

      @Nullable public static String readSetActionText(net.minecraft.nbt.CompoundTag action)
      Parameters:
      action - A set action.
      Returns:
      The text its expression was typed as, or null if there is none or it is too long. It is not checked against the expression, as it is only shown while it reads as the expression.
    • readSetAction

      @Nullable public static Expression readSetAction(ValueDeseralizationContext context, net.minecraft.nbt.CompoundTag action)
      Parameters:
      context - The deserialization context.
      action - A set action.
      Returns:
      Its expression, or null if the action is malformed or the expression can not be edited.
    • actionParamType

      public static net.minecraft.nbt.CompoundTag actionParamType(int index, IValueType<?> type)
    • apply

      public ExpressionEditor.ActionResult apply(ValueDeseralizationContext context, net.minecraft.nbt.CompoundTag action)
      Apply the given action. Actions that edit a slot are only accepted for the node that is currently being edited.
      Parameters:
      context - The deserialization context.
      action - The action.
      Returns:
      The result.
    • applyUnsafe

      protected ExpressionEditor.ActionResult applyUnsafe(ValueDeseralizationContext context, net.minecraft.nbt.CompoundTag action)
    • parseValue

      @Nullable public static IValue parseValue(IValueType<?> expectedType, String text)
      Parse the given text to a value for the given expected type. Numbers and booleans are read as in the text of an expression, such as 5L, and otherwise the text is read as the element of the type in the logic programmer reads it.
      Parameters:
      expectedType - The expected type, possibly a category.
      text - The text.
      Returns:
      The parsed value, or null if it could not be parsed.
    • isTextEditable

      public static boolean isTextEditable(IValueType<?> type)
      Parameters:
      type - A value type.
      Returns:
      If constants of this type can be entered as text: those whose element in the logic programmer is text or a checkbox, and the categories of numbers, booleans or strings.