/**
  *
  *                      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 java.util.LinkedList;

import org.momut.ooas.ast.IScope;
import org.momut.ooas.ast.expressions.Expression;
import org.momut.ooas.ast.identifiers.Identifier;
import org.momut.ooas.parser.SymbolTable;
import org.momut.ooas.utils.exceptions.NotImplementedException;
import org.momut.ooas.utils.exceptions.InternalCompilerException;

///////////////////////////////////////////////
///  Blocks
///
public abstract class Block extends Statement implements IScope
{
	private final SymbolTable m_symbols;
	private LinkedList<Statement> m_statements;
	private Expression m_filter; /*gets transformed to guarded command in resolvevisitor!*/
	private IScope m_parentScope;

	public SymbolTable symbols() { return m_symbols; }
	public LinkedList<Statement> statements() { return m_statements; }
	public Expression filter() { return m_filter; }

	public Block(StatementKind aKind, int aline, int apos)
	{
		super (aKind, aline, apos);
		m_symbols = new SymbolTable();
		m_statements = new LinkedList<Statement>();
	}

	public Block(Block toCopy)
	{
		super (toCopy);
		m_symbols = new SymbolTable(toCopy.m_symbols);
		m_statements = new LinkedList<Statement>(toCopy.m_statements);
		m_parentScope = toCopy.m_parentScope;
		m_filter = toCopy.m_filter;
	}

	@Override
	public /*override*/ Statement Clone()
	{
		throw new NotImplementedException();
	}

	public void AddStatement(Statement toAdd)
	{
		m_statements.addLast(toAdd);
		checkParentScope(toAdd);
	}

	private void checkParentScope(Statement toAdd) {
		if (toAdd instanceof IScope) {
			final IScope child = (IScope) toAdd;
			if (child.GetParentScope() == null)
				child.SetParentScope(this);
			else if (child.GetParentScope() != this)
				throw new InternalCompilerException("Internal error: Statement added to block has parent set to different object.");
		}
	}

	public void SetStatements(LinkedList<Statement> newStatements)
	{
		m_statements = newStatements;
		for (final Statement s: newStatements)
			checkParentScope(s);
	}

	public void AddIdentifier(Identifier anIdentifier)
	{
		m_symbols.AddIdentifier(anIdentifier);
	}

	@Override
	public Identifier ResolveIdentifier(String aName)
	{
		if (m_symbols.Defined(aName))
			return m_symbols.Get(aName);
		else
			return null;
	}

	@Override
	public IScope GetParentScope()
	{
		return m_parentScope;
	}

	@Override
	public void SetParentScope(IScope parentScope)
	{
		if (parentScope == null)
			System.out.println("");
		m_parentScope = parentScope;
	}

	@Override
	public void AddIdentifier(Identifier anIdentifier, Object tag)
	{
		m_symbols.AddIdentifier(anIdentifier);
	}


	public void SetFilter(Expression sexpr)
	{
		m_filter = sexpr;
	}

	/** Checks whether there are no local variables etc. for this block */
	public boolean isSimpleBlock() {
		return m_symbols.size() == 0 && m_filter == null;
	}
}
