Skip to content

Consider replacing displaynode with a more robust ExpressionOperator enum #49

Description

@HenryGeorgist

Generally all of the displaynode logic was identical across implementations and it was weird that it would only get applied to some but not all of the operators. expand the ExpressionOperator to include more metadata and then we can drop the need for the display node implementation all together. this change would need to be staged and then the expressionui library would need to be updated.

public enum ExpressionOperator {
    // category, infix, prefix, arity
    PLUS("Math", "+", "PLUS", Arity.BINARY),
    MULTIPLY("Math", "*", "MULTIPLY", Arity.BINARY),
    DIVIDE("Math", "/", "DIVIDE", Arity.BINARY),
    GT("Comparison", ">", "GT", Arity.BINARY_BOOLEAN),
    AND("Logical", "&&", "AND", Arity.BINARY_BOOLEAN),
    IF("Logical", "", "IF", Arity.TERNARY),
    CONSTANT("", "", "CONSTANT", Arity.LEAF),
    VARIABLE("[", "[", Arity.LEAF);

    private final String category;
    private final String infix;
    private final String prefix;
    private final Arity arity;

    ExpressionOperator(String category, String infix, String prefix, Arity arity) {
        this.category = category;
        this.infix = infix;
        this.prefix = prefix;
        this.arity = arity;
    }

    // UI-facing static access (no instantiation needed)
    public String category() { return category; }
    public String getInfixName() { return infix; }
    public String getPrefixName() { return prefix; }
    
    public DisplayNode asDisplayNode() {
        return new DisplayNode() {
            public String displayName(boolean infix) { return infix ? this.infix : this.prefix; }
            public String category() { return this.category; }
            public String defaultSyntax(boolean infix) { 
                return infix ? this.infix : this.prefix + "(args...)"; 
            }
        };
    }
    
    public static List<DisplayNode> getAllForCategory(String category) {
        return Arrays.stream(values())
                .filter(op -> op.category.equals(category))
                .map(ExpressionOperator::asDisplayNode)
                .toList();
    }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions