From 580b41ded5d50ebf1533fc2582701b01b16a0342 Mon Sep 17 00:00:00 2001 From: q66 Date: Thu, 6 Aug 2015 01:44:13 +0100 Subject: [PATCH] cleanup --- command.cc | 73 ++++++++++++++++++++++-------------------------------- command.hh | 17 ++++++------- 2 files changed, 37 insertions(+), 53 deletions(-) diff --git a/command.cc b/command.cc index e628765a..4dda7d45 100644 --- a/command.cc +++ b/command.cc @@ -2982,7 +2982,7 @@ void executeret(const char *p, TaggedValue &result) { if (int(code[0]) >= 0x100) code.disown(); } -void executeret(Ident *id, TaggedValue *args, int numargs, bool lookup, TaggedValue &result) { +void executeret(Ident *id, TaggedValue *args, int numargs, TaggedValue &result) { result.set_null(); ++rundepth; TaggedValue *prevret = cstate.result; @@ -2996,8 +2996,8 @@ void executeret(Ident *id, TaggedValue *args, int numargs, bool lookup, TaggedVa if (numargs < id->numargs) { TaggedValue buf[MAX_ARGUMENTS]; memcpy(buf, args, numargs * sizeof(TaggedValue)); - callcommand(id, buf, numargs, lookup); - } else callcommand(id, args, numargs, lookup); + callcommand(id, buf, numargs, false); + } else callcommand(id, args, numargs, false); numargs = 0; break; case ID_VAR: @@ -3031,35 +3031,35 @@ void executeret(Ident *id, TaggedValue *args, int numargs, bool lookup, TaggedVa --rundepth; } -char *executestr(const ostd::uint *code) { +ostd::String CsState::run_str(const ostd::uint *code) { TaggedValue result; runcode(code, result); - if (result.type == VAL_NULL) return nullptr; + if (result.type == VAL_NULL) return ostd::String(); result.force_str(); - return result.s; + ostd::String ret(result.s); + delete[] result.s; + return ret; } -char *executestr(const char *p) { +ostd::String CsState::run_str(ostd::ConstCharRange code) { TaggedValue result; - executeret(p, result); - if (result.type == VAL_NULL) return nullptr; + /* FIXME range */ + executeret(code.data(), result); + if (result.type == VAL_NULL) return ostd::String(); result.force_str(); - return result.s; + ostd::String ret(result.s); + delete[] result.s; + return ret; } -char *executestr(Ident *id, TaggedValue *args, int numargs, bool lookup) { +char *executestr(Ident *id, TaggedValue *args, int numargs) { TaggedValue result; - executeret(id, args, numargs, lookup, result); + executeret(id, args, numargs, result); if (result.type == VAL_NULL) return nullptr; result.force_str(); return result.s; } -char *execidentstr(const char *name, bool lookup) { - Ident *id = cstate.idents.at(name); - return id ? executestr(id, nullptr, 0, lookup) : nullptr; -} - int execute(const ostd::uint *code) { TaggedValue result; runcode(code, result); @@ -3080,19 +3080,14 @@ int execute(const char *p) { return i; } -int execute(Ident *id, TaggedValue *args, int numargs, bool lookup) { +int execute(Ident *id, TaggedValue *args, int numargs) { TaggedValue result; - executeret(id, args, numargs, lookup, result); + executeret(id, args, numargs, result); int i = result.get_int(); result.cleanup(); return i; } -int execident(const char *name, int noid, bool lookup) { - Ident *id = cstate.idents.at(name); - return id ? execute(id, nullptr, 0, lookup) : noid; -} - float executefloat(const ostd::uint *code) { TaggedValue result; runcode(code, result); @@ -3109,19 +3104,14 @@ float executefloat(const char *p) { return f; } -float executefloat(Ident *id, TaggedValue *args, int numargs, bool lookup) { +float executefloat(Ident *id, TaggedValue *args, int numargs) { TaggedValue result; - executeret(id, args, numargs, lookup, result); + executeret(id, args, numargs, result); float f = result.get_float(); result.cleanup(); return f; } -float execidentfloat(const char *name, float noid, bool lookup) { - Ident *id = cstate.idents.at(name); - return id ? executefloat(id, nullptr, 0, lookup) : noid; -} - bool executebool(const ostd::uint *code) { TaggedValue result; runcode(code, result); @@ -3138,19 +3128,14 @@ bool executebool(const char *p) { return b; } -bool executebool(Ident *id, TaggedValue *args, int numargs, bool lookup) { +bool executebool(Ident *id, TaggedValue *args, int numargs) { TaggedValue result; - executeret(id, args, numargs, lookup, result); + executeret(id, args, numargs, result); bool b = getbool(result); result.cleanup(); return b; } -bool execidentbool(const char *name, bool noid, bool lookup) { - Ident *id = cstate.idents.at(name); - return id ? executebool(id, nullptr, 0, lookup) : noid; -} - bool execfile(const char *cfgfile, bool msg) { const char *oldsourcefile = sourcefile, *oldsourcestr = sourcestr; char *buf = nullptr; @@ -3645,9 +3630,9 @@ COMMAND(listassoc, "rse"); notfound: \ cs.result->set_int(-1); \ }); -LISTFIND(listfind =, "i", int, , parseint(start) == *val); -LISTFIND(listfind = f, "f", float, , parsefloat(start) == *val); -LISTFIND(listfind = s, "s", char, int len = (int)strlen(val), int(end - start) == len && !memcmp(start, val, len)); +LISTFIND(listfind=, "i", int, , parseint(start) == *val); +LISTFIND(listfind=f, "f", float, , parsefloat(start) == *val); +LISTFIND(listfind=s, "s", char, int len = (int)strlen(val), int(end - start) == len && !memcmp(start, val, len)); #define LISTASSOC(name, fmt, type, init, cmp) \ ICOMMAND(name, "s" fmt, (CsState &, char *list, type *val), \ @@ -3659,9 +3644,9 @@ LISTFIND(listfind = s, "s", char, int len = (int)strlen(val), int(end - start) = if(!parselist(s)) break; \ } \ }); -LISTASSOC(listassoc =, "i", int, , parseint(start) == *val); -LISTASSOC(listassoc = f, "f", float, , parsefloat(start) == *val); -LISTASSOC(listassoc = s, "s", char, int len = (int)strlen(val), int(end - start) == len && !memcmp(start, val, len)); +LISTASSOC(listassoc=, "i", int, , parseint(start) == *val); +LISTASSOC(listassoc=f, "f", float, , parsefloat(start) == *val); +LISTASSOC(listassoc=s, "s", char, int len = (int)strlen(val), int(end - start) == len && !memcmp(start, val, len)); void looplist(CsState &, Ident *id, const char *list, const ostd::uint *body) { if (id->type != ID_ALIAS) return; diff --git a/command.hh b/command.hh index 78ab0c80..45378c98 100644 --- a/command.hh +++ b/command.hh @@ -319,6 +319,9 @@ struct CsState { bool add_command(ostd::ConstCharRange name, ostd::ConstCharRange args, IdentFunc func, int type = ID_COMMAND); + + ostd::String run_str(const ostd::uint *code); + ostd::String run_str(ostd::ConstCharRange code); }; extern CsState cstate; @@ -474,23 +477,19 @@ extern void keepcode(ostd::uint *p); extern void freecode(ostd::uint *p); extern void executeret(const ostd::uint *code, TaggedValue &result = *cstate.result); extern void executeret(const char *p, TaggedValue &result = *cstate.result); -extern void executeret(Ident *id, TaggedValue *args, int numargs, bool lookup = false, TaggedValue &result = *cstate.result); +extern void executeret(Ident *id, TaggedValue *args, int numargs, TaggedValue &result = *cstate.result); extern char *executestr(const ostd::uint *code); extern char *executestr(const char *p); -extern char *executestr(Ident *id, TaggedValue *args, int numargs, bool lookup = false); -extern char *execidentstr(const char *name, bool lookup = false); +extern char *executestr(Ident *id, TaggedValue *args, int numargs); extern int execute(const ostd::uint *code); extern int execute(const char *p); -extern int execute(Ident *id, TaggedValue *args, int numargs, bool lookup = false); -extern int execident(const char *name, int noid = 0, bool lookup = false); +extern int execute(Ident *id, TaggedValue *args, int numargs); extern float executefloat(const ostd::uint *code); extern float executefloat(const char *p); -extern float executefloat(Ident *id, TaggedValue *args, int numargs, bool lookup = false); -extern float execidentfloat(const char *name, float noid = 0, bool lookup = false); +extern float executefloat(Ident *id, TaggedValue *args, int numargs); extern bool executebool(const ostd::uint *code); extern bool executebool(const char *p); -extern bool executebool(Ident *id, TaggedValue *args, int numargs, bool lookup = false); -extern bool execidentbool(const char *name, bool noid = false, bool lookup = false); +extern bool executebool(Ident *id, TaggedValue *args, int numargs); extern bool execfile(const char *cfgfile, bool msg = true); extern void alias(const char *name, const char *action); extern void alias(const char *name, TaggedValue &v);