From 4bd16214f0c6d2b4079b01d68b7e36ad408406d4 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 23 Apr 2013 13:15:06 -0600 Subject: Now supports modulus (strange for fractions). --- src/lexer.cpp | 5 +++++ src/parser.cpp | 14 ++++++++++++++ src/token.cpp | 1 + src/token.h | 1 + 4 files changed, 21 insertions(+) (limited to 'src') diff --git a/src/lexer.cpp b/src/lexer.cpp index 46686c8..2521b40 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -70,6 +70,7 @@ Token Lexer::nextToken() sBuf[iBufPos] != ' ' && sBuf[iBufPos] != '\t' && sBuf[iBufPos] != '+' && sBuf[iBufPos] != '-' && sBuf[iBufPos] != '*' && sBuf[iBufPos] != '/' && + sBuf[iBufPos] != '%' && sBuf[iBufPos] != '(' && sBuf[iBufPos] != ')'; iBufPos++ ) { @@ -99,6 +100,10 @@ Token Lexer::nextToken() iBufPos++; return Token( Token::tDivide ); + case '%': + iBufPos++; + return Token( Token::tModulus ); + case '(': iBufPos++; return Token( Token::tOpenParen ); diff --git a/src/parser.cpp b/src/parser.cpp index 5bb0aa6..df9e869 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -239,6 +239,18 @@ void Parser::unwind() ); } break; + + case Token::tModulus: + { + Token b = tsTerminal.peekPop(); + Token a = tsTerminal.peekPop(); + tsTerminal.push( + Token( Token::tNumber, + new Number( deref(a) % deref(b) ) + ) + ); + } + break; case Token::tOpenParen: tsNonTerminal.push( t ); @@ -296,6 +308,7 @@ int Parser::reqTokens( Token::Type eType ) case Token::tMinus: case Token::tDivide: case Token::tMultiply: + case Token::tModulus: case Token::tEquals: return 2; @@ -326,6 +339,7 @@ int Parser::getPriority( Token::Type eType ) case Token::tDivide: case Token::tMultiply: + case Token::tModulus: return 2; case Token::tOpenParen: diff --git a/src/token.cpp b/src/token.cpp index 019e54d..5b0e6fe 100644 --- a/src/token.cpp +++ b/src/token.cpp @@ -59,6 +59,7 @@ Bu::Formatter &operator<<( Bu::Formatter &f, Token::Type eType ) case Token::tMinus: return f << "-"; case Token::tDivide: return f << "/"; case Token::tMultiply: return f << "*"; + case Token::tModulus: return f << "%"; case Token::tOpenParen: return f << "("; case Token::tCloseParen: return f << ")"; case Token::tEquals: return f << "="; diff --git a/src/token.h b/src/token.h index 05a610a..1cc6516 100644 --- a/src/token.h +++ b/src/token.h @@ -20,6 +20,7 @@ public: tMinus, tDivide, tMultiply, + tModulus, tOpenParen, tCloseParen, tEquals, -- cgit v1.2.3