// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.server.npc.util.expression.compile.ast;

import java.util.NoSuchElementException;
import java.text.ParseException;
import com.hypixel.hytale.server.npc.util.expression.compile.CompileContext;
import com.hypixel.hytale.server.npc.util.expression.compile.Parser;
import com.hypixel.hytale.server.npc.util.expression.Scope;
import com.hypixel.hytale.server.npc.util.expression.ExecutionContext;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import com.hypixel.hytale.server.npc.util.expression.compile.Token;
import javax.annotation.Nonnull;
import com.hypixel.hytale.server.npc.util.expression.ValueType;
import java.util.List;

public abstract class ASTOperator extends AST
{
    private final List<AST> arguments;
    
    public ASTOperator(@Nonnull final ValueType returnType, @Nonnull final Token token, final int tokenPosition) {
        super(returnType, token, tokenPosition);
        this.arguments = new ObjectArrayList<AST>();
    }
    
    public void addArgument(@Nonnull final AST argument) {
        this.arguments.add(argument);
        argument.setParent(this);
    }
    
    @Nonnull
    public List<AST> getArguments() {
        return this.arguments;
    }
    
    @Override
    public ValueType genCode(@Nonnull final List<ExecutionContext.Instruction> list, final Scope scope) {
        this.arguments.forEach(ast -> ast.genCode(list, scope));
        return super.genCode(list, scope);
    }
    
    public static void fromParsedOperator(@Nonnull final Parser.ParsedToken operand, @Nonnull final CompileContext compileContext) throws ParseException {
        try {
            if (operand.token.isUnary()) {
                ASTOperatorUnary.fromUnaryOperator(operand, compileContext);
            }
            else {
                ASTOperatorBinary.fromBinaryOperator(operand, compileContext);
            }
        }
        catch (final NoSuchElementException e) {
            throw new ParseException("Not enough operands for operator '" + operand.tokenString, operand.tokenPosition);
        }
    }
}
