mirror of
https://github.com/louisrubet/rpn
synced 2025-01-01 18:20:06 +01:00
ostring and symbol work
This commit is contained in:
parent
e810a0fbe4
commit
d32a1cf701
2 changed files with 62 additions and 29 deletions
68
src/parse.h
68
src/parse.h
|
@ -199,24 +199,38 @@ static bool get_symbol(const string& entry, object*& obj, unsigned int& obj_len)
|
|||
{
|
||||
bool ret = false;
|
||||
int entry_len = entry.size();
|
||||
if (entry_len>1 && entry[0]=='\'')
|
||||
if (entry_len>=1 && entry[0]=='\'')
|
||||
{
|
||||
int naked_entry_len;
|
||||
if (entry_len == 1)
|
||||
{
|
||||
// total object length
|
||||
obj_len = sizeof(symbol) + 1;
|
||||
|
||||
// entry length without prefix / postfix
|
||||
naked_entry_len = entry[entry_len-1]=='\''?(entry_len-3):(entry_len-2);
|
||||
// allocate object
|
||||
obj = (symbol*)malloc(obj_len);
|
||||
|
||||
// total object length
|
||||
obj_len = sizeof(symbol) + naked_entry_len;
|
||||
//set it
|
||||
((symbol*)obj)->set("", 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
int naked_entry_len;
|
||||
|
||||
// allocate object
|
||||
obj = (object*)malloc(obj_len);
|
||||
// entry length without prefix / postfix
|
||||
naked_entry_len = entry[entry_len-1]=='\''?(entry_len-2):(entry_len-1);
|
||||
|
||||
// set it
|
||||
((symbol*)obj)->set(&entry[1], naked_entry_len);
|
||||
// total object length
|
||||
obj_len = sizeof(symbol) + naked_entry_len + 1;
|
||||
|
||||
// allocate object
|
||||
obj = (symbol*)malloc(obj_len);
|
||||
|
||||
// set it
|
||||
((symbol*)obj)->set(entry.substr(1, naked_entry_len).c_str(), naked_entry_len);
|
||||
}
|
||||
ret = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -224,24 +238,38 @@ static bool get_string(const string& entry, object*& obj, unsigned int& obj_len)
|
|||
{
|
||||
bool ret = false;
|
||||
int entry_len = entry.size();
|
||||
if (entry_len>1 && entry[0]=='"')
|
||||
if (entry_len>=1 && entry[0]=='"')
|
||||
{
|
||||
int naked_entry_len;
|
||||
if (entry_len == 1)
|
||||
{
|
||||
// total object length
|
||||
obj_len = sizeof(ostring) + 1;
|
||||
|
||||
// entry length without prefix / postfix
|
||||
naked_entry_len = entry[entry_len-1]=='"'?(entry_len-3):(entry_len-2);
|
||||
// allocate object
|
||||
obj = (ostring*)malloc(obj_len);
|
||||
|
||||
// total object length
|
||||
obj_len = sizeof(ostring) + naked_entry_len;
|
||||
//set it
|
||||
((ostring*)obj)->set("", 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
int naked_entry_len;
|
||||
|
||||
// allocate object
|
||||
obj = (object*)malloc(obj_len);
|
||||
// entry length without prefix / postfix
|
||||
naked_entry_len = entry[entry_len-1]=='"'?(entry_len-2):(entry_len-1);
|
||||
|
||||
// set it
|
||||
((ostring*)obj)->set(&entry[1], naked_entry_len);
|
||||
// total object length
|
||||
obj_len = sizeof(ostring) + naked_entry_len + 1;
|
||||
|
||||
// allocate object
|
||||
obj = (ostring*)malloc(obj_len);
|
||||
|
||||
// set it
|
||||
((ostring*)obj)->set(entry.substr(1, naked_entry_len).c_str(), naked_entry_len);
|
||||
}
|
||||
ret = true;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
23
src/rpn.cpp
23
src/rpn.cpp
|
@ -176,7 +176,8 @@ struct ostring : public object
|
|||
_type = cmd_string;
|
||||
if (value != NULL)
|
||||
{
|
||||
(void)memcpy(_value, value, len);
|
||||
if (len>0)
|
||||
(void)memcpy(_value, value, len);
|
||||
_value[len] = 0;
|
||||
_len = len;
|
||||
}
|
||||
|
@ -197,7 +198,8 @@ struct oprogram : public object
|
|||
_type = cmd_program;
|
||||
if (value != NULL)
|
||||
{
|
||||
(void)memcpy(_value, value, len);
|
||||
if (len>0)
|
||||
(void)memcpy(_value, value, len);
|
||||
_value[len] = 0;
|
||||
_len = len;
|
||||
}
|
||||
|
@ -219,7 +221,8 @@ struct symbol : public object
|
|||
_auto_eval = false;
|
||||
if (value != NULL)
|
||||
{
|
||||
(void)memcpy(_value, value, len);
|
||||
if (len>0)
|
||||
(void)memcpy(_value, value, len);
|
||||
_value[len] = 0;
|
||||
_len = len;
|
||||
}
|
||||
|
@ -242,7 +245,8 @@ struct keyword : public object
|
|||
_fn = fn;
|
||||
if (value != NULL)
|
||||
{
|
||||
(void)memcpy(_value, value, len);
|
||||
if (len>0)
|
||||
(void)memcpy(_value, value, len);
|
||||
_value[len] = 0;
|
||||
_len = len;
|
||||
}
|
||||
|
@ -271,7 +275,8 @@ struct branch : public object
|
|||
arg_bool = 0;
|
||||
if (value != NULL)
|
||||
{
|
||||
(void)memcpy(_value, value, len);
|
||||
if (len>0)
|
||||
(void)memcpy(_value, value, len);
|
||||
_value[len] = 0;
|
||||
_len = len;
|
||||
}
|
||||
|
@ -321,17 +326,17 @@ void object::show(ostream& stream)
|
|||
}
|
||||
break;
|
||||
case cmd_string:
|
||||
stream << "\"" << *((ostring*)this)->_value << "\"";
|
||||
stream << "\"" << ((ostring*)this)->_value << "\"";
|
||||
break;
|
||||
case cmd_program:
|
||||
stream << "<< " << *((oprogram*)this)->_value << " >>";
|
||||
stream << "<< " << ((oprogram*)this)->_value << " >>";
|
||||
break;
|
||||
case cmd_symbol:
|
||||
stream << "'" << *((symbol*)this)->_value << "'";
|
||||
stream << "'" << ((symbol*)this)->_value << "'";
|
||||
break;
|
||||
case cmd_keyword:
|
||||
case cmd_branch:
|
||||
stream << *((keyword*)this)->_value;
|
||||
stream << ((keyword*)this)->_value;
|
||||
break;
|
||||
default:
|
||||
stream << "< unknown object representation >";
|
||||
|
|
Loading…
Reference in a new issue