|  | 
|  | 1 | +/* ************************************************************************** */ | 
|  | 2 | +/* */ | 
|  | 3 | +/* ::: :::::::: */ | 
|  | 4 | +/* Interpreter.cpp :+: :+: :+: */ | 
|  | 5 | +/* +:+ +:+ +:+ */ | 
|  | 6 | +/* By: shovsepy <marvin@42.fr> +#+ +:+ +#+ */ | 
|  | 7 | +/* +#+#+#+#+#+ +#+ */ | 
|  | 8 | +/* Created: 2022年04月04日 18:32:41 by shovsepy #+# #+# */ | 
|  | 9 | +/* Updated: 2022年04月05日 17:07:42 by shovsepy ### ########.fr */ | 
|  | 10 | +/* */ | 
|  | 11 | +/* ************************************************************************** */ | 
|  | 12 | + | 
|  | 13 | +#include "Interpreter.hpp" | 
|  | 14 | + | 
|  | 15 | +Interpreter::Interpreter(std::string const &value): | 
|  | 16 | +	str(value), type(TypeInvalid) | 
|  | 17 | +{ | 
|  | 18 | +	for (int i = 0; i < 4; i++) | 
|  | 19 | +		this->status[i] = 0; | 
|  | 20 | +	this->parse(); | 
|  | 21 | +	if (this->type != TypeInvalid) | 
|  | 22 | +	{ | 
|  | 23 | +		this->convert(); | 
|  | 24 | +		if (this->hasFlag(TypeInt, this->isImpossible) | 
|  | 25 | +			|| this->ivalue > 127 || this->ivalue < 0) | 
|  | 26 | +			this->setFlag(TypeChar, this->isImpossible); | 
|  | 27 | +		if (!std::isprint(this->cvalue)) | 
|  | 28 | +			this->setFlag(TypeChar, this->nonDisplayable); | 
|  | 29 | +	} | 
|  | 30 | +	else | 
|  | 31 | +	{ | 
|  | 32 | +		this->setFlag(TypeInt, this->isImpossible); | 
|  | 33 | +		this->setFlag(TypeFloat, this->isImpossible); | 
|  | 34 | +		this->setFlag(TypeDouble, this->isImpossible); | 
|  | 35 | +		this->setFlag(TypeChar, this->isImpossible); | 
|  | 36 | +	} | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | +Interpreter::Interpreter(Interpreter const &other): | 
|  | 40 | +	str(other.str), type(other.type), | 
|  | 41 | +	ivalue(other.ivalue), fvalue(other.fvalue), | 
|  | 42 | +	dvalue(other.dvalue), cvalue(other.cvalue) | 
|  | 43 | +{ | 
|  | 44 | +	for (int i = 0; i < 4; i++) | 
|  | 45 | +		this->status[i] = other.status[i]; | 
|  | 46 | +} | 
|  | 47 | + | 
|  | 48 | +Interpreter::~Interpreter() | 
|  | 49 | +{ | 
|  | 50 | +} | 
|  | 51 | + | 
|  | 52 | +void Interpreter::parse(void) | 
|  | 53 | +{ | 
|  | 54 | +	std::stringstream ss; | 
|  | 55 | +	size_t length = this->str.length(); | 
|  | 56 | +	size_t i = 0; | 
|  | 57 | + | 
|  | 58 | +	if (length == 1 && !std::isdigit(this->str[0])) | 
|  | 59 | +	{ | 
|  | 60 | +		this->type = TypeChar; | 
|  | 61 | +		this->cvalue = this->str[0]; | 
|  | 62 | +		return ; | 
|  | 63 | +	} | 
|  | 64 | +	if (this->str[0] == '+' || this->str[0] == '-') | 
|  | 65 | +		ss << str[i++]; | 
|  | 66 | +	this->type = TypeInt; | 
|  | 67 | +	for ( ; i < length; i++) | 
|  | 68 | +	{ | 
|  | 69 | +		if (this->str[i] == '.' && this->type != TypeDouble) | 
|  | 70 | +		{ | 
|  | 71 | +			this->type = TypeDouble; | 
|  | 72 | +			ss << str[i]; | 
|  | 73 | +		} | 
|  | 74 | +		else if (this->str[i] == 'e' && i < length - 1 | 
|  | 75 | +			&& (this->str[i + 1] == '-' | 
|  | 76 | +				|| this->str[i + 1] == '+' | 
|  | 77 | +				|| std::isdigit(this->str[i + 1]))) | 
|  | 78 | +		{ | 
|  | 79 | +			ss << str[i] << str[i + 1]; | 
|  | 80 | +			i++; | 
|  | 81 | +			this->type = TypeDouble; | 
|  | 82 | +		} | 
|  | 83 | +		else if (this->str[i] == 'f' && i == length - 1 && this->type == TypeDouble) | 
|  | 84 | +			this->type = TypeFloat; | 
|  | 85 | +		else if (!std::isdigit(this->str[i])) | 
|  | 86 | +		{ | 
|  | 87 | +			this->type = TypeInvalid; | 
|  | 88 | +			i = length; | 
|  | 89 | +		} | 
|  | 90 | +		else | 
|  | 91 | +			ss << str[i]; | 
|  | 92 | +	} | 
|  | 93 | +	if (this->type == TypeFloat) | 
|  | 94 | +		ss >> this->fvalue; | 
|  | 95 | +	else if (this->type == TypeDouble) | 
|  | 96 | +		ss >> this->dvalue; | 
|  | 97 | +	else if (this->type == TypeInt) | 
|  | 98 | +	{ | 
|  | 99 | +		long lvalue; | 
|  | 100 | +		ss >> lvalue; | 
|  | 101 | +		this->ivalue = lvalue; | 
|  | 102 | +		if (ss.fail() | 
|  | 103 | +			|| lvalue > std::numeric_limits<int>::max() | 
|  | 104 | +			|| lvalue < std::numeric_limits<int>::min()) | 
|  | 105 | +			this->type = TypeInvalid; | 
|  | 106 | +	} | 
|  | 107 | +	else if (this->type == TypeInvalid) | 
|  | 108 | +	{ | 
|  | 109 | +		if (this->str == "inff" || this->str == "-inff" || this->str == "+inff" | 
|  | 110 | +			|| this->str == "nanf") | 
|  | 111 | +		{ | 
|  | 112 | +			this->fvalue = atof(this->str.c_str()); | 
|  | 113 | +			this->type = TypeFloat; | 
|  | 114 | +		} | 
|  | 115 | +		else if (this->str == "inf" || this->str == "-inf" || this->str == "+inf" | 
|  | 116 | +				|| this->str == "nan") | 
|  | 117 | +		{ | 
|  | 118 | +			this->dvalue = atof(this->str.c_str()); | 
|  | 119 | +			this->type = TypeDouble; | 
|  | 120 | +		} | 
|  | 121 | +	} | 
|  | 122 | +} | 
|  | 123 | + | 
|  | 124 | +void Interpreter::convert(void) | 
|  | 125 | +{ | 
|  | 126 | +	switch (this->type) | 
|  | 127 | +	{ | 
|  | 128 | +	case TypeInt: | 
|  | 129 | +		this->fromInt(); | 
|  | 130 | +		break; | 
|  | 131 | +	case TypeFloat: | 
|  | 132 | +		this->fromFloat(); | 
|  | 133 | +		break; | 
|  | 134 | +	case TypeDouble: | 
|  | 135 | +		this->fromDouble(); | 
|  | 136 | +		break; | 
|  | 137 | +	case TypeChar: | 
|  | 138 | +		this->fromChar(); | 
|  | 139 | +		break; | 
|  | 140 | +	} | 
|  | 141 | +} | 
|  | 142 | + | 
|  | 143 | +void Interpreter::fromInt(void) | 
|  | 144 | +{ | 
|  | 145 | +	this->fvalue = static_cast<float>(this->ivalue); | 
|  | 146 | +	this->dvalue = static_cast<double>(this->ivalue); | 
|  | 147 | +	this->cvalue = static_cast<char>(this->ivalue); | 
|  | 148 | +} | 
|  | 149 | + | 
|  | 150 | +bool Interpreter::floatIsValue(void) const | 
|  | 151 | +{ | 
|  | 152 | +	return (!(std::isnan(this->fvalue) || std::isinf(this->fvalue))); | 
|  | 153 | +} | 
|  | 154 | + | 
|  | 155 | +bool Interpreter::doubleIsValue(void) const | 
|  | 156 | +{ | 
|  | 157 | +	return (!(std::isnan(this->dvalue) || std::isinf(this->dvalue))); | 
|  | 158 | +} | 
|  | 159 | + | 
|  | 160 | +void Interpreter::fromFloat(void) | 
|  | 161 | +{ | 
|  | 162 | +	this->ivalue = static_cast<int>(this->fvalue); | 
|  | 163 | +	this->dvalue = static_cast<double>(this->fvalue); | 
|  | 164 | +	this->cvalue = static_cast<char>(this->fvalue); | 
|  | 165 | +	if (!this->floatIsValue() | 
|  | 166 | +		|| this->fvalue > std::numeric_limits<int>::max() | 
|  | 167 | +		|| this->fvalue < std::numeric_limits<int>::min()) | 
|  | 168 | +		this->setFlag(TypeInt, this->isImpossible); | 
|  | 169 | +} | 
|  | 170 | + | 
|  | 171 | +void Interpreter::fromDouble(void) | 
|  | 172 | +{ | 
|  | 173 | +	this->ivalue = static_cast<int>(this->dvalue); | 
|  | 174 | +	this->fvalue = static_cast<float>(this->dvalue); | 
|  | 175 | +	this->cvalue = static_cast<char>(this->dvalue); | 
|  | 176 | +	if (!this->doubleIsValue() | 
|  | 177 | +		|| this->dvalue > std::numeric_limits<int>::max() | 
|  | 178 | +		|| this->dvalue < std::numeric_limits<int>::min()) | 
|  | 179 | +		this->setFlag(TypeInt, this->isImpossible); | 
|  | 180 | +} | 
|  | 181 | + | 
|  | 182 | +void Interpreter::fromChar(void) | 
|  | 183 | +{ | 
|  | 184 | +	this->ivalue = static_cast<int>(this->cvalue); | 
|  | 185 | +	this->fvalue = static_cast<float>(this->cvalue); | 
|  | 186 | +	this->dvalue = static_cast<double>(this->cvalue); | 
|  | 187 | +} | 
|  | 188 | + | 
|  | 189 | +void Interpreter::setFlag(int status, int flag) | 
|  | 190 | +{ | 
|  | 191 | +	this->status[status] |= flag; | 
|  | 192 | +} | 
|  | 193 | + | 
|  | 194 | +bool Interpreter::hasFlag(int status, int flag) const | 
|  | 195 | +{ | 
|  | 196 | +	return (this->status[status] & flag); | 
|  | 197 | +} | 
|  | 198 | + | 
|  | 199 | +Interpreter &Interpreter::operator=(Interpreter const &other) | 
|  | 200 | +{ | 
|  | 201 | +	this->str = other.str; | 
|  | 202 | +	this->type = other.type; | 
|  | 203 | +	for (int i = 0; i < 4; i++) | 
|  | 204 | +		this->status[i] = other.status[i]; | 
|  | 205 | +	this->ivalue = other.ivalue; | 
|  | 206 | +	this->fvalue = other.fvalue; | 
|  | 207 | +	this->dvalue = other.dvalue; | 
|  | 208 | +	this->cvalue = other.cvalue; | 
|  | 209 | +	return (*this); | 
|  | 210 | +} | 
|  | 211 | + | 
|  | 212 | +int Interpreter::getAsInt(void) const | 
|  | 213 | +{ | 
|  | 214 | +	return (this->ivalue); | 
|  | 215 | +} | 
|  | 216 | + | 
|  | 217 | +float Interpreter::getAsFloat(void) const | 
|  | 218 | +{ | 
|  | 219 | +	return (this->fvalue); | 
|  | 220 | +} | 
|  | 221 | + | 
|  | 222 | +double Interpreter::getAsDouble(void) const | 
|  | 223 | +{ | 
|  | 224 | +	return (this->dvalue); | 
|  | 225 | +} | 
|  | 226 | + | 
|  | 227 | +char Interpreter::getAsChar(void) const | 
|  | 228 | +{ | 
|  | 229 | +	return (this->cvalue); | 
|  | 230 | +} | 
|  | 231 | + | 
|  | 232 | +std::string const &Interpreter::getRaw(void) const | 
|  | 233 | +{ | 
|  | 234 | +	return (this->str); | 
|  | 235 | +} | 
|  | 236 | + | 
|  | 237 | +std::ostream &operator<<(std::ostream &out, Interpreter const &pr) | 
|  | 238 | +{ | 
|  | 239 | +	std::stringstream ss; | 
|  | 240 | +	std::string tmp; | 
|  | 241 | + | 
|  | 242 | +	if (pr.hasFlag(0, Interpreter::isImpossible)) | 
|  | 243 | +		ss << "char: impossible" << '\n'; | 
|  | 244 | +	else if (pr.hasFlag(0, Interpreter::nonDisplayable)) | 
|  | 245 | +		ss << "char: Non displayable" << '\n'; | 
|  | 246 | +	else | 
|  | 247 | +		ss << "char: '" << pr.getAsChar() << "'\n"; | 
|  | 248 | + | 
|  | 249 | +	if (pr.hasFlag(1, Interpreter::isImpossible)) | 
|  | 250 | +		ss << "int: impossible" << '\n'; | 
|  | 251 | +	else | 
|  | 252 | +		ss << "int: " << pr.getAsInt() << '\n'; | 
|  | 253 | +	out << ss.str(); | 
|  | 254 | +	ss.str(std::string()); | 
|  | 255 | +	ss.clear(); | 
|  | 256 | + | 
|  | 257 | +	if (pr.hasFlag(2, Interpreter::isImpossible)) | 
|  | 258 | +		ss << "float: impossible" << '\n'; | 
|  | 259 | +	else | 
|  | 260 | +	{ | 
|  | 261 | +		ss << "float: " << pr.getAsFloat(); | 
|  | 262 | +		tmp = ss.str(); | 
|  | 263 | +		if (pr.floatIsValue() && tmp.find('.') == std::string::npos) | 
|  | 264 | +			ss << ".0"; | 
|  | 265 | +		ss << "f\n"; | 
|  | 266 | +	} | 
|  | 267 | +	out << ss.str(); | 
|  | 268 | +	ss.str(std::string()); | 
|  | 269 | +	ss.clear(); | 
|  | 270 | + | 
|  | 271 | +	if (pr.hasFlag(3, Interpreter::isImpossible)) | 
|  | 272 | +		ss << "double: impossible"; | 
|  | 273 | +	else | 
|  | 274 | +	{ | 
|  | 275 | +		ss << "double: " << pr.getAsDouble(); | 
|  | 276 | +		tmp = ss.str(); | 
|  | 277 | +		if (pr.doubleIsValue() && tmp.find('.') == std::string::npos) | 
|  | 278 | +			ss << ".0"; | 
|  | 279 | +	} | 
|  | 280 | +	out << ss.str() << std::endl; | 
|  | 281 | +	return (out); | 
|  | 282 | +} | 
0 commit comments