diff options
Diffstat (limited to 'src/parser.cpp')
-rw-r--r-- | src/parser.cpp | 65 |
1 files changed, 34 insertions, 31 deletions
diff --git a/src/parser.cpp b/src/parser.cpp index 04a81ce..c7cf994 100644 --- a/src/parser.cpp +++ b/src/parser.cpp | |||
@@ -3,14 +3,13 @@ | |||
3 | #include "lexer.h" | 3 | #include "lexer.h" |
4 | #include "number.h" | 4 | #include "number.h" |
5 | #include "datafiles.h" | 5 | #include "datafiles.h" |
6 | #include "expression.h" | ||
6 | 7 | ||
7 | #include <bu/sio.h> | 8 | #include <bu/sio.h> |
8 | #include <stdlib.h> | 9 | #include <stdlib.h> |
9 | 10 | ||
10 | 11 | Parser::Parser( Lexer &lex ) : | |
11 | Parser::Parser( Lexer &lex, Bu::Stream &rOut ) : | 12 | lex( lex ) |
12 | lex( lex ), | ||
13 | rOut( rOut ) | ||
14 | { | 13 | { |
15 | } | 14 | } |
16 | 15 | ||
@@ -18,22 +17,19 @@ Parser::~Parser() | |||
18 | { | 17 | { |
19 | } | 18 | } |
20 | 19 | ||
21 | void Parser::parse() | 20 | Expression *Parser::parse() |
22 | { | 21 | { |
23 | for(;;) | 22 | pCurExp = new Expression(); |
24 | { | 23 | exprR(); |
25 | lex.nextToken(); | 24 | reduce(); |
26 | if( lex[0].eType == Token::tEndOfInput ) | ||
27 | break; | ||
28 | 25 | ||
29 | exprR(); | 26 | printState("Final"); |
30 | reduce(); | ||
31 | 27 | ||
32 | printState("Final"); | 28 | tsParse.clear(); |
33 | 29 | ||
34 | tsParse.clear(); | 30 | Expression *pTmp = pCurExp; |
35 | tsScript.clear(); | 31 | pCurExp = NULL; |
36 | } | 32 | return pTmp; |
37 | } | 33 | } |
38 | 34 | ||
39 | void Parser::exprR() | 35 | void Parser::exprR() |
@@ -44,7 +40,7 @@ void Parser::exprR() | |||
44 | 40 | ||
45 | void Parser::expr() | 41 | void Parser::expr() |
46 | { | 42 | { |
47 | Bu::sio << "::expr " << lex[0] << Bu::sio.nl; | 43 | //Bu::sio << "::expr " << lex[0] << Bu::sio.nl; |
48 | if( lex[0].eType == Token::tEndOfInput ) | 44 | if( lex[0].eType == Token::tEndOfInput ) |
49 | return; | 45 | return; |
50 | 46 | ||
@@ -61,7 +57,7 @@ void Parser::expr() | |||
61 | case Token::tModulus: | 57 | case Token::tModulus: |
62 | Token t = lex[0]; | 58 | Token t = lex[0]; |
63 | lex.nextToken(); | 59 | lex.nextToken(); |
64 | Bu::sio << "->expr " << t << " " << lex[0] << Bu::sio.nl; | 60 | //Bu::sio << "->expr " << t << " " << lex[0] << Bu::sio.nl; |
65 | if( lex[0].eType == Token::tOpenParen ) | 61 | if( lex[0].eType == Token::tOpenParen ) |
66 | { | 62 | { |
67 | exprP(); | 63 | exprP(); |
@@ -74,10 +70,10 @@ void Parser::expr() | |||
74 | if( lex[0].eType == Token::tNumber || | 70 | if( lex[0].eType == Token::tNumber || |
75 | lex[0].eType == Token::tVariable ) | 71 | lex[0].eType == Token::tVariable ) |
76 | { | 72 | { |
77 | Bu::sio << "->expr " << t << | 73 | //Bu::sio << "->expr " << t << |
78 | " " << lex[0] << | 74 | // " " << lex[0] << |
79 | " " << lex[1] << | 75 | // " " << lex[1] << |
80 | Bu::sio.nl; | 76 | // Bu::sio.nl; |
81 | shift( lex[0] ); | 77 | shift( lex[0] ); |
82 | if( lex[1].eType&Token::mMetaOperator ) | 78 | if( lex[1].eType&Token::mMetaOperator ) |
83 | { | 79 | { |
@@ -115,14 +111,14 @@ void Parser::expr() | |||
115 | else if( lex[0].eType == Token::tMinus ) | 111 | else if( lex[0].eType == Token::tMinus ) |
116 | { | 112 | { |
117 | // This is negation | 113 | // This is negation |
118 | Bu::sio << "next token: " << lex[0] << Bu::sio.nl; | 114 | //Bu::sio << "next token: " << lex[0] << Bu::sio.nl; |
119 | printState("inline-negate"); | 115 | printState("inline-negate"); |
120 | exprP(); | 116 | exprP(); |
121 | printState("inline-negate-post"); | 117 | printState("inline-negate-post"); |
122 | shift( t ); | 118 | shift( t ); |
123 | printState("inline-negate-post2"); | 119 | printState("inline-negate-post2"); |
124 | 120 | ||
125 | Bu::sio << "??? " << lex[0] << Bu::sio.nl; | 121 | //Bu::sio << "??? " << lex[0] << Bu::sio.nl; |
126 | } | 122 | } |
127 | } | 123 | } |
128 | break; | 124 | break; |
@@ -161,7 +157,7 @@ void Parser::exprP() | |||
161 | exprR(); | 157 | exprR(); |
162 | if( lex[0].eType != Token::tCloseParen ) | 158 | if( lex[0].eType != Token::tCloseParen ) |
163 | { | 159 | { |
164 | Bu::sio << "::exprP " << lex[0] << Bu::sio.nl; | 160 | //Bu::sio << "::exprP " << lex[0] << Bu::sio.nl; |
165 | throw Bu::ExceptionBase("Expected close paren"); | 161 | throw Bu::ExceptionBase("Expected close paren"); |
166 | } | 162 | } |
167 | shift( lex[0] ); | 163 | shift( lex[0] ); |
@@ -283,7 +279,7 @@ void Parser::reduce() | |||
283 | { | 279 | { |
284 | case Token::tNumber: | 280 | case Token::tNumber: |
285 | case Token::tVariable: | 281 | case Token::tVariable: |
286 | tsScript.append( tOp ); | 282 | output( tOp ); |
287 | tsParse.push( Token( Token::tComputedValue ) ); | 283 | tsParse.push( Token( Token::tComputedValue ) ); |
288 | break; | 284 | break; |
289 | 285 | ||
@@ -303,14 +299,14 @@ void Parser::reduce() | |||
303 | if( t.eType == Token::tNumber || | 299 | if( t.eType == Token::tNumber || |
304 | t.eType == Token::tVariable ) | 300 | t.eType == Token::tVariable ) |
305 | { | 301 | { |
306 | tsScript.append( t ); | 302 | output( t ); |
307 | } | 303 | } |
308 | else if( t.eType == Token::tComputedValue ) | 304 | else if( t.eType == Token::tComputedValue ) |
309 | { | 305 | { |
310 | // Nope, we don't care | 306 | // Nope, we don't care |
311 | } | 307 | } |
312 | } | 308 | } |
313 | tsScript.append( tOp ); | 309 | output( tOp ); |
314 | shift( Token( Token::tComputedValue ) ); | 310 | shift( Token( Token::tComputedValue ) ); |
315 | break; | 311 | break; |
316 | 312 | ||
@@ -318,8 +314,8 @@ void Parser::reduce() | |||
318 | { | 314 | { |
319 | Token t = tsParse.peekPop(); | 315 | Token t = tsParse.peekPop(); |
320 | if( t.eType != Token::tComputedValue ) | 316 | if( t.eType != Token::tComputedValue ) |
321 | tsScript.append( t ); | 317 | output( t ); |
322 | tsScript.append( tOp ); | 318 | output( tOp ); |
323 | shift( Token( Token::tComputedValue ) ); | 319 | shift( Token( Token::tComputedValue ) ); |
324 | } | 320 | } |
325 | break; | 321 | break; |
@@ -345,6 +341,11 @@ void Parser::reduce() | |||
345 | } | 341 | } |
346 | } | 342 | } |
347 | 343 | ||
344 | void Parser::output( const Token &t ) | ||
345 | { | ||
346 | pCurExp->append( t ); | ||
347 | } | ||
348 | |||
348 | int Parser::reqTokens( Token::Type eType ) | 349 | int Parser::reqTokens( Token::Type eType ) |
349 | { | 350 | { |
350 | switch( eType ) | 351 | switch( eType ) |
@@ -402,6 +403,7 @@ int Parser::getPriority( Token::Type eType ) | |||
402 | 403 | ||
403 | void Parser::printState( const Bu::String &sTag ) | 404 | void Parser::printState( const Bu::String &sTag ) |
404 | { | 405 | { |
406 | /* | ||
405 | Bu::sio << "----------------" << Bu::sio.nl; | 407 | Bu::sio << "----------------" << Bu::sio.nl; |
406 | Bu::sio << sTag << " stack:"; | 408 | Bu::sio << sTag << " stack:"; |
407 | for( TokenStack::iterator i = tsParse.begin(); i; i++ ) | 409 | for( TokenStack::iterator i = tsParse.begin(); i; i++ ) |
@@ -410,11 +412,12 @@ void Parser::printState( const Bu::String &sTag ) | |||
410 | } | 412 | } |
411 | Bu::sio << Bu::sio.nl; | 413 | Bu::sio << Bu::sio.nl; |
412 | Bu::sio << sTag << " script:"; | 414 | Bu::sio << sTag << " script:"; |
413 | for( TokenStack::iterator i = tsScript.begin(); i; i++ ) | 415 | for( TokenStack::iterator i = pCurExp->begin(); i; i++ ) |
414 | { | 416 | { |
415 | Bu::sio << " " << (*i); | 417 | Bu::sio << " " << (*i); |
416 | } | 418 | } |
417 | Bu::sio << Bu::sio.nl; | 419 | Bu::sio << Bu::sio.nl; |
418 | Bu::sio << "----------------" << Bu::sio.nl; | 420 | Bu::sio << "----------------" << Bu::sio.nl; |
421 | */ | ||
419 | } | 422 | } |
420 | 423 | ||