mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-22 05:29:10 +08:00
[muparser] Clean up constructors and other miscellaneous
This commit is contained in:
parent
e675a66504
commit
732b32c8b4
@ -67,15 +67,9 @@ class ParserBase {
|
|||||||
*/
|
*/
|
||||||
typedef ValueOrError (ParserBase::*ParseFunction)() const;
|
typedef ValueOrError (ParserBase::*ParseFunction)() const;
|
||||||
|
|
||||||
/** \brief Type used for storing an array of values. */
|
|
||||||
typedef std::vector<value_type> valbuf_type;
|
|
||||||
|
|
||||||
/** \brief Type for a vector of strings. */
|
/** \brief Type for a vector of strings. */
|
||||||
typedef std::vector<string_type> stringbuf_type;
|
typedef std::vector<string_type> stringbuf_type;
|
||||||
|
|
||||||
/** \brief Typedef for the token reader. */
|
|
||||||
typedef ParserTokenReader token_reader_type;
|
|
||||||
|
|
||||||
/** \brief Type used for parser tokens. */
|
/** \brief Type used for parser tokens. */
|
||||||
typedef ParserToken<value_type, string_type> token_type;
|
typedef ParserToken<value_type, string_type> token_type;
|
||||||
|
|
||||||
@ -199,7 +193,6 @@ class ParserBase {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void InitTokenReader();
|
|
||||||
void ReInit() const;
|
void ReInit() const;
|
||||||
|
|
||||||
OptionalError AddCallback(const string_type &a_strName, const ParserCallback &a_Callback,
|
OptionalError AddCallback(const string_type &a_strName, const ParserCallback &a_Callback,
|
||||||
@ -247,8 +240,7 @@ class ParserBase {
|
|||||||
m_vStringBuf; ///< String buffer, used for storing string function arguments
|
m_vStringBuf; ///< String buffer, used for storing string function arguments
|
||||||
stringbuf_type m_vStringVarBuf;
|
stringbuf_type m_vStringVarBuf;
|
||||||
|
|
||||||
std::auto_ptr<token_reader_type>
|
std::unique_ptr<ParserTokenReader> m_pTokenReader;
|
||||||
m_pTokenReader; ///< Managed pointer to the token reader object.
|
|
||||||
|
|
||||||
funmap_type m_FunDef; ///< Map of function names and pointers.
|
funmap_type m_FunDef; ///< Map of function names and pointers.
|
||||||
funmap_type m_PostOprtDef; ///< Postfix operator callbacks
|
funmap_type m_PostOprtDef; ///< Postfix operator callbacks
|
||||||
@ -258,16 +250,17 @@ class ParserBase {
|
|||||||
strmap_type m_StrVarDef; ///< user defined string constants
|
strmap_type m_StrVarDef; ///< user defined string constants
|
||||||
varmap_type m_VarDef; ///< user defind variables.
|
varmap_type m_VarDef; ///< user defind variables.
|
||||||
|
|
||||||
bool m_bBuiltInOp; ///< Flag that can be used for switching built in operators on and off
|
bool m_bBuiltInOp =
|
||||||
|
true; ///< Flag that can be used for switching built in operators on and off
|
||||||
|
|
||||||
string_type m_sNameChars; ///< Charset for names
|
string_type m_sNameChars; ///< Charset for names
|
||||||
string_type m_sOprtChars; ///< Charset for postfix/ binary operator tokens
|
string_type m_sOprtChars; ///< Charset for postfix/ binary operator tokens
|
||||||
string_type m_sInfixOprtChars; ///< Charset for infix operator tokens
|
string_type m_sInfixOprtChars; ///< Charset for infix operator tokens
|
||||||
|
|
||||||
// items merely used for caching state information
|
// items merely used for caching state information
|
||||||
mutable valbuf_type
|
/// This is merely a buffer used for the stack in the cmd parsing routine
|
||||||
m_vStackBuffer; ///< This is merely a buffer used for the stack in the cmd parsing routine
|
mutable std::vector<value_type> m_vStackBuffer;
|
||||||
mutable int m_nFinalResultIdx;
|
mutable int m_nFinalResultIdx = 0;
|
||||||
};
|
};
|
||||||
} // namespace mu
|
} // namespace mu
|
||||||
|
|
||||||
|
@ -83,9 +83,6 @@ class ParserByteCode {
|
|||||||
/** \brief Token type for internal use only. */
|
/** \brief Token type for internal use only. */
|
||||||
typedef ParserToken<value_type, string_type> token_type;
|
typedef ParserToken<value_type, string_type> token_type;
|
||||||
|
|
||||||
/** \brief Token vector for storing the RPN. */
|
|
||||||
typedef std::vector<SToken> rpn_type;
|
|
||||||
|
|
||||||
/** \brief Position in the Calculation array. */
|
/** \brief Position in the Calculation array. */
|
||||||
unsigned m_iStackPos;
|
unsigned m_iStackPos;
|
||||||
|
|
||||||
@ -93,7 +90,7 @@ class ParserByteCode {
|
|||||||
std::size_t m_iMaxStackSize;
|
std::size_t m_iMaxStackSize;
|
||||||
|
|
||||||
/** \brief The actual rpn storage. */
|
/** \brief The actual rpn storage. */
|
||||||
rpn_type m_vRPN;
|
std::vector<SToken> m_vRPN;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ParserByteCode();
|
ParserByteCode();
|
||||||
|
@ -64,25 +64,7 @@ const char_type *ParserBase::c_DefaultOprt[] = {
|
|||||||
\param a_szFormula the formula to interpret.
|
\param a_szFormula the formula to interpret.
|
||||||
\throw ParserException if a_szFormula is null.
|
\throw ParserException if a_szFormula is null.
|
||||||
*/
|
*/
|
||||||
ParserBase::ParserBase()
|
ParserBase::ParserBase() : m_pTokenReader(new ParserTokenReader(this)) {}
|
||||||
: m_vRPN(),
|
|
||||||
m_vStringBuf(),
|
|
||||||
m_pTokenReader(),
|
|
||||||
m_FunDef(),
|
|
||||||
m_PostOprtDef(),
|
|
||||||
m_InfixOprtDef(),
|
|
||||||
m_OprtDef(),
|
|
||||||
m_ConstDef(),
|
|
||||||
m_StrVarDef(),
|
|
||||||
m_VarDef(),
|
|
||||||
m_bBuiltInOp(true),
|
|
||||||
m_sNameChars(),
|
|
||||||
m_sOprtChars(),
|
|
||||||
m_sInfixOprtChars(),
|
|
||||||
m_vStackBuffer(),
|
|
||||||
m_nFinalResultIdx(0) {
|
|
||||||
InitTokenReader();
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
ParserBase::~ParserBase() = default;
|
ParserBase::~ParserBase() = default;
|
||||||
@ -124,16 +106,6 @@ void ParserBase::ResetLocale() {
|
|||||||
SetArgSep(',');
|
SetArgSep(',');
|
||||||
}
|
}
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
|
||||||
/** \brief Initialize the token reader.
|
|
||||||
|
|
||||||
Create new token reader object and submit pointers to function, operator,
|
|
||||||
constant and variable definitions.
|
|
||||||
|
|
||||||
\post m_pTokenReader.get()!=0
|
|
||||||
*/
|
|
||||||
void ParserBase::InitTokenReader() { m_pTokenReader.reset(new token_reader_type(this)); }
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------------
|
//---------------------------------------------------------------------------
|
||||||
/** \brief Reset parser to string parsing mode and clear internal buffers.
|
/** \brief Reset parser to string parsing mode and clear internal buffers.
|
||||||
|
|
||||||
@ -539,7 +511,6 @@ OptionalError ParserBase::ApplyStrFunc(const token_type &a_FunTok,
|
|||||||
return Error(ecVAL_EXPECTED, m_pTokenReader->GetPos(), a_FunTok.GetAsString());
|
return Error(ecVAL_EXPECTED, m_pTokenReader->GetPos(), a_FunTok.GetAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// string functions won't be optimized
|
|
||||||
m_vRPN.AddStrFun(pFunc, a_FunTok.GetArgCount(), a_vArg.back().GetIdx());
|
m_vRPN.AddStrFun(pFunc, a_FunTok.GetArgCount(), a_vArg.back().GetIdx());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
@ -766,7 +737,6 @@ ValueOrError ParserBase::InvokeFunction(generic_fun_type func, const value_type
|
|||||||
ValueOrError ParserBase::ExecuteRPN() const {
|
ValueOrError ParserBase::ExecuteRPN() const {
|
||||||
assert(! m_vRPN.empty() && "Missing RPN");
|
assert(! m_vRPN.empty() && "Missing RPN");
|
||||||
value_type *Stack = &m_vStackBuffer[0];
|
value_type *Stack = &m_vStackBuffer[0];
|
||||||
value_type buf;
|
|
||||||
int sidx(0);
|
int sidx(0);
|
||||||
for (const SToken *pTok = m_vRPN.GetBase(); pTok->Cmd != cmEND; ++pTok) {
|
for (const SToken *pTok = m_vRPN.GetBase(); pTok->Cmd != cmEND; ++pTok) {
|
||||||
switch (pTok->Cmd) {
|
switch (pTok->Cmd) {
|
||||||
|
@ -179,27 +179,30 @@ void ParserByteCode::Finalize() {
|
|||||||
SToken tok;
|
SToken tok;
|
||||||
tok.Cmd = cmEND;
|
tok.Cmd = cmEND;
|
||||||
m_vRPN.push_back(tok);
|
m_vRPN.push_back(tok);
|
||||||
rpn_type(m_vRPN).swap(m_vRPN); // shrink bytecode vector to fit
|
m_vRPN.shrink_to_fit();
|
||||||
|
|
||||||
// Determine the if-then-else jump offsets
|
// Determine the if-then-else jump offsets
|
||||||
ParserStack<int> stIf, stElse;
|
ParserStack<int> stIf, stElse;
|
||||||
int idx;
|
|
||||||
for (int i = 0; i < (int)m_vRPN.size(); ++i) {
|
for (int i = 0; i < (int)m_vRPN.size(); ++i) {
|
||||||
switch (m_vRPN[i].Cmd) {
|
switch (m_vRPN[i].Cmd) {
|
||||||
case cmIF:
|
case cmIF:
|
||||||
stIf.push_back(i);
|
stIf.push_back(i);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case cmELSE:
|
case cmELSE: {
|
||||||
|
int idx = stIf.back();
|
||||||
|
stIf.pop_back();
|
||||||
|
m_vRPN[idx].Oprt.offset = i - idx;
|
||||||
stElse.push_back(i);
|
stElse.push_back(i);
|
||||||
idx = stIf.pop();
|
|
||||||
m_vRPN[idx].Oprt.offset = i - idx;
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case cmENDIF:
|
case cmENDIF: {
|
||||||
idx = stElse.pop();
|
int idx = stElse.back();
|
||||||
|
stElse.pop_back();
|
||||||
m_vRPN[idx].Oprt.offset = i - idx;
|
m_vRPN[idx].Oprt.offset = i - idx;
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user