/**
  *
  *                      OOAS Compiler
  *
  *       Copyright 2015, AIT Austrian Institute of Technology.
  * This code is based on the C# Version of the OOAS Compiler, which is
  * copyright 2015 by the Institute of Software Technology, Graz University
  * of Technology with portions copyright by the AIT Austrian Institute of
  * Technology. All rights reserved.
  *
  * SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
  *
  * If you modify the file please update the list of contributors below to in-
  * clude your name. Please also stick to the coding convention of using TABs
  * to do the basic (block-level) indentation and spaces for anything after
  * that. (Enable the display of special chars and it should be pretty obvious
  * what this means.) Also, remove all trailing whitespace.
  *
  * Contributors:
  *               Willibald Krenn (AIT)
  *               Stephan Zimmerer (AIT)
  *               Markus Demetz (AIT)
  *               Christoph Czurda (AIT)
  *
  */


package org.momut.ooas.ast.statements;

import org.momut.ooas.ast.IAstVisitor;
import org.momut.ooas.ast.IScope;
import org.momut.ooas.ast.expressions.Expression;
import org.momut.ooas.ast.identifiers.Identifier;
import org.momut.ooas.utils.exceptions.ArgumentException;
import org.momut.ooas.utils.exceptions.InternalCompilerException;

///////////////////////////////////////////////
///  Guarded Command
///
public final class GuardedCommand extends Statement implements IScope
{
	private Expression m_guard;
	private Statement m_body;
	private boolean m_isQualitative;
	private IScope m_parentScope;

	public Expression guard() { return m_guard; }
	public Statement body() { return m_body; }
	public boolean isQualitative() { return m_isQualitative; }


	public GuardedCommand(Expression aGuard, Block aBlock, int aline, int apos)
	{
		super (StatementKind.GuardedCommand, aline, apos);
		m_guard = aGuard;
		m_isQualitative = false;
		m_body = aBlock;
		checkBody();
	}

	public GuardedCommand(GuardedCommand toCopy)
	{
		super (toCopy);
		m_guard = toCopy.m_guard;
		m_body = toCopy.m_body;
		m_isQualitative = toCopy.m_isQualitative;
		m_parentScope = toCopy.m_parentScope;
	}

	@Override
	public /*override*/ Statement Clone()
	{
		return new GuardedCommand(this);
	}

	@Override
	public /*override*/ void Accept(IAstVisitor visitor)
	{
		visitor.visit(this);
	}

	public void SetGuard(Expression newGuard)
	{
		if (newGuard == null)
			throw new ArgumentException();
		m_guard = newGuard;
	}

	public void SetBody(Statement newBody)
	{
		if (newBody == null)
			throw new ArgumentException();
		m_body = newBody;
		checkBody();
	}

	private void checkBody() {
		if (m_body instanceof IScope) {
			final IScope s = (IScope) m_body;
			if (s.GetParentScope() == null)
				s.SetParentScope(this);
			else if (s.GetParentScope() != this)
				throw new InternalCompilerException("Internal Error: GuardedCommandBody has parent set to different method than it was added to.");
		}
	}

	public void SetIsQualitative(boolean newVal)
	{
		m_isQualitative = newVal;
	}

	@Override
	public Identifier ResolveIdentifier(String aName) {
		return null;
	}
	@Override
	public IScope GetParentScope() {
		return m_parentScope;
	}
	@Override
	public void SetParentScope(IScope parentScope) {
		m_parentScope = parentScope;
	}
	@Override
	public void AddIdentifier(Identifier anIdentifier, Object tag) {
		throw new InternalCompilerException("Cannot add identifier to guarded command.");
	}

}
