package sbahjsic.interpreter.parser.syntaxtree;

import sbahjsic.interpreter.compiler.Instruction;
import sbahjsic.interpreter.parser.Node;
import sbahjsic.interpreter.parser.NodeType;

public final class TypedefNode extends Node {
	
	private final String defined;
	private final ValueNode value;
	
	public TypedefNode(String defined, ValueNode value) {
		this.defined = defined;
		this.value = value;
	}

	@Override
	public NodeType type() {
		return NodeType.TYPEDEF_NODE;
	}

	@Override
	public Instruction[] toInstructions() {
		Instruction[] valueIns = value.toInstructions();
		Instruction[] ins = new Instruction[valueIns.length + 2];
		System.arraycopy(valueIns, 0, ins, 0, valueIns.length);
		ins[ins.length - 2] = Instruction.pushString(defined);
		ins[ins.length - 1] = Instruction.typedef();
		return ins;
	}

	@Override
	public String toString() {
		return "TYPEDEF{" + defined + ", " + value +"}";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((defined == null) ? 0 : defined.hashCode());
		result = prime * result + ((value == null) ? 0 : value.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof TypedefNode)) {
			return false;
		}
		TypedefNode other = (TypedefNode) obj;
		if (defined == null) {
			if (other.defined != null) {
				return false;
			}
		} else if (!defined.equals(other.defined)) {
			return false;
		}
		if (value == null) {
			if (other.value != null) {
				return false;
			}
		} else if (!value.equals(other.value)) {
			return false;
		}
		return true;
	}
	
}