26 pStmt = ShParse(session);
34 if( !bIsIteractive (rc !=
DYNA_OK) )
41 void ShError(
const char *sFmt, ...)
103 ShVal(ShDataType eObjType,
const char *sObjName)
106 m_eObjType = eObjType;
107 m_sObjName =
NEWSTR(sObjName);
115 static ShVal *New(
bool val);
116 static ShVal *New(
int val);
117 static ShVal *New(
double val);
118 static ShVal *New(
const char *val);
120 ShDataType GetObjType()
const 125 const char *GetObjName()
const 130 int GetStatus()
const 135 int SetStatus(
int nStatus)
150 virtual bool ToBool()
const = 0;
151 virtual int ToInt()
const = 0;
152 virtual double ToReal()
const = 0;
153 virtual const char *ToString()
const = 0;
161 virtual bool IsEQ(
const ShVal *pRhs) = 0;
162 virtual bool IsNotEQ(
const ShVal *pRhs) = 0;
163 virtual bool IsGT(
const ShVal *pRhs) = 0;
164 virtual bool IsGE(
const ShVal *pRhs) = 0;
165 virtual bool IsLT(
const ShVal *pRhs) = 0;
166 virtual bool IsLE(
const ShVal *pRhs) = 0;
171 ShDataType m_eObjType;
172 const char *m_sObjName;
185 virtual bool ToBool()
const 187 return m_val != 0?
true:
false;
190 virtual int ToInt()
const 195 virtual double ToReal()
const 197 return (
double)m_val;
200 virtual const char *ToString()
const 219 virtual void copy(
const ShVal *pOrig)
221 m_val = pOrig->ToInt();
235 virtual bool ToBool()
const 237 return m_val != 0.0?
true:
false;
240 virtual int ToInt()
const 245 virtual double ToReal()
const 250 virtual const char *ToString()
const 269 virtual void copy(
const ShVal *pOrig)
271 m_val = pOrig->ToReal();
279 switch( pRhs->GetObjType() )
283 pVal =
new ShValInt(m_val + pRhs->ToInt());
286 pVal =
new ShValReal(ToReal() + pRhs->ToReal());
289 ShError(
"Error: Unsupported operand types for 'int' and 'str'.");
295 ShError(
"Error: Undefined R.H.S.");
308 switch( pRhs->GetObjType() )
313 pVal =
new ShValReal(m_val + pRhs->ToReal());
316 ShError(
"Error: Unsupported operand types for 'real' and 'string'.");
322 ShError(
"Error: Undefined R.H.S.");
335 ShLang(ShLangToken eObjType,
const char *sObjName)
338 m_sObjName =
NEWSTR(sObjName);
347 const ShVal *GetResult()
356 ShLangToken m_eObjType;
357 const char *m_sObjName;
370 argv[0] = m_sCmdName;
372 for(i=1, rc=
DYNA_OK; i<m_argc; ++i)
374 if( (rc = m_vecArg[i]->Eval()) ==
DYNA_OK )
376 argv[i] = m_pArg->GetResult()->ToString();
382 rc = Exec(session, argc, argv);
385 for(i=1; i<m_argc; ++i)
390 return m_result.setstatus(rc);
397 :
ShLang(TokenAssignStmt,
"assignment");
401 if( (rc = m_pExpr->Eval()) ==
DYNA_OK )
403 rc = ShHashSet(m_sIdentifier, m_pExpr->GetResult());
405 return m_result.setstatus(rc);
412 :
ShLang(TokenIfStmt,
"if");
416 if( ((rc = m_pCond->Eval()) ==
DYNA_OK) &&
417 (m_pCond->GetResult()->ToBool() ==
true) )
419 rc = m_pBlockStmt->Eval();
421 return m_result.setstatus(rc);
433 rc = m_pBlockStmt->Eval();
434 return m_result.setstatus(rc);
441 :
ShLang(TokenWhileStmt,
"while");
447 while( ((rc = m_pCond->Eval()) ==
DYNA_OK) &&
448 (m_pCond->GetResult()->ToBool() ==
true) )
450 if( (rc = m_pBlockStmt->Eval()) !=
DYNA_OK )
467 for(i=0; i<m_vecStmts.size(); ++i)
469 if( (rc = m_vecStmts[i]->Eval()) !=
DYNA_OK )
474 return m_result.setstatus(rc);
488 if( (rc = m_pAddExpr->Eval()) ==
DYNA_OK )
490 m_result = m_pAddExpr->GetResult();
492 return m_result.setstatus(rc);
496 ShLangAddExpr *m_pLhs;
499 class ShLangAddExpr :
public ShLang 502 ShLangAddExpr(ShLangAddExpr *pLhsExpr, ShBinOp eOp,
ShLangMulExpr *pRhsExpr)
503 :
ShLang(TokenAddExpr,
"add-expr")
505 m_pLhsExpr = pLhsExpr;
507 m_pRhsExpr = pRhsExpr;
514 m_eOp = ShBinOpUnary;
515 m_pRhsExpr = pMulExpr;
519 virtual ~ShLangAddExpr()
532 if( (rc = pRhs->Eval()) !=
DYNA_OK )
536 else if( (pLhs != NULL) && (rc = pLhs->Eval()) !=
DYNA_OK )
544 m_pResult = m_pLhs->GetResult()->Add(m_pRhs);
547 m_pResult = m_pLhs->GetResult()->Sub(m_pRhs);
550 m_pResult = m_pRhs->GetResult()->Dup();
553 return m_pResult->GetStatus();
557 ShLangAddExpr *m_pLhs;
569 m_pLhsExpr = pLhsExpr;
571 m_pRhsExpr = pRhsExpr;
578 m_eOp = ShBinOpUnary;
579 m_pRhsExpr = pPrimaryExpr;
583 virtual ~ShLangAddExpr()
596 if( (rc = pRhs->Eval()) !=
DYNA_OK )
600 else if( (pLhs != NULL) && (rc = pLhs->Eval()) !=
DYNA_OK )
608 m_pResult = m_pLhs->Add(m_pRhs);
611 m_pResult = m_pLhs->Sub(m_pRhs);
614 m_pResult = m_pRhs->Copy();
617 return m_pResult->GetStatus();
631 m_sIdentifier = NULL;
632 m_pResult =
new ShVal::New(val);
638 m_sIdentifier = NULL;
639 m_pResult =
new ShVal::New(val);
645 m_sIdentifier = NULL;
646 m_pResult =
new ShVal::New(val);
652 m_sIdentifier = NULL;
653 m_pResult =
new ShVal::New(val);
658 :
ShLang(TokenPrimaryExpr,
"identifier")
660 m_eType = INDENTIFIER;
661 m_sIdentifier =
newstr(val);
677 m_pResult = ShHashGet(m_sIdentifier);
685 return m_pResult->GetStatus();
char * newstr(const char *s)
Allocate new duplicated string.
#define NEWSTR(s)
Allocate a new duplicated string convenience macro.
#define DYNA_OK
not an error, success
#define DELOBJ(p)
Delete an allocated object convenience macro.
#define DYNA_ECODE_PARSE
Shell parse error.
void delstr(const char *s)
A safer string delete utility.
#define DELSTR(s)
Delete an allocated string convenience macro.