/*	Copyright  (c)	Günter Woigk 2010 - 2020
					mailto:kio@little-bat.de

	This file is free software.

	Permission to use, copy, modify, distribute, and sell this software
	and its documentation for any purpose is hereby granted without fee,
	provided that the above copyright notice appears in all copies and
	that both that copyright notice, this permission notice and the
	following disclaimer appear in supporting documentation.

	THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT ANY WARRANTY,
	NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
	A PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE COPYRIGHT HOLDER
	BE LIABLE FOR ANY DAMAGES ARISING FROM THE USE OF THIS SOFTWARE,
	TO THE EXTENT PERMITTED BY APPLICABLE LAW.
*/


#include "kio/kio.h"
#include "Word.h"
#include "Ival.h"
#include "Type.h"
#include "Parser/Names.h"


Word::~Word()
{
	if (idf == tIVAL) delete value;
}

Word::Word(cWord& q)
:
	idf(q.idf),
	spos(q.spos),
	value(nullptr)
{
	if(idf == tIVAL)
	{
		value = new Ival(*q.value);
	}
}

Word::Word(Word&& q)
:
	idf(q.idf),
	spos(q.spos),
	value(q.value)
{
	q.idf = tEMPTYSTR;
	q.value = nullptr;
}

Word& Word::operator= (cWord& q)
{
	if(this != &q)
	{
		this->~Word();
		new(this) Word(q);
	}
	return *this;
}

Word& Word::operator= (Word&& q)
{
	assert(this!=&q);

	std::swap(idf,q.idf);
	spos = q.spos;
	std::swap(value,q.value);

	return *this;
}

Word::Word (uint32 spos, NameID idf) noexcept  // identifiers
:
	idf(idf),
	spos(spos),
	value(nullptr)
{}

Word::Word (uint32 spos, cType* type, float128 n) noexcept  // sets errno, numeric value
:
	idf(tIVAL),
	spos(spos),
	value(new Ival(type,n))
{}

Word::Word (uint32 spos, cstr s) noexcept  // cstring: sets errno, may contain null as a 2-byte encoding
:
	idf(tIVAL),
	spos(spos),
	value(new Ival(s))
{}

Word::Word (uint32 spos, Ival* value) noexcept
:
	idf(tIVAL),
	spos(spos),
	value(value)
{}

bool Word::operator != (cWord& b) const noexcept
{
	if(idf != b.idf) return true;	// ne
	if(idf != tIVAL) return false;	// eq
	else return value != b.value;
}


void Word::serialize(FD& fd) const
{
	fd.write(idf);
	fd.write(spos);
	if (idf==tIVAL) value->serialize(fd);
}

void Word::deserialize(FD& fd)
{

}



















