clean up constructors and hide Command away from public API
parent
3f406de1de
commit
f0f5fbc1ba
12
cs_vm.hh
12
cs_vm.hh
|
@ -18,6 +18,18 @@ enum {
|
|||
ID_LOCAL, ID_DO, ID_DOARGS, ID_IF, ID_RESULT, ID_NOT, ID_AND, ID_OR
|
||||
};
|
||||
|
||||
struct Command: Ident {
|
||||
char *cargs;
|
||||
ostd::Uint32 argmask;
|
||||
int numargs;
|
||||
CmdFunc cb_cftv;
|
||||
|
||||
Command(
|
||||
int type, ostd::ConstCharRange name, ostd::ConstCharRange args,
|
||||
ostd::Uint32 argmask, int numargs, CmdFunc func
|
||||
);
|
||||
};
|
||||
|
||||
enum {
|
||||
CODE_START = 0,
|
||||
CODE_OFFSET,
|
||||
|
|
101
cubescript.cc
101
cubescript.cc
|
@ -38,90 +38,71 @@ bool cs_check_num(ostd::ConstCharRange s) {
|
|||
}
|
||||
}
|
||||
|
||||
Ident::Ident(): type(ID_UNKNOWN) {}
|
||||
Ident::Ident(IdentType tp, ostd::ConstCharRange nm, int fl):
|
||||
type(int(tp)), flags(fl), name(nm)
|
||||
{}
|
||||
|
||||
Var::Var(VarCb f): cb_var(ostd::move(f)) {}
|
||||
Var::Var(IdentType tp, ostd::ConstCharRange name, VarCb f, int fl):
|
||||
Ident(tp, name, fl), cb_var(ostd::move(f))
|
||||
{}
|
||||
|
||||
/* ID_IVAR */
|
||||
Ivar::Ivar(
|
||||
ostd::ConstCharRange n, CsInt m, CsInt x, CsInt *s, VarCb f, int flagsv
|
||||
): Var(ostd::move(f)), minval(m), maxval(x), overrideval(0), storage(s) {
|
||||
type = ID_IVAR;
|
||||
flags = flagsv | (m > x ? IDF_READONLY : 0);
|
||||
name = n;
|
||||
}
|
||||
ostd::ConstCharRange name, CsInt m, CsInt x, CsInt *s, VarCb f, int fl
|
||||
):
|
||||
Var(IdentType::ivar, name, ostd::move(f), fl | ((m > x) ? IDF_READONLY : 0)),
|
||||
minval(m), maxval(x), overrideval(0), storage(s)
|
||||
{}
|
||||
|
||||
/* ID_FVAR */
|
||||
Fvar::Fvar(
|
||||
ostd::ConstCharRange n, CsFloat m, CsFloat x, CsFloat *s,
|
||||
VarCb f, int flagsv
|
||||
): Var(ostd::move(f)), minval(m), maxval(x), overrideval(0), storage(s) {
|
||||
type = ID_FVAR;
|
||||
flags = flagsv | (m > x ? IDF_READONLY : 0);
|
||||
name = n;
|
||||
}
|
||||
ostd::ConstCharRange name, CsFloat m, CsFloat x, CsFloat *s, VarCb f, int fl
|
||||
):
|
||||
Var(IdentType::fvar, name, ostd::move(f), fl | ((m > x) ? IDF_READONLY : 0)),
|
||||
minval(m), maxval(x), overrideval(0), storage(s)
|
||||
{}
|
||||
|
||||
/* ID_SVAR */
|
||||
Svar::Svar(ostd::ConstCharRange n, char **s, VarCb f, int flagsv):
|
||||
Var(ostd::move(f)), overrideval(nullptr), storage(s)
|
||||
{
|
||||
type = ID_SVAR;
|
||||
flags = flagsv;
|
||||
name = n;
|
||||
}
|
||||
Svar::Svar(ostd::ConstCharRange name, char **s, VarCb f, int fl):
|
||||
Var(IdentType::svar, name, ostd::move(f), fl),
|
||||
overrideval(nullptr), storage(s)
|
||||
{}
|
||||
|
||||
/* ID_ALIAS */
|
||||
Alias::Alias(ostd::ConstCharRange n, char *a, int flagsv):
|
||||
Alias::Alias(ostd::ConstCharRange name, char *a, int fl):
|
||||
Ident(IdentType::alias, name, fl),
|
||||
code(nullptr), stack(nullptr)
|
||||
{
|
||||
type = ID_ALIAS;
|
||||
flags = flagsv;
|
||||
name = n;
|
||||
val_v.set_mstr(a);
|
||||
}
|
||||
Alias::Alias(ostd::ConstCharRange n, CsInt a, int flagsv):
|
||||
Alias::Alias(ostd::ConstCharRange name, CsInt a, int fl):
|
||||
Ident(IdentType::alias, name, fl),
|
||||
code(nullptr), stack(nullptr)
|
||||
{
|
||||
type = ID_ALIAS;
|
||||
flags = flagsv;
|
||||
name = n;
|
||||
val_v.set_int(a);
|
||||
}
|
||||
Alias::Alias(ostd::ConstCharRange n, CsFloat a, int flagsv):
|
||||
Alias::Alias(ostd::ConstCharRange name, CsFloat a, int fl):
|
||||
Ident(IdentType::alias, name, fl),
|
||||
code(nullptr), stack(nullptr)
|
||||
{
|
||||
type = ID_ALIAS;
|
||||
flags = flagsv;
|
||||
name = n;
|
||||
val_v.set_float(a);
|
||||
}
|
||||
Alias::Alias(ostd::ConstCharRange n, int flagsv):
|
||||
Alias::Alias(ostd::ConstCharRange name, int fl):
|
||||
Ident(IdentType::alias, name, fl),
|
||||
code(nullptr), stack(nullptr)
|
||||
{
|
||||
type = ID_ALIAS;
|
||||
flags = flagsv;
|
||||
name = n;
|
||||
val_v.set_null();
|
||||
}
|
||||
Alias::Alias(ostd::ConstCharRange n, TaggedValue const &v, int flagsv):
|
||||
Alias::Alias(ostd::ConstCharRange name, TaggedValue const &v, int fl):
|
||||
Ident(IdentType::alias, name, fl),
|
||||
code(nullptr), stack(nullptr), val_v(v)
|
||||
{
|
||||
type = ID_ALIAS;
|
||||
flags = flagsv;
|
||||
name = n;
|
||||
}
|
||||
{}
|
||||
|
||||
/* ID_COMMAND */
|
||||
Command::Command(
|
||||
int t, ostd::ConstCharRange n, ostd::ConstCharRange args,
|
||||
int tp, ostd::ConstCharRange name, ostd::ConstCharRange args,
|
||||
ostd::Uint32 amask, int nargs, CmdFunc f
|
||||
):
|
||||
Ident(IdentType::unknown, name, 0),
|
||||
cargs(!args.empty() ? cs_dup_ostr(args) : nullptr),
|
||||
argmask(amask), numargs(nargs), cb_cftv(ostd::move(f))
|
||||
{
|
||||
type = t;
|
||||
flags = 0;
|
||||
name = n;
|
||||
type = tp;
|
||||
}
|
||||
|
||||
bool Ident::is_alias() const {
|
||||
|
@ -146,20 +127,6 @@ bool Ident::is_command() const {
|
|||
return get_type() == IdentType::command;
|
||||
}
|
||||
|
||||
Command *Ident::get_command() {
|
||||
if (!is_command()) {
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<Command *>(this);
|
||||
}
|
||||
|
||||
Command const *Ident::get_command() const {
|
||||
if (!is_command()) {
|
||||
return nullptr;
|
||||
}
|
||||
return static_cast<Command const *>(this);
|
||||
}
|
||||
|
||||
bool Ident::is_var() const {
|
||||
IdentType tp = get_type();
|
||||
return (tp >= IdentType::ivar) && (tp <= IdentType::svar);
|
||||
|
|
|
@ -65,11 +65,8 @@ private:
|
|||
OSTD_EXPORT bool code_is_empty(Bytecode const *code);
|
||||
|
||||
struct Ident;
|
||||
struct Alias;
|
||||
|
||||
struct OSTD_EXPORT TaggedValue {
|
||||
friend struct Alias;
|
||||
|
||||
union {
|
||||
CsInt i; /* ID_IVAR, VAL_INT */
|
||||
CsFloat f; /* ID_FVAR, VAL_FLOAT */
|
||||
|
@ -175,16 +172,16 @@ enum class IdentType {
|
|||
ivar, fvar, svar, command, alias
|
||||
};
|
||||
|
||||
struct Command;
|
||||
struct Var;
|
||||
struct Ivar;
|
||||
struct Fvar;
|
||||
struct Svar;
|
||||
struct Alias;
|
||||
|
||||
struct OSTD_EXPORT Ident {
|
||||
ostd::byte type; /* ID_something */
|
||||
int type; /* ID_something */
|
||||
ostd::ushort flags;
|
||||
int index;
|
||||
int index = -1;
|
||||
ostd::String name;
|
||||
|
||||
IdentType get_type() const;
|
||||
|
@ -194,8 +191,6 @@ struct OSTD_EXPORT Ident {
|
|||
Alias const *get_alias() const;
|
||||
|
||||
bool is_command() const;
|
||||
Command *get_command();
|
||||
Command const *get_command() const;
|
||||
|
||||
bool is_var() const;
|
||||
Var *get_var();
|
||||
|
@ -214,7 +209,7 @@ struct OSTD_EXPORT Ident {
|
|||
Svar const *get_svar() const;
|
||||
|
||||
protected:
|
||||
Ident();
|
||||
Ident(IdentType tp, ostd::ConstCharRange name, int flags = 0);
|
||||
};
|
||||
|
||||
using VarCb = ostd::Function<void(Ident &)>;
|
||||
|
@ -229,7 +224,7 @@ struct OSTD_EXPORT Var: Ident {
|
|||
}
|
||||
|
||||
protected:
|
||||
Var(VarCb f);
|
||||
Var(IdentType tp, ostd::ConstCharRange name, VarCb func, int flags = 0);
|
||||
};
|
||||
|
||||
struct OSTD_EXPORT Ivar: Var {
|
||||
|
@ -300,20 +295,6 @@ struct OSTD_EXPORT Alias: Ident {
|
|||
}
|
||||
};
|
||||
|
||||
using CmdFunc = ostd::Function<void(TvalRange, TaggedValue &)>;
|
||||
|
||||
struct OSTD_EXPORT Command: Ident {
|
||||
char *cargs;
|
||||
ostd::Uint32 argmask;
|
||||
int numargs;
|
||||
CmdFunc cb_cftv;
|
||||
|
||||
Command(
|
||||
int t, ostd::ConstCharRange n, ostd::ConstCharRange args,
|
||||
ostd::Uint32 argmask, int numargs, CmdFunc f = CmdFunc()
|
||||
);
|
||||
};
|
||||
|
||||
struct IdentLink {
|
||||
Ident *id;
|
||||
IdentLink *next;
|
||||
|
@ -321,6 +302,8 @@ struct IdentLink {
|
|||
IdentStack *argstack;
|
||||
};
|
||||
|
||||
using CmdFunc = ostd::Function<void(TvalRange, TaggedValue &)>;
|
||||
|
||||
struct OSTD_EXPORT CsState {
|
||||
ostd::Map<ostd::ConstCharRange, Ident *> idents;
|
||||
ostd::Vector<Ident *> identmap;
|
||||
|
|
Loading…
Reference in New Issue