// joycub V2 - an interpeter for the programming language Joy
// (c) 2004-2006 Sergey Zubkov 
// prototype.cc - prototype module to test out the new design considerations
static char RCSID[] ="$Id: char.cc,v 1.1 2006/02/27 18:50:34 cubbi Exp cubbi $";

#include <iostream>
#include <sstream>
#include "exceptions.h"
#include "char.h"

// global object counters (for memory debugging)
int char_type_count = 0;

JC_char::JC_char(char i) : JC_int(i) {
	char_type_count++;
};

JC_char::JC_char(const std::string& s) {
	int i;
	if(s.length() == 1) { // regular 'a
			i=s[0];
	} else if(s.length() == 2) { // special character like '\n
		switch(s[1]) {
			case '\\': i='\\'; break;
			case 'n': i='\n'; break;
			case 'r': i='\r'; break;
			case 't': i='\t'; break;
			default:  i=s[1]; break;
		}
	} else if(s.length() == 4) {
			i=100*(s[1]-'0')
				+10*(s[2]-'0')
				+(s[3]-'0');
	} else {
		throw std::runtime_error( ("cannot convert " + s + " to char").data() );
	}

	content=i; calc_key();
	char_type_count++;
}

JC_char::JC_char(const JC_char& j) {
	char_type_count++;
	content = j.content;
	_key = j._key;
};

JC_char::~JC_char(void) {
	char_type_count--;
};

std::string JC_char::to_string(void) const {
	std::ostringstream s;
	s << "'" << (char)content;
	return s.str();
}

