Add floordiv, mod and pi
This commit is contained in:
parent
a2d9109ae8
commit
0457e29e6f
|
@ -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'
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
12
lexer.py
12
lexer.py
|
@ -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():
|
||||
|
|
Loading…
Reference in New Issue