summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2016-12-01 11:16:25 -0700
committerMike Buland <eichlan@xagasoft.com>2016-12-01 11:16:25 -0700
commit32b48aeb75db4ad40fe0173614e585f9bee72257 (patch)
tree36ec0bbb72ece290ba82906edf3ae6c50f2a0984
parentb96daa4e9849ac9caf5c0dfcea8daac28267ea19 (diff)
downloadclic-32b48aeb75db4ad40fe0173614e585f9bee72257.tar.gz
clic-32b48aeb75db4ad40fe0173614e585f9bee72257.tar.bz2
clic-32b48aeb75db4ad40fe0173614e585f9bee72257.tar.xz
clic-32b48aeb75db4ad40fe0173614e585f9bee72257.zip
Found a big problem with order of operations.
I believe this is because I'm using right tail recursion, but I'm not really sure how I could change it for this program. order of operations is being observed perfectly, but then operations are being performed from right to left, not left to right. I think I may be reducing too frequently, honestly.
-rw-r--r--src/parser.cpp38
-rw-r--r--src/parser.h1
-rw-r--r--src/scriptengine.cpp24
-rw-r--r--src/token.h1
4 files changed, 46 insertions, 18 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index c7cf994..701c4bd 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -20,8 +20,7 @@ Parser::~Parser()
20Expression *Parser::parse() 20Expression *Parser::parse()
21{ 21{
22 pCurExp = new Expression(); 22 pCurExp = new Expression();
23 exprR(); 23 statement();
24 reduce();
25 24
26 printState("Final"); 25 printState("Final");
27 26
@@ -40,7 +39,7 @@ void Parser::exprR()
40 39
41void Parser::expr() 40void Parser::expr()
42{ 41{
43 //Bu::sio << "::expr " << lex[0] << Bu::sio.nl; 42 Bu::sio << "::expr " << lex[0] << Bu::sio.nl;
44 if( lex[0].eType == Token::tEndOfInput ) 43 if( lex[0].eType == Token::tEndOfInput )
45 return; 44 return;
46 45
@@ -57,7 +56,7 @@ void Parser::expr()
57 case Token::tModulus: 56 case Token::tModulus:
58 Token t = lex[0]; 57 Token t = lex[0];
59 lex.nextToken(); 58 lex.nextToken();
60 //Bu::sio << "->expr " << t << " " << lex[0] << Bu::sio.nl; 59 Bu::sio << "->expr " << t << " " << lex[0] << Bu::sio.nl;
61 if( lex[0].eType == Token::tOpenParen ) 60 if( lex[0].eType == Token::tOpenParen )
62 { 61 {
63 exprP(); 62 exprP();
@@ -70,10 +69,10 @@ void Parser::expr()
70 if( lex[0].eType == Token::tNumber || 69 if( lex[0].eType == Token::tNumber ||
71 lex[0].eType == Token::tVariable ) 70 lex[0].eType == Token::tVariable )
72 { 71 {
73 //Bu::sio << "->expr " << t << 72 Bu::sio << "->expr " << t <<
74 // " " << lex[0] << 73 " " << lex[0] <<
75 // " " << lex[1] << 74 " " << lex[1] <<
76 // Bu::sio.nl; 75 Bu::sio.nl;
77 shift( lex[0] ); 76 shift( lex[0] );
78 if( lex[1].eType&Token::mMetaOperator ) 77 if( lex[1].eType&Token::mMetaOperator )
79 { 78 {
@@ -111,14 +110,14 @@ void Parser::expr()
111 else if( lex[0].eType == Token::tMinus ) 110 else if( lex[0].eType == Token::tMinus )
112 { 111 {
113 // This is negation 112 // This is negation
114 //Bu::sio << "next token: " << lex[0] << Bu::sio.nl; 113 Bu::sio << "next token: " << lex[0] << Bu::sio.nl;
115 printState("inline-negate"); 114 printState("inline-negate");
116 exprP(); 115 exprP();
117 printState("inline-negate-post"); 116 printState("inline-negate-post");
118 shift( t ); 117 shift( t );
119 printState("inline-negate-post2"); 118 printState("inline-negate-post2");
120 119
121 //Bu::sio << "??? " << lex[0] << Bu::sio.nl; 120 Bu::sio << "??? " << lex[0] << Bu::sio.nl;
122 } 121 }
123 } 122 }
124 break; 123 break;
@@ -157,7 +156,7 @@ void Parser::exprP()
157 exprR(); 156 exprR();
158 if( lex[0].eType != Token::tCloseParen ) 157 if( lex[0].eType != Token::tCloseParen )
159 { 158 {
160 //Bu::sio << "::exprP " << lex[0] << Bu::sio.nl; 159 Bu::sio << "::exprP " << lex[0] << Bu::sio.nl;
161 throw Bu::ExceptionBase("Expected close paren"); 160 throw Bu::ExceptionBase("Expected close paren");
162 } 161 }
163 shift( lex[0] ); 162 shift( lex[0] );
@@ -175,6 +174,21 @@ void Parser::exprP()
175 break; 174 break;
176 } 175 }
177} 176}
177
178void Parser::statement()
179{
180 if( lex[0].eType == Token::tCommand )
181 {
182 lex.setMode( Lexer::modeCommand );
183 lex.setMode( Lexer::modeNormal );
184 }
185 else
186 {
187 exprR();
188 reduce();
189 }
190}
191
178/* 192/*
179case Token::tCommand: 193case Token::tCommand:
180 lex.setMode( Lexer::modeCommand ); 194 lex.setMode( Lexer::modeCommand );
@@ -403,7 +417,6 @@ int Parser::getPriority( Token::Type eType )
403 417
404void Parser::printState( const Bu::String &sTag ) 418void Parser::printState( const Bu::String &sTag )
405{ 419{
406 /*
407 Bu::sio << "----------------" << Bu::sio.nl; 420 Bu::sio << "----------------" << Bu::sio.nl;
408 Bu::sio << sTag << " stack:"; 421 Bu::sio << sTag << " stack:";
409 for( TokenStack::iterator i = tsParse.begin(); i; i++ ) 422 for( TokenStack::iterator i = tsParse.begin(); i; i++ )
@@ -418,6 +431,5 @@ void Parser::printState( const Bu::String &sTag )
418 } 431 }
419 Bu::sio << Bu::sio.nl; 432 Bu::sio << Bu::sio.nl;
420 Bu::sio << "----------------" << Bu::sio.nl; 433 Bu::sio << "----------------" << Bu::sio.nl;
421 */
422} 434}
423 435
diff --git a/src/parser.h b/src/parser.h
index 1e0d1c4..60dd8e1 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -55,6 +55,7 @@ private:
55 void expr(); 55 void expr();
56 void exprP(); 56 void exprP();
57 void exprR(); 57 void exprR();
58 void statement();
58 59
59private: 60private:
60 void shift( const Token &t ); 61 void shift( const Token &t );
diff --git a/src/scriptengine.cpp b/src/scriptengine.cpp
index 7b5df03..b828409 100644
--- a/src/scriptengine.cpp
+++ b/src/scriptengine.cpp
@@ -56,19 +56,35 @@ Number ScriptEngine::exec( Expression *pExpr )
56 break; 56 break;
57 57
58 case Token::tPlus: 58 case Token::tPlus:
59 sNums.push( sNums.peekPop() + sNums.peekPop() ); 59 {
60 Number a = sNums.peekPop();
61 Number b = sNums.peekPop();
62 sNums.push( a + b );
63 }
60 break; 64 break;
61 65
62 case Token::tMinus: 66 case Token::tMinus:
63 sNums.push( sNums.peekPop() - sNums.peekPop() ); 67 {
68 Number a = sNums.peekPop();
69 Number b = sNums.peekPop();
70 sNums.push( a - b );
71 }
64 break; 72 break;
65 73
66 case Token::tDivide: 74 case Token::tDivide:
67 sNums.push( sNums.peekPop() / sNums.peekPop() ); 75 {
76 Number a = sNums.peekPop();
77 Number b = sNums.peekPop();
78 sNums.push( a / b );
79 }
68 break; 80 break;
69 81
70 case Token::tMultiply: 82 case Token::tMultiply:
71 sNums.push( sNums.peekPop() * sNums.peekPop() ); 83 {
84 Number a = sNums.peekPop();
85 Number b = sNums.peekPop();
86 sNums.push( a * b );
87 }
72 break; 88 break;
73 89
74 case Token::tNegate: 90 case Token::tNegate:
diff --git a/src/token.h b/src/token.h
index 4ad1136..5bede41 100644
--- a/src/token.h
+++ b/src/token.h
@@ -40,7 +40,6 @@ public:
40 mMetaOperator = 0x4000, 40 mMetaOperator = 0x4000,
41 mMetaAltOp = 0x8000, 41 mMetaAltOp = 0x8000,
42 mMetaMeta = 0x0100, 42 mMetaMeta = 0x0100,
43
44 }; 43 };
45 44
46 Token(); 45 Token();