summaryrefslogtreecommitdiff
path: root/src/scriptengine.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2016-12-01 15:51:52 -0700
committerMike Buland <eichlan@xagasoft.com>2016-12-01 15:51:52 -0700
commit5d59aa3e9dffe2912215335ce0b76c67ebbe5a4e (patch)
treefa4f7b73f912b0b5ff87280c0bc4dc118a82392d /src/scriptengine.cpp
parent17a39c19e5bff97c3b3d2bc888a3bb5ded7c1b96 (diff)
downloadclic-5d59aa3e9dffe2912215335ce0b76c67ebbe5a4e.tar.gz
clic-5d59aa3e9dffe2912215335ce0b76c67ebbe5a4e.tar.bz2
clic-5d59aa3e9dffe2912215335ce0b76c67ebbe5a4e.tar.xz
clic-5d59aa3e9dffe2912215335ce0b76c67ebbe5a4e.zip
Signals solve many problems.
The command structures will be changed, I think. I want the lexer to actually lex the command names into tokens, then the parser and the engine can both use them to update their state when necesarry. It will be less ambiguous and easier for both sides to stay synchronized.
Diffstat (limited to 'src/scriptengine.cpp')
-rw-r--r--src/scriptengine.cpp75
1 files changed, 64 insertions, 11 deletions
diff --git a/src/scriptengine.cpp b/src/scriptengine.cpp
index 65b6ec2..b65db70 100644
--- a/src/scriptengine.cpp
+++ b/src/scriptengine.cpp
@@ -5,8 +5,12 @@
5 5
6#include <bu/staticmembuf.h> 6#include <bu/staticmembuf.h>
7#include <bu/exceptionbase.h> 7#include <bu/exceptionbase.h>
8#include <bu/sio.h>
8 9
9ScriptEngine::ScriptEngine() 10#include <stdlib.h>
11
12ScriptEngine::ScriptEngine() :
13 bRunning( true )
10{ 14{
11} 15}
12 16
@@ -14,23 +18,22 @@ ScriptEngine::~ScriptEngine()
14{ 18{
15} 19}
16 20
17Number ScriptEngine::exec( const Bu::String &sExpr ) 21void ScriptEngine::exec( const Bu::String &sExpr )
18{ 22{
19 Bu::StaticMemBuf mb( sExpr.getStr(), sExpr.getSize() ); 23 Bu::StaticMemBuf mb( sExpr.getStr(), sExpr.getSize() );
20 return exec( sExpr ); 24 exec( sExpr );
21} 25}
22 26
23Number ScriptEngine::exec( Bu::Stream &sInput ) 27void ScriptEngine::exec( Bu::Stream &sInput )
24{ 28{
25 Lexer l( sInput ); 29 Lexer l( sInput );
26 Parser p( l ); 30 Parser p( l );
27 Expression *pExp = p.parse(); 31 Expression *pExp = p.parse();
28 Number n = exec( pExp ); 32 exec( pExp );
29 delete pExp; 33 delete pExp;
30 return n;
31} 34}
32 35
33Number ScriptEngine::exec( Expression *pExpr ) 36void ScriptEngine::exec( Expression *pExpr )
34{ 37{
35 NumStack sNums; 38 NumStack sNums;
36 39
@@ -49,9 +52,12 @@ Number ScriptEngine::exec( Expression *pExpr )
49 } 52 }
50 else 53 else
51 { 54 {
52 throw Bu::ExceptionBase( 55 if( sigError.isSet() )
53 Bu::String("No such variable: $%1").arg(*(*i).sVal).end().getStr() 56 sigError(
54 ); 57 Bu::String("No such variable: $%1").
58 arg(*(*i).sVal)
59 );
60 return;
55 } 61 }
56 break; 62 break;
57 63
@@ -90,9 +96,56 @@ Number ScriptEngine::exec( Expression *pExpr )
90 case Token::tNegate: 96 case Token::tNegate:
91 sNums.push( -sNums.peekPop() ); 97 sNums.push( -sNums.peekPop() );
92 break; 98 break;
99
100 case Token::tCommand:
101 command( i );
102 return;
93 } 103 }
94 } 104 }
95 105
96 return sNums.peekPop(); 106 if( sigNumResult.isSet() )
107 sigNumResult( sNums.peekPop() );
108}
109
110void ScriptEngine::command( Expression::iterator &i )
111{
112 Bu::String sCmd = *(*i).sVal;
113 if( sCmd == "exit" || sCmd == "quit" )
114 {
115 bRunning = false;
116 return;
117 }
118 else if( sCmd == "scale" )
119 {
120 if( !(++i) )
121 {
122 if( sigError.isSet() )
123 sigError("You must provide a positive integer.");
124 return;
125 }
126 int32_t iScale = strtol( (*i).sVal->getStr(), 0, 10 );
127 if( iScale < 0 )
128 {
129 if( sigError.isSet() )
130 sigError("You must provide a positive integer.");
131 return;
132 }
133 if( sigMessage.isSet() )
134 sigMessage(Bu::String("Changed scale to: %1").arg( iScale ));
135 for( VarHash::iterator i = hVarState.begin(); i; i++ )
136 (*i).setScale( iScale );
137 }
138 else if( sCmd == "radix" )
139 {
140 }
141 else if( sCmd == "vars" )
142 {
143 }
144 else if( sCmd == "help" || sCmd == "?" )
145 {
146 }
147 else
148 {
149 }
97} 150}
98 151