Added string type and entry

This commit is contained in:
Louis 2015-02-11 16:34:43 +01:00
parent 24c52a3851
commit 720f6efbe6
2 changed files with 92 additions and 59 deletions

View file

@ -24,7 +24,7 @@ static bool _cut(const string& entry, vector<string>& entries)
entries.push_back(tmp);
tmp.clear();
break;
#if 0
//string
case '"':
//push prec entry if exists
@ -45,7 +45,7 @@ static bool _cut(const string& entry, vector<string>& entries)
entries.push_back(tmp);
tmp.clear();
break;
#endif
//other
default:
if (entry.at(i)!=' ' && entry.at(i)!='\t')
@ -110,6 +110,19 @@ static bool get_symbol(const string& entry, object*& obj)
return ret;
}
static bool get_string(const string& entry, object*& obj)
{
bool ret = false;
int len = entry.size();
if (len>1 && entry[0]=='\"')
{
int last = entry[len-1]=='\"'?(len-2):(len-1);
obj = new ostring(entry.substr(1, last).c_str());
ret = true;
}
return ret;
}
static bool get_float(const string& entry, object*& obj)
{
stringstream token;
@ -221,6 +234,12 @@ static bool _obj_from_string(const string& entry, object*& obj, unsigned int& ob
obj_size = sizeof(symbol);
ret = true;
}
else if (get_string(entry, obj))
{
type = cmd_string;
obj_size = sizeof(ostring);
ret = true;
}
else if (get_keyword(entry, obj, obj_size, type))
{
ret = true;
@ -238,6 +257,57 @@ static bool _obj_from_string(const string& entry, object*& obj, unsigned int& ob
return ret;
}
static char** entry_completion(const char* text, int start, int end)
{
char** matches = NULL;
if (start == 0)
matches = rl_completion_matches((char*)text, &entry_completion_generator);
return matches;
}
static char* entry_completion_generator(const char* text, int state)
{
static int list_index, len, too_far;
if (state == 0)
{
list_index = 0;
too_far = 0;
len = strlen(text);
}
while(too_far == 0)
{
list_index++;
if (_keywords[list_index].fn != NULL)
{
// compare list entry with text, return if match
if (strncmp(_keywords[list_index].name, text, len) == 0)
return entry_completion_dupstr(_keywords[list_index].name);
}
else
{
// fn=NULL and size=0 = last entry in list -> go out
if (_keywords[list_index].comment.size() == 0)
too_far = 1;
}
}
/* If no names matched, then return NULL. */
return NULL;
}
static char* entry_completion_dupstr(char* s)
{
char* r = (char*)malloc((strlen(s)+1));
if (r != NULL)
strcpy(r, s);
return r;
}
static ret_value parse(const string& entry, program& prog)
{
vector<string> entries;
@ -282,7 +352,7 @@ static ret_value entry(program& prog)
string entry = buf;
//enable auto-complete
rl_bind_key('\t',rl_complete);
rl_bind_key('\t', rl_complete);
// parse it
ret = parse(entry, prog);
@ -296,54 +366,3 @@ static ret_value entry(program& prog)
free(buf);
}
static char** entry_completion(const char* text, int start, int end)
{
char** matches = NULL;
if (start == 0)
matches = rl_completion_matches((char*)text, &entry_completion_generator);
return matches;
}
static char* entry_completion_generator(const char* text, int state)
{
static int list_index, len, too_far;
if (state == 0)
{
list_index = 0;
too_far = 0;
len = strlen(text);
}
while(too_far == 0)
{
list_index++;
if (_keywords[list_index].fn != NULL)
{
// compare list entry with text, return if match
if (strncmp(_keywords[list_index].name, text, len) == 0)
return entry_completion_dupstr(_keywords[list_index].name);
}
else
{
// fn=NULL and size=0 = last entry in list -> go out
if (_keywords[list_index].comment.size() == 0)
too_far = 1;
}
}
/* If no names matched, then return NULL. */
return NULL;
}
static char* entry_completion_dupstr(char* s)
{
char* r = (char*)malloc((strlen(s)+1));
if (r != NULL)
strcpy(r, s);
return r;
}

View file

@ -79,6 +79,7 @@ typedef enum {
cmd_undef,
cmd_number,/* floating value to put in stack */
cmd_binary,/* binary (integer) value to put in stack */
cmd_string,/* string value to put in stack */
cmd_symbol,/* symbol value to put in stack */
cmd_keyword,/* langage keyword */
cmd_branch,/* langage branch keyword */
@ -180,18 +181,31 @@ public:
binary::binary_enum binary::s_default_mode = binary::dec;
binary::binary_enum binary::s_mode = binary::s_default_mode;
class ostring : public object
{
public:
ostring(string& name, cmd_type_t type = cmd_string) : object(type)
{
_name = new string(name);
}
ostring(const char* name, cmd_type_t type = cmd_string) : object(type)
{
_name = new string(name);
}
virtual void show(ostream& stream = cout) { stream << "\"" << *_name << "\""; }
string* _name;
};
class symbol : public object
{
public:
symbol(string& name, cmd_type_t type = cmd_symbol) : object(type), _auto_eval(false)
{
_name = new string;
*_name = name;
_name = new string(name);
}
symbol(const char* name, cmd_type_t type = cmd_symbol) : object(type), _auto_eval(false)
{
_name = new string;
*_name = name;
_name = new string(name);
}
virtual void show(ostream& stream = cout) { stream << "'" << *_name << "'"; }
string* _name;
@ -267,7 +281,7 @@ public:
}
// not a command, but a stack entry, manage it
if ((type == cmd_number) || (type == cmd_binary))
if ((type == cmd_number) || (type == cmd_binary) || (type == cmd_string))
{
stk.push_back(seq_obj(i), seq_len(i), type);
i++;