#include "scriptengine.h" #include "lexer.h" #include "parser.h" #include "expression.h" #include #include ScriptEngine::ScriptEngine() { } ScriptEngine::~ScriptEngine() { } Number ScriptEngine::exec( const Bu::String &sExpr ) { Bu::StaticMemBuf mb( sExpr.getStr(), sExpr.getSize() ); return exec( sExpr ); } Number ScriptEngine::exec( Bu::Stream &sInput ) { Lexer l( sInput ); Parser p( l ); Expression *pExp = p.parse(); Number n = exec( pExp ); delete pExp; return n; } Number ScriptEngine::exec( Expression *pExpr ) { NumStack sNums; for( Expression::iterator i = pExpr->begin(); i; i++ ) { switch( (*i).eType ) { case Token::tNumber: sNums.push( *(*i).nVal ); break; case Token::tVariable: if( hVarState.has( *(*i).sVal ) ) { sNums.push( hVarState[*(*i).sVal] ); } else { throw Bu::ExceptionBase( Bu::String("No such variable: $%1").arg(*(*i).sVal).end().getStr() ); } break; case Token::tPlus: { Number b = sNums.peekPop(); Number a = sNums.peekPop(); sNums.push( a + b ); } break; case Token::tMinus: { Number b = sNums.peekPop(); Number a = sNums.peekPop(); sNums.push( a - b ); } break; case Token::tDivide: { Number b = sNums.peekPop(); Number a = sNums.peekPop(); sNums.push( a / b ); } break; case Token::tMultiply: { Number b = sNums.peekPop(); Number a = sNums.peekPop(); sNums.push( a * b ); } break; case Token::tNegate: sNums.push( -sNums.peekPop() ); break; } } return sNums.peekPop(); }