mirror of
https://github.com/louisrubet/rpn
synced 2025-01-05 11:01:52 +01:00
ostring transforming
This commit is contained in:
parent
0467bcd535
commit
bcd7d1e771
1 changed files with 57 additions and 43 deletions
96
src/rpn.cpp
96
src/rpn.cpp
|
@ -111,14 +111,14 @@ class object
|
||||||
public:
|
public:
|
||||||
cmd_type_t _type;// object type
|
cmd_type_t _type;// object type
|
||||||
object(cmd_type_t type = cmd_undef):_type(type) { }
|
object(cmd_type_t type = cmd_undef):_type(type) { }
|
||||||
virtual void show(ostream& stream = cout) { }
|
|
||||||
|
void show(ostream& stream = cout);
|
||||||
};
|
};
|
||||||
|
|
||||||
class number : public object
|
class number : public object
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
number(floating_t value) : object(cmd_number) { _value = value; }
|
number(floating_t value) : object(cmd_number) { _value = value; }
|
||||||
virtual void show(ostream& stream = cout) { stream << _value; }
|
|
||||||
floating_t _value;
|
floating_t _value;
|
||||||
|
|
||||||
// representation mode
|
// representation mode
|
||||||
|
@ -143,31 +143,6 @@ class binary : public object
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
binary(integer_t value) : object(cmd_binary) { _value = value; }
|
binary(integer_t value) : object(cmd_binary) { _value = value; }
|
||||||
|
|
||||||
virtual void show(ostream& stream = cout)
|
|
||||||
{
|
|
||||||
cout << "# ";
|
|
||||||
switch(s_mode)
|
|
||||||
{
|
|
||||||
case dec: cout<<std::right<<std::setw(8)<<std::dec<<_value<<" d"; break;
|
|
||||||
case hex: cout<<std::right<<std::setw(16)<<std::hex<<_value<<" h"; break;
|
|
||||||
case oct: cout<<std::right<<std::setw(16)<<std::oct<<_value<<" o"; break;
|
|
||||||
case bin:
|
|
||||||
{
|
|
||||||
string mybin;
|
|
||||||
for (int i = (int)(log((floating_t)_value) / log(2.)); i>=0; i--)
|
|
||||||
{
|
|
||||||
if (_value & (1 << i))
|
|
||||||
mybin+='1';
|
|
||||||
else
|
|
||||||
mybin+='0';
|
|
||||||
}
|
|
||||||
cout<<std::right<<std::setw(20)<<std::oct<<mybin<<" b";
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
integer_t _value;
|
integer_t _value;
|
||||||
|
|
||||||
// representation mode
|
// representation mode
|
||||||
|
@ -194,7 +169,6 @@ public:
|
||||||
{
|
{
|
||||||
_value = new string(value);
|
_value = new string(value);
|
||||||
}
|
}
|
||||||
virtual void show(ostream& stream = cout) { stream << "\"" << *_value << "\""; }
|
|
||||||
string* _value;
|
string* _value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -203,22 +177,13 @@ class oprogram : public ostring
|
||||||
public:
|
public:
|
||||||
oprogram(string& value, cmd_type_t type = cmd_program) : ostring(value, type) { }
|
oprogram(string& value, cmd_type_t type = cmd_program) : ostring(value, type) { }
|
||||||
oprogram(const char* value, cmd_type_t type = cmd_program) : ostring(value, type) { }
|
oprogram(const char* value, cmd_type_t type = cmd_program) : ostring(value, type) { }
|
||||||
virtual void show(ostream& stream = cout) { stream << "<< " << *_value << " >>"; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class symbol : public object
|
class symbol : public ostring
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
symbol(string& value, cmd_type_t type = cmd_symbol) : object(type), _auto_eval(false)
|
symbol(string& value, cmd_type_t type = cmd_symbol) : ostring(value, type), _auto_eval(false) { }
|
||||||
{
|
symbol(const char* value, cmd_type_t type = cmd_symbol) : ostring(value, type), _auto_eval(false) { }
|
||||||
_value = new string(value);
|
|
||||||
}
|
|
||||||
symbol(const char* value, cmd_type_t type = cmd_symbol) : object(type), _auto_eval(false)
|
|
||||||
{
|
|
||||||
_value = new string(value);
|
|
||||||
}
|
|
||||||
virtual void show(ostream& stream = cout) { stream << "'" << *_value << "'"; }
|
|
||||||
string* _value;
|
|
||||||
bool _auto_eval;
|
bool _auto_eval;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -227,7 +192,6 @@ class keyword : public symbol
|
||||||
public:
|
public:
|
||||||
keyword(program_fn_t fn, string& value, cmd_type_t type = cmd_keyword) : symbol(value, type) { _fn = fn; }
|
keyword(program_fn_t fn, string& value, cmd_type_t type = cmd_keyword) : symbol(value, type) { _fn = fn; }
|
||||||
program_fn_t _fn;
|
program_fn_t _fn;
|
||||||
virtual void show(ostream& stream = cout) { stream << *_value; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class branch : public keyword
|
class branch : public keyword
|
||||||
|
@ -246,6 +210,56 @@ public:
|
||||||
bool arg_bool;
|
bool arg_bool;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void object::show(ostream& stream)
|
||||||
|
{
|
||||||
|
switch(_type)
|
||||||
|
{
|
||||||
|
case cmd_number:
|
||||||
|
stream << ((number*)this)->_value;
|
||||||
|
break;
|
||||||
|
case cmd_binary:
|
||||||
|
{
|
||||||
|
cout << "# ";
|
||||||
|
switch(((binary*)this)->s_mode)
|
||||||
|
{
|
||||||
|
case binary::dec: cout<<std::right<<std::setw(8)<<std::dec<<((binary*)this)->_value<<" d"; break;
|
||||||
|
case binary::hex: cout<<std::right<<std::setw(16)<<std::hex<<((binary*)this)->_value<<" h"; break;
|
||||||
|
case binary::oct: cout<<std::right<<std::setw(16)<<std::oct<<((binary*)this)->_value<<" o"; break;
|
||||||
|
case binary::bin:
|
||||||
|
{
|
||||||
|
string mybin;
|
||||||
|
for (int i = (int)(log((floating_t)((binary*)this)->_value) / log(2.)); i>=0; i--)
|
||||||
|
{
|
||||||
|
if (((binary*)this)->_value & (1 << i))
|
||||||
|
mybin+='1';
|
||||||
|
else
|
||||||
|
mybin+='0';
|
||||||
|
}
|
||||||
|
cout<<std::right<<std::setw(20)<<std::oct<<mybin<<" b";
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case cmd_string:
|
||||||
|
stream << "\"" << *((ostring*)this)->_value << "\"";
|
||||||
|
break;
|
||||||
|
case cmd_program:
|
||||||
|
stream << "<< " << *((oprogram*)this)->_value << " >>";
|
||||||
|
break;
|
||||||
|
case cmd_symbol:
|
||||||
|
stream << "'" << *((symbol*)this)->_value << "'";
|
||||||
|
break;
|
||||||
|
case cmd_keyword:
|
||||||
|
case cmd_branch:
|
||||||
|
stream << *((keyword*)this)->_value;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
stream << "< unknown object representation >";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct if_layout_t
|
struct if_layout_t
|
||||||
{
|
{
|
||||||
if_layout_t():index_then(-1),index_else(-1),index_end(-1) { }
|
if_layout_t():index_then(-1),index_else(-1),index_end(-1) { }
|
||||||
|
|
Loading…
Reference in a new issue