Add floordiv, mod and pi
This commit is contained in:
parent
a2d9109ae8
commit
0457e29e6f
|
@ -13,6 +13,8 @@ PLUS = 'PLUS'
|
||||||
MINUS = 'MINUS'
|
MINUS = 'MINUS'
|
||||||
MUL = 'MUL'
|
MUL = 'MUL'
|
||||||
DIV = 'DIV'
|
DIV = 'DIV'
|
||||||
|
IDIV = 'IDIV'
|
||||||
|
MOD = 'MOD'
|
||||||
POW = 'POW'
|
POW = 'POW'
|
||||||
SQRT = 'SQRT'
|
SQRT = 'SQRT'
|
||||||
|
|
||||||
|
@ -20,6 +22,8 @@ HEX = 'HEX'
|
||||||
DEC = 'DEC'
|
DEC = 'DEC'
|
||||||
BIN = 'BIN'
|
BIN = 'BIN'
|
||||||
|
|
||||||
|
PI = 'PI'
|
||||||
|
|
||||||
SWAP = 'SWAP'
|
SWAP = 'SWAP'
|
||||||
POP = 'POP'
|
POP = 'POP'
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
|
|
||||||
from constants import (
|
from constants import (
|
||||||
NUMBER, EOF,
|
NUMBER, EOF,
|
||||||
PLUS, MINUS, MUL, DIV, POW, SQRT,
|
PLUS, MINUS, MUL, DIV, IDIV, MOD, POW, SQRT,
|
||||||
HEX, DEC, BIN,
|
HEX, DEC, BIN,
|
||||||
SWAP, POP,
|
SWAP, POP,
|
||||||
)
|
)
|
||||||
|
@ -53,7 +53,7 @@ class Interpreter:
|
||||||
while self.current_token.type is not EOF:
|
while self.current_token.type is not EOF:
|
||||||
if self.current_token.type == NUMBER:
|
if self.current_token.type == NUMBER:
|
||||||
self.stack.append(self.current_token.value)
|
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)
|
self.evaluate_binary_operator(self.current_token)
|
||||||
elif self.current_token.type in (SQRT):
|
elif self.current_token.type in (SQRT):
|
||||||
self.evaluate_unary_operator(self.current_token)
|
self.evaluate_unary_operator(self.current_token)
|
||||||
|
|
12
lexer.py
12
lexer.py
|
@ -12,8 +12,9 @@ import operator
|
||||||
from token import Token
|
from token import Token
|
||||||
from constants import (
|
from constants import (
|
||||||
NUMBER, EOF,
|
NUMBER, EOF,
|
||||||
PLUS, MINUS, MUL, DIV, POW, SQRT,
|
PLUS, MINUS, MUL, DIV, IDIV, MOD, POW, SQRT,
|
||||||
HEX, DEC, BIN,
|
HEX, DEC, BIN,
|
||||||
|
PI,
|
||||||
SWAP, POP,
|
SWAP, POP,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -73,8 +74,15 @@ class Lexer():
|
||||||
|
|
||||||
elif self.current_char == '/':
|
elif self.current_char == '/':
|
||||||
self.advance()
|
self.advance()
|
||||||
|
if self.current_char == '/':
|
||||||
|
self.advance()
|
||||||
|
return Token(IDIV, operator.floordiv)
|
||||||
return Token(DIV, operator.truediv)
|
return Token(DIV, operator.truediv)
|
||||||
|
|
||||||
|
elif self.current_char == '%':
|
||||||
|
self.advance()
|
||||||
|
return Token(MOD, operator.mod)
|
||||||
|
|
||||||
elif self.current_char == '^':
|
elif self.current_char == '^':
|
||||||
self.advance()
|
self.advance()
|
||||||
return Token(POW, operator.pow)
|
return Token(POW, operator.pow)
|
||||||
|
@ -94,6 +102,8 @@ class Lexer():
|
||||||
return Token(BIN, bin)
|
return Token(BIN, bin)
|
||||||
elif check.lower() == SQRT.lower():
|
elif check.lower() == SQRT.lower():
|
||||||
return Token(SQRT, math.sqrt)
|
return Token(SQRT, math.sqrt)
|
||||||
|
elif check.lower() == PI.lower():
|
||||||
|
return Token(NUMBER, math.pi)
|
||||||
elif check.lower() == SWAP.lower():
|
elif check.lower() == SWAP.lower():
|
||||||
return Token(SWAP, text)
|
return Token(SWAP, text)
|
||||||
elif check.lower() == POP.lower():
|
elif check.lower() == POP.lower():
|
||||||
|
|
Loading…
Reference in New Issue