clean up constructors and hide Command away from public API

master
Daniel Kolesa 2016-08-18 02:00:32 +01:00
parent 3f406de1de
commit f0f5fbc1ba
3 changed files with 53 additions and 91 deletions

View File

@ -18,6 +18,18 @@ enum {
ID_LOCAL, ID_DO, ID_DOARGS, ID_IF, ID_RESULT, ID_NOT, ID_AND, ID_OR 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 { enum {
CODE_START = 0, CODE_START = 0,
CODE_OFFSET, CODE_OFFSET,

View File

@ -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( Ivar::Ivar(
ostd::ConstCharRange n, CsInt m, CsInt x, CsInt *s, VarCb f, int flagsv ostd::ConstCharRange name, CsInt m, CsInt x, CsInt *s, VarCb f, int fl
): Var(ostd::move(f)), minval(m), maxval(x), overrideval(0), storage(s) { ):
type = ID_IVAR; Var(IdentType::ivar, name, ostd::move(f), fl | ((m > x) ? IDF_READONLY : 0)),
flags = flagsv | (m > x ? IDF_READONLY : 0); minval(m), maxval(x), overrideval(0), storage(s)
name = n; {}
}
/* ID_FVAR */
Fvar::Fvar( Fvar::Fvar(
ostd::ConstCharRange n, CsFloat m, CsFloat x, CsFloat *s, ostd::ConstCharRange name, CsFloat m, CsFloat x, CsFloat *s, VarCb f, int fl
VarCb f, int flagsv ):
): Var(ostd::move(f)), minval(m), maxval(x), overrideval(0), storage(s) { Var(IdentType::fvar, name, ostd::move(f), fl | ((m > x) ? IDF_READONLY : 0)),
type = ID_FVAR; minval(m), maxval(x), overrideval(0), storage(s)
flags = flagsv | (m > x ? IDF_READONLY : 0); {}
name = n;
}
/* ID_SVAR */ Svar::Svar(ostd::ConstCharRange name, char **s, VarCb f, int fl):
Svar::Svar(ostd::ConstCharRange n, char **s, VarCb f, int flagsv): Var(IdentType::svar, name, ostd::move(f), fl),
Var(ostd::move(f)), overrideval(nullptr), storage(s) overrideval(nullptr), storage(s)
{ {}
type = ID_SVAR;
flags = flagsv;
name = n;
}
/* ID_ALIAS */ Alias::Alias(ostd::ConstCharRange name, char *a, int fl):
Alias::Alias(ostd::ConstCharRange n, char *a, int flagsv): Ident(IdentType::alias, name, fl),
code(nullptr), stack(nullptr) code(nullptr), stack(nullptr)
{ {
type = ID_ALIAS;
flags = flagsv;
name = n;
val_v.set_mstr(a); 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) code(nullptr), stack(nullptr)
{ {
type = ID_ALIAS;
flags = flagsv;
name = n;
val_v.set_int(a); 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) code(nullptr), stack(nullptr)
{ {
type = ID_ALIAS;
flags = flagsv;
name = n;
val_v.set_float(a); 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) code(nullptr), stack(nullptr)
{ {
type = ID_ALIAS;
flags = flagsv;
name = n;
val_v.set_null(); 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) code(nullptr), stack(nullptr), val_v(v)
{ {}
type = ID_ALIAS;
flags = flagsv;
name = n;
}
/* ID_COMMAND */
Command::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 ostd::Uint32 amask, int nargs, CmdFunc f
): ):
Ident(IdentType::unknown, name, 0),
cargs(!args.empty() ? cs_dup_ostr(args) : nullptr), cargs(!args.empty() ? cs_dup_ostr(args) : nullptr),
argmask(amask), numargs(nargs), cb_cftv(ostd::move(f)) argmask(amask), numargs(nargs), cb_cftv(ostd::move(f))
{ {
type = t; type = tp;
flags = 0;
name = n;
} }
bool Ident::is_alias() const { bool Ident::is_alias() const {
@ -146,20 +127,6 @@ bool Ident::is_command() const {
return get_type() == IdentType::command; 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 { bool Ident::is_var() const {
IdentType tp = get_type(); IdentType tp = get_type();
return (tp >= IdentType::ivar) && (tp <= IdentType::svar); return (tp >= IdentType::ivar) && (tp <= IdentType::svar);

View File

@ -65,11 +65,8 @@ private:
OSTD_EXPORT bool code_is_empty(Bytecode const *code); OSTD_EXPORT bool code_is_empty(Bytecode const *code);
struct Ident; struct Ident;
struct Alias;
struct OSTD_EXPORT TaggedValue { struct OSTD_EXPORT TaggedValue {
friend struct Alias;
union { union {
CsInt i; /* ID_IVAR, VAL_INT */ CsInt i; /* ID_IVAR, VAL_INT */
CsFloat f; /* ID_FVAR, VAL_FLOAT */ CsFloat f; /* ID_FVAR, VAL_FLOAT */
@ -175,16 +172,16 @@ enum class IdentType {
ivar, fvar, svar, command, alias ivar, fvar, svar, command, alias
}; };
struct Command;
struct Var; struct Var;
struct Ivar; struct Ivar;
struct Fvar; struct Fvar;
struct Svar; struct Svar;
struct Alias;
struct OSTD_EXPORT Ident { struct OSTD_EXPORT Ident {
ostd::byte type; /* ID_something */ int type; /* ID_something */
ostd::ushort flags; ostd::ushort flags;
int index; int index = -1;
ostd::String name; ostd::String name;
IdentType get_type() const; IdentType get_type() const;
@ -194,8 +191,6 @@ struct OSTD_EXPORT Ident {
Alias const *get_alias() const; Alias const *get_alias() const;
bool is_command() const; bool is_command() const;
Command *get_command();
Command const *get_command() const;
bool is_var() const; bool is_var() const;
Var *get_var(); Var *get_var();
@ -214,7 +209,7 @@ struct OSTD_EXPORT Ident {
Svar const *get_svar() const; Svar const *get_svar() const;
protected: protected:
Ident(); Ident(IdentType tp, ostd::ConstCharRange name, int flags = 0);
}; };
using VarCb = ostd::Function<void(Ident &)>; using VarCb = ostd::Function<void(Ident &)>;
@ -229,7 +224,7 @@ struct OSTD_EXPORT Var: Ident {
} }
protected: protected:
Var(VarCb f); Var(IdentType tp, ostd::ConstCharRange name, VarCb func, int flags = 0);
}; };
struct OSTD_EXPORT Ivar: Var { 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 { struct IdentLink {
Ident *id; Ident *id;
IdentLink *next; IdentLink *next;
@ -321,6 +302,8 @@ struct IdentLink {
IdentStack *argstack; IdentStack *argstack;
}; };
using CmdFunc = ostd::Function<void(TvalRange, TaggedValue &)>;
struct OSTD_EXPORT CsState { struct OSTD_EXPORT CsState {
ostd::Map<ostd::ConstCharRange, Ident *> idents; ostd::Map<ostd::ConstCharRange, Ident *> idents;
ostd::Vector<Ident *> identmap; ostd::Vector<Ident *> identmap;