api cleanup
This commit is contained in:
parent
83eb47e688
commit
5324223203
121
command.cc
121
command.cc
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
namespace cscript {
|
namespace cscript {
|
||||||
|
|
||||||
static inline bool check_num(const char *s) {
|
static inline bool cs_check_num(const char *s) {
|
||||||
if (isdigit(s[0]))
|
if (isdigit(s[0]))
|
||||||
return true;
|
return true;
|
||||||
switch (s[0]) {
|
switch (s[0]) {
|
||||||
|
@ -82,7 +82,7 @@ void CsState::clear_overrides() {
|
||||||
Ident *CsState::new_ident(ostd::ConstCharRange name, int flags) {
|
Ident *CsState::new_ident(ostd::ConstCharRange name, int flags) {
|
||||||
Ident *id = idents.at(name);
|
Ident *id = idents.at(name);
|
||||||
if (!id) {
|
if (!id) {
|
||||||
if (check_num(name.data())) {
|
if (cs_check_num(name.data())) {
|
||||||
debug_code("number %s is not a valid identifier name", name);
|
debug_code("number %s is not a valid identifier name", name);
|
||||||
return dummy;
|
return dummy;
|
||||||
}
|
}
|
||||||
|
@ -157,7 +157,7 @@ void CsState::set_alias(ostd::ConstCharRange name, TaggedValue &v) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
v.cleanup();
|
v.cleanup();
|
||||||
} else if (check_num(name.data())) {
|
} else if (cs_check_num(name.data())) {
|
||||||
debug_code("cannot alias number %s", name);
|
debug_code("cannot alias number %s", name);
|
||||||
v.cleanup();
|
v.cleanup();
|
||||||
} else {
|
} else {
|
||||||
|
@ -309,20 +309,20 @@ void Ident::clean_code() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::ConstCharRange debug_line(CsState &cs, ostd::ConstCharRange p,
|
ostd::ConstCharRange CsState::debug_line(ostd::ConstCharRange p,
|
||||||
ostd::ConstCharRange fmt,
|
ostd::ConstCharRange fmt,
|
||||||
ostd::CharRange buf) {
|
ostd::CharRange buf) {
|
||||||
if (cs.src_str.empty()) return fmt;
|
if (src_str.empty()) return fmt;
|
||||||
ostd::Size num = 1;
|
ostd::Size num = 1;
|
||||||
ostd::ConstCharRange line(cs.src_str);
|
ostd::ConstCharRange line(src_str);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
ostd::ConstCharRange end = ostd::find(line, '\n');
|
ostd::ConstCharRange end = ostd::find(line, '\n');
|
||||||
if (!end.empty())
|
if (!end.empty())
|
||||||
line = line.slice(0, line.distance_front(end));
|
line = line.slice(0, line.distance_front(end));
|
||||||
if (&p[0] >= &line[0] && &p[0] <= &line[line.size()]) {
|
if (&p[0] >= &line[0] && &p[0] <= &line[line.size()]) {
|
||||||
ostd::CharRange r(buf);
|
ostd::CharRange r(buf);
|
||||||
if (!cs.src_file.empty())
|
if (!src_file.empty())
|
||||||
ostd::format(r, "%s:%d: %s", cs.src_file, num, fmt);
|
ostd::format(r, "%s:%d: %s", src_file, num, fmt);
|
||||||
else
|
else
|
||||||
ostd::format(r, "%d: %s", num, fmt);
|
ostd::format(r, "%d: %s", num, fmt);
|
||||||
r.put('\0');
|
r.put('\0');
|
||||||
|
@ -336,17 +336,17 @@ ostd::ConstCharRange debug_line(CsState &cs, ostd::ConstCharRange p,
|
||||||
return fmt;
|
return fmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void debug_alias(CsState &cs) {
|
void CsState::debug_alias() {
|
||||||
if (!cs.dbgalias) return;
|
if (!dbgalias) return;
|
||||||
int total = 0, depth = 0;
|
int total = 0, depth = 0;
|
||||||
for (IdentLink *l = cs.stack; l != &cs.noalias; l = l->next) total++;
|
for (IdentLink *l = stack; l != &noalias; l = l->next) total++;
|
||||||
for (IdentLink *l = cs.stack; l != &cs.noalias; l = l->next) {
|
for (IdentLink *l = stack; l != &noalias; l = l->next) {
|
||||||
Ident *id = l->id;
|
Ident *id = l->id;
|
||||||
++depth;
|
++depth;
|
||||||
if (depth < cs.dbgalias)
|
if (depth < dbgalias)
|
||||||
ostd::err.writefln(" %d) %s", total - depth + 1, id->name);
|
ostd::err.writefln(" %d) %s", total - depth + 1, id->name);
|
||||||
else if (l->next == &cs.noalias)
|
else if (l->next == &noalias)
|
||||||
ostd::err.writefln(depth == cs.dbgalias ? " %d) %s"
|
ostd::err.writefln(depth == dbgalias ? " %d) %s"
|
||||||
: " ..%d) %s",
|
: " ..%d) %s",
|
||||||
total - depth + 1, id->name);
|
total - depth + 1, id->name);
|
||||||
}
|
}
|
||||||
|
@ -1768,7 +1768,7 @@ noid:
|
||||||
} else {
|
} else {
|
||||||
Ident *id = gs.cs.idents.at(idname);
|
Ident *id = gs.cs.idents.at(idname);
|
||||||
if (!id) {
|
if (!id) {
|
||||||
if (!check_num(idname.data())) {
|
if (!cs_check_num(idname.data())) {
|
||||||
gs.gen_str(idname, true);
|
gs.gen_str(idname, true);
|
||||||
goto noid;
|
goto noid;
|
||||||
}
|
}
|
||||||
|
@ -2063,8 +2063,8 @@ void GenState::gen_main(const char *p, int ret_type) {
|
||||||
code.push(CODE_EXIT | ((ret_type < VAL_ANY) ? (ret_type << CODE_RET) : 0));
|
code.push(CODE_EXIT | ((ret_type < VAL_ANY) ? (ret_type << CODE_RET) : 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
ostd::Uint32 *compilecode(CsState &cs, const char *p) {
|
ostd::Uint32 *CsState::compile(const char *p) {
|
||||||
GenState gs(cs);
|
GenState gs(*this);
|
||||||
gs.code.reserve(64);
|
gs.code.reserve(64);
|
||||||
gs.gen_main(p);
|
gs.gen_main(p);
|
||||||
ostd::Uint32 *code = new ostd::Uint32[gs.code.size()];
|
ostd::Uint32 *code = new ostd::Uint32[gs.code.size()];
|
||||||
|
@ -2095,7 +2095,7 @@ static inline void forcecond(CsState &cs, TaggedValue &v) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void keepcode(ostd::Uint32 *code) {
|
void bcode_ref(ostd::Uint32 *code) {
|
||||||
if (!code) return;
|
if (!code) return;
|
||||||
switch (*code & CODE_OP_MASK) {
|
switch (*code & CODE_OP_MASK) {
|
||||||
case CODE_START:
|
case CODE_START:
|
||||||
|
@ -2113,7 +2113,7 @@ void keepcode(ostd::Uint32 *code) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void freecode(ostd::Uint32 *code) {
|
void bcode_unref(ostd::Uint32 *code) {
|
||||||
if (!code) return;
|
if (!code) return;
|
||||||
switch (*code & CODE_OP_MASK) {
|
switch (*code & CODE_OP_MASK) {
|
||||||
case CODE_START:
|
case CODE_START:
|
||||||
|
@ -2907,7 +2907,7 @@ static const ostd::Uint32 *runcode(CsState &cs, const ostd::Uint32 *code, Tagged
|
||||||
(cs).identflags |= id->flags&IDF_OVERRIDDEN; \
|
(cs).identflags |= id->flags&IDF_OVERRIDDEN; \
|
||||||
IdentLink aliaslink = { id, (cs).stack, (1<<callargs)-1, argstack }; \
|
IdentLink aliaslink = { id, (cs).stack, (1<<callargs)-1, argstack }; \
|
||||||
(cs).stack = &aliaslink; \
|
(cs).stack = &aliaslink; \
|
||||||
if(!id->code) id->code = compilecode((cs), id->get_str()); \
|
if(!id->code) id->code = (cs).compile(id->get_str()); \
|
||||||
ostd::Uint32 *code = id->code; \
|
ostd::Uint32 *code = id->code; \
|
||||||
code[0] += 0x100; \
|
code[0] += 0x100; \
|
||||||
runcode((cs), code+1, (result)); \
|
runcode((cs), code+1, (result)); \
|
||||||
|
@ -2964,7 +2964,7 @@ litval:
|
||||||
Ident *id = cs.idents.at(idarg.s);
|
Ident *id = cs.idents.at(idarg.s);
|
||||||
if (!id) {
|
if (!id) {
|
||||||
noid:
|
noid:
|
||||||
if (check_num(idarg.s)) goto litval;
|
if (cs_check_num(idarg.s)) goto litval;
|
||||||
cs.debug_code("unknown command: %s", idarg.s);
|
cs.debug_code("unknown command: %s", idarg.s);
|
||||||
result.force_null();
|
result.force_null();
|
||||||
FORCERESULT;
|
FORCERESULT;
|
||||||
|
@ -3229,38 +3229,6 @@ void init_lib_io(CsState &cs) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool validateblock(const char *s) {
|
|
||||||
const int maxbrak = 100;
|
|
||||||
static char brakstack[maxbrak];
|
|
||||||
int brakdepth = 0;
|
|
||||||
for (; *s; s++) switch (*s) {
|
|
||||||
case '[':
|
|
||||||
case '(':
|
|
||||||
if (brakdepth >= maxbrak) return false;
|
|
||||||
brakstack[brakdepth++] = *s;
|
|
||||||
break;
|
|
||||||
case ']':
|
|
||||||
if (brakdepth <= 0 || brakstack[--brakdepth] != '[') return false;
|
|
||||||
break;
|
|
||||||
case ')':
|
|
||||||
if (brakdepth <= 0 || brakstack[--brakdepth] != '(') return false;
|
|
||||||
break;
|
|
||||||
case '"':
|
|
||||||
s = parsestring(s + 1);
|
|
||||||
if (*s != '"') return false;
|
|
||||||
break;
|
|
||||||
case '/':
|
|
||||||
if (s[1] == '/') return false;
|
|
||||||
break;
|
|
||||||
case '@':
|
|
||||||
case '\f':
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return brakdepth == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* standard lib */
|
|
||||||
|
|
||||||
void cs_init_lib_base_loops(CsState &cs);
|
void cs_init_lib_base_loops(CsState &cs);
|
||||||
|
|
||||||
void init_lib_base(CsState &cs) {
|
void init_lib_base(CsState &cs) {
|
||||||
|
@ -3546,11 +3514,6 @@ const char *floatstr(float v) {
|
||||||
return retbuf[retidx];
|
return retbuf[retidx];
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef ICOMMANDNAME
|
|
||||||
#define ICOMMANDNAME(name) _stdcmd
|
|
||||||
#undef ICOMMANDSNAME
|
|
||||||
#define ICOMMANDSNAME _stdcmd
|
|
||||||
|
|
||||||
static const char *liststart = nullptr, *listend = nullptr, *listquotestart = nullptr, *listquoteend = nullptr;
|
static const char *liststart = nullptr, *listend = nullptr, *listquotestart = nullptr, *listquoteend = nullptr;
|
||||||
|
|
||||||
static inline void skiplist(const char *&p) {
|
static inline void skiplist(const char *&p) {
|
||||||
|
@ -3635,23 +3598,31 @@ static inline ostd::String listelem(const char *start = liststart, const char *e
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
void explodelist(const char *s, ostd::Vector<ostd::String> &elems, int limit) {
|
namespace util {
|
||||||
const char *start, *end, *qstart;
|
ostd::Size list_length(const char *str) {
|
||||||
while ((limit < 0 || int(elems.size()) < limit) && parselist(s, start, end, qstart)) {
|
ostd::Size ret = 0;
|
||||||
elems.push(ostd::move(listelem(start, end, qstart)));
|
while (parselist(str)) ++ret;
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
char *indexlist(const char *s, int pos) {
|
ostd::Maybe<ostd::String> list_index(const char *s, ostd::Size idx) {
|
||||||
for (int i = 0; i < pos; ++i) if (!parselist(s)) return dup_ostr("");
|
for (ostd::Size i = 0; i < idx; ++i)
|
||||||
|
if (!parselist(s)) return ostd::nothing;
|
||||||
const char *start, *end, *qstart;
|
const char *start, *end, *qstart;
|
||||||
return parselist(s, start, end, qstart) ? listelem(start, end, qstart).disown() : dup_ostr("");
|
if (!parselist(s, start, end, qstart))
|
||||||
}
|
return ostd::nothing;
|
||||||
|
return ostd::move(listelem(start, end, qstart));
|
||||||
|
}
|
||||||
|
|
||||||
int listlen(CsState &, const char *s) {
|
ostd::Vector<ostd::String> list_explode(const char *s,
|
||||||
int n = 0;
|
ostd::Size limit) {
|
||||||
while (parselist(s)) n++;
|
ostd::Vector<ostd::String> ret;
|
||||||
return n;
|
const char *start, *end, *qstart;
|
||||||
|
while ((ret.size() < limit) && parselist(s, start, end, qstart)) {
|
||||||
|
ret.push(ostd::move(listelem(start, end, qstart)));
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void cs_set_iter(Ident &id, char *val, IdentStack &stack) {
|
static inline void cs_set_iter(Ident &id, char *val, IdentStack &stack) {
|
||||||
|
@ -3711,7 +3682,7 @@ static void cs_init_lib_list_sort(CsState &cs);
|
||||||
|
|
||||||
void init_lib_list(CsState &cs) {
|
void init_lib_list(CsState &cs) {
|
||||||
cs.add_command("listlen", "s", [](CsState &cs, char *s) {
|
cs.add_command("listlen", "s", [](CsState &cs, char *s) {
|
||||||
cs.result->set_int(listlen(cs, s));
|
cs.result->set_int(int(util::list_length(s)));
|
||||||
});
|
});
|
||||||
|
|
||||||
cs.add_command("at", "si1V", [](CsState &cs, TaggedValue *args,
|
cs.add_command("at", "si1V", [](CsState &cs, TaggedValue *args,
|
||||||
|
@ -3970,8 +3941,8 @@ found:
|
||||||
cs.add_command("prettylist", "ss", [](CsState &cs, const char *s,
|
cs.add_command("prettylist", "ss", [](CsState &cs, const char *s,
|
||||||
const char *conj) {
|
const char *conj) {
|
||||||
ostd::Vector<char> p;
|
ostd::Vector<char> p;
|
||||||
int len = listlen(cs, s);
|
ostd::Size len = util::list_length(s);
|
||||||
int n = 0;
|
ostd::Size n = 0;
|
||||||
const char *start, *end, *qstart;
|
const char *start, *end, *qstart;
|
||||||
for (; parselist(s, start, end, qstart); ++n) {
|
for (; parselist(s, start, end, qstart); ++n) {
|
||||||
if (*qstart == '"') {
|
if (*qstart == '"') {
|
||||||
|
|
35
command.hh
35
command.hh
|
@ -305,11 +305,6 @@ struct IdentLink {
|
||||||
IdentStack *argstack;
|
IdentStack *argstack;
|
||||||
};
|
};
|
||||||
|
|
||||||
void debug_alias(CsState &cs);
|
|
||||||
ostd::ConstCharRange debug_line(CsState &cs, ostd::ConstCharRange p,
|
|
||||||
ostd::ConstCharRange fmt,
|
|
||||||
ostd::CharRange buf);
|
|
||||||
|
|
||||||
struct CsState {
|
struct CsState {
|
||||||
ostd::Keyset<Ident> idents;
|
ostd::Keyset<Ident> idents;
|
||||||
ostd::Vector<Ident *> identmap;
|
ostd::Vector<Ident *> identmap;
|
||||||
|
@ -406,7 +401,7 @@ struct CsState {
|
||||||
void debug_code(ostd::ConstCharRange fmt, A &&...args) {
|
void debug_code(ostd::ConstCharRange fmt, A &&...args) {
|
||||||
if (nodebug) return;
|
if (nodebug) return;
|
||||||
ostd::err.writefln(fmt, ostd::forward<A>(args)...);
|
ostd::err.writefln(fmt, ostd::forward<A>(args)...);
|
||||||
debug_alias(*this);
|
debug_alias();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename ...A>
|
template<typename ...A>
|
||||||
|
@ -414,11 +409,10 @@ struct CsState {
|
||||||
ostd::ConstCharRange fmt, A &&...args) {
|
ostd::ConstCharRange fmt, A &&...args) {
|
||||||
if (nodebug) return;
|
if (nodebug) return;
|
||||||
ostd::Array<char, 256> buf;
|
ostd::Array<char, 256> buf;
|
||||||
ostd::err.writefln(debug_line(*this, p, fmt,
|
ostd::err.writefln(debug_line(p, fmt, ostd::CharRange(buf.data(),
|
||||||
ostd::CharRange(buf.data(),
|
|
||||||
buf.size())),
|
buf.size())),
|
||||||
ostd::forward<A>(args)...);
|
ostd::forward<A>(args)...);
|
||||||
debug_alias(*this);
|
debug_alias();
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_alias(ostd::ConstCharRange name, TaggedValue &v);
|
void set_alias(ostd::ConstCharRange name, TaggedValue &v);
|
||||||
|
@ -451,6 +445,14 @@ struct CsState {
|
||||||
void print_var_int(Ident *id, int i);
|
void print_var_int(Ident *id, int i);
|
||||||
void print_var_float(Ident *id, float f);
|
void print_var_float(Ident *id, float f);
|
||||||
void print_var_str(Ident *id, ostd::ConstCharRange s);
|
void print_var_str(Ident *id, ostd::ConstCharRange s);
|
||||||
|
|
||||||
|
ostd::Uint32 *compile(const char *code);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void debug_alias();
|
||||||
|
ostd::ConstCharRange debug_line(ostd::ConstCharRange p,
|
||||||
|
ostd::ConstCharRange fmt,
|
||||||
|
ostd::CharRange buf);
|
||||||
};
|
};
|
||||||
|
|
||||||
extern const char *intstr(int v);
|
extern const char *intstr(int v);
|
||||||
|
@ -583,14 +585,8 @@ inline void Ident::getcval(TaggedValue &v) const {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern ostd::Uint32 *compilecode(const char *p);
|
void bcode_ref(ostd::Uint32 *p);
|
||||||
extern void keepcode(ostd::Uint32 *p);
|
void bcode_unref(ostd::Uint32 *p);
|
||||||
extern void freecode(ostd::Uint32 *p);
|
|
||||||
|
|
||||||
extern bool validateblock(const char *s);
|
|
||||||
void explodelist(const char *s, ostd::Vector<ostd::String> &elems, int limit = -1);
|
|
||||||
extern char *indexlist(const char *s, int pos);
|
|
||||||
extern int listlen(CsState &cs, const char *s);
|
|
||||||
|
|
||||||
void init_lib_base(CsState &cs);
|
void init_lib_base(CsState &cs);
|
||||||
void init_lib_io(CsState &cs);
|
void init_lib_io(CsState &cs);
|
||||||
|
@ -626,6 +622,11 @@ namespace util {
|
||||||
writer.put('"');
|
writer.put('"');
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ostd::Size list_length(const char *str);
|
||||||
|
ostd::Maybe<ostd::String> list_index(const char *s, ostd::Size idx);
|
||||||
|
ostd::Vector<ostd::String> list_explode(const char *s,
|
||||||
|
ostd::Size limit = -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace cscript */
|
} /* namespace cscript */
|
Loading…
Reference in a new issue