Add floordiv, mod and pi

This commit is contained in:
Pavle Portic 2019-04-26 16:02:24 +02:00
parent a2d9109ae8
commit 0457e29e6f
Signed by: TheEdgeOfRage
GPG Key ID: 6758ACE46AA2A849
3 changed files with 17 additions and 3 deletions

View File

@ -13,6 +13,8 @@ PLUS = 'PLUS'
MINUS = 'MINUS'
MUL = 'MUL'
DIV = 'DIV'
IDIV = 'IDIV'
MOD = 'MOD'
POW = 'POW'
SQRT = 'SQRT'
@ -20,6 +22,8 @@ HEX = 'HEX'
DEC = 'DEC'
BIN = 'BIN'
PI = 'PI'
SWAP = 'SWAP'
POP = 'POP'

View File

@ -8,7 +8,7 @@
from constants import (
NUMBER, EOF,
PLUS, MINUS, MUL, DIV, POW, SQRT,
PLUS, MINUS, MUL, DIV, IDIV, MOD, POW, SQRT,
HEX, DEC, BIN,
SWAP, POP,
)
@ -53,7 +53,7 @@ class Interpreter:
while self.current_token.type is not EOF:
if self.current_token.type == NUMBER:
self.stack.append(self.current_token.value)
elif self.current_token.type in (PLUS, MINUS, MUL, DIV, POW):
elif self.current_token.type in (PLUS, MINUS, MUL, DIV, IDIV, MOD, POW):
self.evaluate_binary_operator(self.current_token)
elif self.current_token.type in (SQRT):
self.evaluate_unary_operator(self.current_token)

View File

@ -12,8 +12,9 @@ import operator
from token import Token
from constants import (
NUMBER, EOF,
PLUS, MINUS, MUL, DIV, POW, SQRT,
PLUS, MINUS, MUL, DIV, IDIV, MOD, POW, SQRT,
HEX, DEC, BIN,
PI,
SWAP, POP,
)
@ -73,8 +74,15 @@ class Lexer():
elif self.current_char == '/':
self.advance()
if self.current_char == '/':
self.advance()
return Token(IDIV, operator.floordiv)
return Token(DIV, operator.truediv)
elif self.current_char == '%':
self.advance()
return Token(MOD, operator.mod)
elif self.current_char == '^':
self.advance()
return Token(POW, operator.pow)
@ -94,6 +102,8 @@ class Lexer():
return Token(BIN, bin)
elif check.lower() == SQRT.lower():
return Token(SQRT, math.sqrt)
elif check.lower() == PI.lower():
return Token(NUMBER, math.pi)
elif check.lower() == SWAP.lower():
return Token(SWAP, text)
elif check.lower() == POP.lower():