/* Copyright 2015 Brendan Guild
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License. */
package com.github.bguild.spag.parse;

import java.util.EnumMap;
import java.util.Map;

import com.github.bguild.spag.tok.TType;

/** @author Brendan Guild */
public enum Operator {
	MEMBER(120, 120, TType.MEMBER),
	NOT(Integer.MAX_VALUE, 110, TType.NOT),
	NEGATIVE(Integer.MAX_VALUE, 100, TType.MINUS),
	MUTABLE(100, Integer.MAX_VALUE, TType.MUTABLE),
	TIMES(90, 90, TType.TIMES),
	DIVIDE(90, 90, TType.DIVIDE),
	PLUS(80, 80, TType.PLUS),
	MINUS(80, 80, TType.MINUS),
	LTE(70, 70, TType.LTE),
	GTE(70, 70, TType.GTE),
	LT(70, 70, TType.LT),
	GT(70, 70, TType.GT),
	EQUALS(60, 60, TType.EQUAL),
	NEQ(60, 60, TType.NEQ),
	AND(50, 50, TType.AND),
	OR(40, 40, TType.OR);
	public final int left;
	public final int right;
	public final TType token;
	private static Map<TType, Operator> afterValue = new EnumMap<TType, Operator>(TType.class);
	private static Map<TType, Operator> afterOperator = new EnumMap<TType, Operator>(TType.class);

	static {
		for (Operator op : values()) {
			if(op.left == Integer.MAX_VALUE) continue;
			final TType type = op.token;
			afterValue.put(type, op);
		}
	}

	static {
		for (Operator op : values()) {
			if(op.left != Integer.MAX_VALUE) continue;
			final TType type = op.token;
			afterOperator.put(type, op);
		}
	}

	private Operator(int left, int right, TType token) {
		this.left = left;
		this.right = right;
		this.token = token;
	}
	
	public int argumentCount()
	{
		int count = 0;
		if(left < Integer.MAX_VALUE) count++;
		if(right < Integer.MAX_VALUE) count++;
		return count;
	}
	
	public boolean isSuffix()
	{
		return right == Integer.MAX_VALUE;
	}
	
	public boolean isPrefix()
	{
		return left == Integer.MAX_VALUE;
	}

	public static Operator getSuffixOperator(TType type) {
		return afterValue.get(type);
	}
	public static Operator getPrefixOperator(TType type) {
		return afterOperator.get(type);
	}
}
