#include "vhdl_bison_parser.hpp"


#include <iostream>
#include <list>
#include <string>

using namespace std;
// ---------------------------------------------------------------------
BinOp::BinOp(pExpr l,const StringToken& o,pExpr r,OpType op):left_(l),right_(r),operator_(o),opType_(op){}
// ---------------------------------------------------------------------
StringToken* BinOp::Connect(VhdlParserClient* client,bool createNet){
	StringToken* leftName = left_->Connect(client);
	StringToken* rightName = right_->Connect(client);
	if(!leftName) {
		throw VhdlOthException(
			OthReason("Signal Assertion Statement:","Invalid operand on left side of",operator_.GetName()),
			VhdlFilePosition(operator_),CircCtx());
	}
	if(!rightName) {
		throw VhdlOthException(
			OthReason("Signal Assertion Statement:","Invalid operand on right side of",operator_.GetName()),
			VhdlFilePosition(operator_),CircCtx());
	}
	if(opType_ != LOG ){
		throw VhdlOthException(
			OthReason("Signal Assertion Statement:","Invalid operator ",operator_.GetName()),
			VhdlFilePosition(operator_),CircCtx());
	}
	list<StringToken> inputs;
	inputs.push_back(*leftName);
	inputs.push_back(*rightName);
	delete leftName;
	delete rightName;
	StringToken* output = client->haveSymFunc(inputs,operator_,createNet);
	return output;
}
// ---------------------------------------------------------------------
UnOp::UnOp(pExpr e,const StringToken& o):expr_(e),operator_(o){}
// ---------------------------------------------------------------------
StringToken* UnOp::Connect(VhdlParserClient* client,bool createNet){
	StringToken* expr = expr_->Connect(client);
	list<StringToken> inputs;
	inputs.push_back(*expr);
	delete expr;
	StringToken* output = client->haveSymFunc(inputs,operator_,createNet);
	return output;
}
// ---------------------------------------------------------------------
StringToken* LiteralExpr::Connect(VhdlParserClient* client,bool createNet){
	list<StringToken> inputs;
	if( val_ == 0 )
		return client->haveSymFunc(inputs,"gnd",createNet);
	if( val_ == 1 )
		return client->haveSymFunc(inputs,"vcc",createNet);
	return NULL;
}
// ---------------------------------------------------------------------
StringToken* NameExpr::Connect(VhdlParserClient* client,bool createNet){
	client->haveOperand(name_->GenerateName());
	return new StringToken( name_->GenerateName() ,(yy::location)(name_->GetMark()) );
}
// ---------------------------------------------------------------------
NameExpr::~NameExpr(){ 
	delete name_; 
}
// ---------------------------------------------------------------------
