mirror of
https://github.com/louisrubet/rpn
synced 2025-01-19 10:26:22 +01:00
Changed ostring
This commit is contained in:
parent
0467bcd535
commit
c8c38ac1cd
2 changed files with 91 additions and 85 deletions
13
src/parse.h
13
src/parse.h
|
@ -205,26 +205,29 @@ static bool get_program(const string& entry, object*& obj)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// care: not threadsafe
|
||||||
static bool get_float(const string& entry, object*& obj)
|
static bool get_float(const string& entry, object*& obj)
|
||||||
{
|
{
|
||||||
|
static number new_number(0);
|
||||||
stringstream token;
|
stringstream token;
|
||||||
floating_t val;
|
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
|
|
||||||
token<<entry;
|
token<<entry;
|
||||||
token>>val;
|
token>>new_number._value;
|
||||||
|
|
||||||
if ( (!token.fail()) && (!token.bad()) )
|
if ( (!token.fail()) && (!token.bad()) )
|
||||||
{
|
{
|
||||||
obj = new number(val);
|
obj = &new_number;
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// care: not threadsafe
|
||||||
static bool get_binary(const string& entry, object*& obj)
|
static bool get_binary(const string& entry, object*& obj)
|
||||||
{
|
{
|
||||||
|
static binary new_binary(0);
|
||||||
integer_t val;
|
integer_t val;
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
if ((entry.size() >= 2) && (entry[0] == '#'))
|
if ((entry.size() >= 2) && (entry[0] == '#'))
|
||||||
|
@ -252,10 +255,10 @@ static bool get_binary(const string& entry, object*& obj)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
token>>val;
|
token>>new_binary._value;
|
||||||
if(syntax && !token.fail())
|
if(syntax && !token.fail())
|
||||||
{
|
{
|
||||||
obj = new binary(val);
|
obj = &new_binary;
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
153
src/rpn.cpp
153
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
|
||||||
|
@ -186,54 +161,47 @@ binary::binary_enum binary::s_mode = binary::s_default_mode;
|
||||||
class ostring : public object
|
class ostring : public object
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ostring(string& value, cmd_type_t type = cmd_string) : object(type)
|
ostring(cmd_type_t type = cmd_string) : object(type), len(0) { }
|
||||||
|
void set(const char* value, unsigned int len)
|
||||||
{
|
{
|
||||||
_value = new string(value);
|
if (value != NULL)
|
||||||
}
|
|
||||||
ostring(const char* value, cmd_type_t type = cmd_string) : object(type)
|
|
||||||
{
|
{
|
||||||
_value = new string(value);
|
(void)memcpy(_value, value, len);
|
||||||
|
_value[len] = 0;
|
||||||
|
_len = len;
|
||||||
}
|
}
|
||||||
virtual void show(ostream& stream = cout) { stream << "\"" << *_value << "\""; }
|
else
|
||||||
string* _value;
|
len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int _len;
|
||||||
|
char _value[];
|
||||||
};
|
};
|
||||||
|
|
||||||
class oprogram : public ostring
|
class oprogram : public ostring
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
oprogram(string& value, cmd_type_t type = cmd_program) : ostring(value, type) { }
|
oprogram(cmd_type_t type = cmd_program) : ostring(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(cmd_type_t type = cmd_symbol) : ostring(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;
|
||||||
};
|
};
|
||||||
|
|
||||||
class keyword : public symbol
|
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, cmd_type_t type = cmd_keyword) : symbol(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
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
branch(branch_fn_t fn, string& value) : keyword(NULL, value, cmd_branch), arg1(-1), arg2(-1), arg3(-1), arg_bool(false)
|
branch(branch_fn_t fn) : keyword(NULL, cmd_branch), arg1(-1), arg2(-1), arg3(-1), arg_bool(false)
|
||||||
{
|
{
|
||||||
_type = cmd_branch;
|
_type = cmd_branch;
|
||||||
_fn = fn;
|
_fn = fn;
|
||||||
|
@ -246,6 +214,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) { }
|
||||||
|
@ -366,7 +384,7 @@ public:
|
||||||
if (type == cmd_keyword)
|
if (type == cmd_keyword)
|
||||||
{
|
{
|
||||||
keyword* k = (keyword*)seq_obj(i);
|
keyword* k = (keyword*)seq_obj(i);
|
||||||
if (k->_value->compare("end") == 0)
|
if (strncmp(k->_value, "end", 3) == 0)
|
||||||
{
|
{
|
||||||
int next = i + 1;
|
int next = i + 1;
|
||||||
if (next >= (int)size())
|
if (next >= (int)size())
|
||||||
|
@ -396,14 +414,14 @@ public:
|
||||||
else if (type == cmd_branch)
|
else if (type == cmd_branch)
|
||||||
{
|
{
|
||||||
branch* k = (branch*)seq_obj(i);
|
branch* k = (branch*)seq_obj(i);
|
||||||
if (k->_value->compare("if") == 0)
|
if (strncmp(k->_value, "if", 2) == 0)
|
||||||
{
|
{
|
||||||
if_layout_t layout;
|
if_layout_t layout;
|
||||||
layout.index_if = i;
|
layout.index_if = i;
|
||||||
vlayout.push_back(layout);
|
vlayout.push_back(layout);
|
||||||
layout_index++;
|
layout_index++;
|
||||||
}
|
}
|
||||||
else if (k->_value->compare("then") == 0)
|
else if (strncmp(k->_value, "then", 4) == 0)
|
||||||
{
|
{
|
||||||
int next = i + 1;
|
int next = i + 1;
|
||||||
if (next >= (int)size())
|
if (next >= (int)size())
|
||||||
|
@ -432,7 +450,7 @@ public:
|
||||||
k->arg1 = next;
|
k->arg1 = next;
|
||||||
k->arg3 = vlayout[layout_index].index_if;
|
k->arg3 = vlayout[layout_index].index_if;
|
||||||
}
|
}
|
||||||
else if (k->_value->compare("else") == 0)
|
else if (strncmp(k->_value, "else", 4) == 0)
|
||||||
{
|
{
|
||||||
int next = i + 1;
|
int next = i + 1;
|
||||||
if (next >= (int)size())
|
if (next >= (int)size())
|
||||||
|
@ -468,16 +486,16 @@ public:
|
||||||
k->arg3 = vlayout[layout_index].index_if;
|
k->arg3 = vlayout[layout_index].index_if;
|
||||||
((branch*)seq_obj(vlayout[layout_index].index_then))->arg2 = next;// fill branch2 (if was false) of 'then'
|
((branch*)seq_obj(vlayout[layout_index].index_then))->arg2 = next;// fill branch2 (if was false) of 'then'
|
||||||
}
|
}
|
||||||
else if (k->_value->compare("start") == 0)
|
else if (strncmp(k->_value, "start", 5) == 0)
|
||||||
{
|
{
|
||||||
vstartindex.push_back(i);
|
vstartindex.push_back(i);
|
||||||
}
|
}
|
||||||
else if (k->_value->compare("for") == 0)
|
else if (strncmp(k->_value, "for", 3) == 0)
|
||||||
{
|
{
|
||||||
vstartindex.push_back(i);
|
vstartindex.push_back(i);
|
||||||
k->arg1 = i + 1;// arg1 points on symbol variable
|
k->arg1 = i + 1;// arg1 points on symbol variable
|
||||||
}
|
}
|
||||||
else if(k->_value->compare("next") == 0)
|
else if(strncmp(k->_value, "next", 4) == 0)
|
||||||
{
|
{
|
||||||
if (vstartindex.size() == 0)
|
if (vstartindex.size() == 0)
|
||||||
{
|
{
|
||||||
|
@ -488,7 +506,7 @@ public:
|
||||||
k->arg1 = vstartindex[vstartindex.size() - 1];// fill 'next' branch1 = 'start' index
|
k->arg1 = vstartindex[vstartindex.size() - 1];// fill 'next' branch1 = 'start' index
|
||||||
vstartindex.pop_back();
|
vstartindex.pop_back();
|
||||||
}
|
}
|
||||||
else if (k->_value->compare("step") == 0)
|
else if (strncmp(k->_value, "step", 4) == 0)
|
||||||
{
|
{
|
||||||
if (vstartindex.size() == 0)
|
if (vstartindex.size() == 0)
|
||||||
{
|
{
|
||||||
|
@ -499,7 +517,7 @@ public:
|
||||||
k->arg1 = vstartindex[vstartindex.size() - 1];// fill 'step' branch1 = 'start' index
|
k->arg1 = vstartindex[vstartindex.size() - 1];// fill 'step' branch1 = 'start' index
|
||||||
vstartindex.pop_back();
|
vstartindex.pop_back();
|
||||||
}
|
}
|
||||||
else if (k->_value->compare("->") == 0)
|
else if (strncmp(k->_value, "->", 2) == 0)
|
||||||
{
|
{
|
||||||
k->arg1 = i;// arg1 is '->' command index in program
|
k->arg1 = i;// arg1 is '->' command index in program
|
||||||
}
|
}
|
||||||
|
@ -630,21 +648,6 @@ private:
|
||||||
_stack->push_back(&num, sizeof(binary), cmd_binary);
|
_stack->push_back(&num, sizeof(binary), cmd_binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
string getn()
|
|
||||||
{
|
|
||||||
/* warning, caller must check object type before */
|
|
||||||
string* a = ((symbol*)_stack->back())->_value;
|
|
||||||
_stack->pop_back();
|
|
||||||
return *a;
|
|
||||||
}
|
|
||||||
|
|
||||||
void putn(string& a)
|
|
||||||
{
|
|
||||||
/* warning, caller must check object type before */
|
|
||||||
symbol sym(a);
|
|
||||||
_stack->push_back(&sym, sizeof(symbol), cmd_symbol);
|
|
||||||
}
|
|
||||||
|
|
||||||
int stack_size()
|
int stack_size()
|
||||||
{
|
{
|
||||||
return _stack->size();
|
return _stack->size();
|
||||||
|
|
Loading…
Reference in a new issue