summaryrefslogtreecommitdiff
path: root/src/parser.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2016-11-30 14:52:39 -0700
committerMike Buland <eichlan@xagasoft.com>2016-11-30 14:52:39 -0700
commit2297488eae424197dce4ed6b1dc50ae67c093074 (patch)
treec28956f193f9ee8bdde083f780984736d48a5e41 /src/parser.cpp
parent0321e6e39b8cf24718cf853c28f0f35443753264 (diff)
downloadclic-2297488eae424197dce4ed6b1dc50ae67c093074.tar.gz
clic-2297488eae424197dce4ed6b1dc50ae67c093074.tar.bz2
clic-2297488eae424197dce4ed6b1dc50ae67c093074.tar.xz
clic-2297488eae424197dce4ed6b1dc50ae67c093074.zip
I managed to confuse myself pretty well.
I'm doing some reading up on LR(n) parsers, I just sort of started without thinking about it this time, not a great approach. I feel like it can't hurt to have an update on how this works anyway. I think the idea was solid, but I was trying to do too much at once. One question is what my goal should be for this. I could just solve the equation as we go, or I could generate code that will let us solve the equation. The later is obviously attractive in that it will let us run an expression more than once, and maybe even define functions. I like that.
Diffstat (limited to 'src/parser.cpp')
-rw-r--r--src/parser.cpp34
1 files changed, 31 insertions, 3 deletions
diff --git a/src/parser.cpp b/src/parser.cpp
index 1f9a193..0131ba3 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -23,25 +23,53 @@ void Parser::parse()
23 for(;;) 23 for(;;)
24 { 24 {
25 lex.nextToken(); 25 lex.nextToken();
26 if( lex[0].eType == Token::tEndOfInput )
27 break;
28
26 expr(); 29 expr();
27 30
31 Bu::sio << "Final stack:";
28 for( TokenStack::iterator i = tsStack.begin(); i; i++ ) 32 for( TokenStack::iterator i = tsStack.begin(); i; i++ )
29 { 33 {
30 Bu::sio << *i << " "; 34 Bu::sio << " " << (*i).eType;
35 if( (*i).eType == Token::tNumber )
36 {
37 Bu::sio << "(" << (*i).nVal->toString() << ")";
38 }
31 } 39 }
40 Bu::sio << Bu::sio.nl;
32 } 41 }
33} 42}
34 43
35void Parser::expr() 44void Parser::expr()
36{ 45{
46 if( lex[0].eType == Token::tEndOfInput )
47 return;
37 if( lex[0].eType == Token::tVariable && 48 if( lex[0].eType == Token::tVariable &&
38 lex[1].eType == Token::tEquals ) 49 lex[1].eType == Token::tEquals )
39 { 50 {
40 // Assignment! 51 Token t = lex[0];
52 lex.nextToken();
53 lex.nextToken();
41 expr(); 54 expr();
55
42 return; 56 return;
43 } 57 }
44 exprP(); 58 exprP();
59
60 switch( lex[0].eType )
61 {
62 case Token::tPlus:
63 case Token::tMinus:
64 case Token::tMultiply:
65 case Token::tDivide:
66 case Token::tModulus:
67 Token t = lex[0];
68 lex.nextToken();
69 expr();
70 tsStack.push( t );
71 break;
72 }
45} 73}
46 74
47void Parser::exprP() 75void Parser::exprP()
@@ -222,7 +250,7 @@ void Parser::setVariable( const Bu::String &sName, const Number &rValue )
222 hVars.insert( sName, rValue ); 250 hVars.insert( sName, rValue );
223} 251}
224 252
225void Parser::unwind() 253void Parser::reduce()
226{ 254{
227 /* 255 /*
228 for(;;) 256 for(;;)