Skip to content

Floor division operator #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions scrapscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ class BinopKind(enum.Enum):
SUB = auto()
MUL = auto()
DIV = auto()
FLOOR_DIV = auto()
EXP = auto()
MOD = auto()
EQUAL = auto()
Expand All @@ -653,6 +654,7 @@ def from_str(cls, x: str) -> "BinopKind":
"-": cls.SUB,
"*": cls.MUL,
"/": cls.DIV,
"//": cls.FLOOR_DIV,
"^": cls.EXP,
"%": cls.MOD,
"==": cls.EQUAL,
Expand Down Expand Up @@ -1010,6 +1012,7 @@ def make_bool(x: bool) -> Object:
BinopKind.SUB: lambda env, x, y: Int(eval_int(env, x) - eval_int(env, y)),
BinopKind.MUL: lambda env, x, y: Int(eval_int(env, x) * eval_int(env, y)),
BinopKind.DIV: lambda env, x, y: Int(eval_int(env, x) // eval_int(env, y)),
BinopKind.FLOOR_DIV: lambda env, x, y: Int(eval_int(env, x) // eval_int(env, y)),
BinopKind.EXP: lambda env, x, y: Int(eval_int(env, x) ** eval_int(env, y)),
BinopKind.MOD: lambda env, x, y: Int(eval_int(env, x) % eval_int(env, y)),
BinopKind.EQUAL: lambda env, x, y: make_bool(eval_exp(env, x) == eval_exp(env, y)),
Expand Down Expand Up @@ -2421,6 +2424,10 @@ def test_eval_with_binop_div(self) -> None:
exp = Binop(BinopKind.DIV, Int(2), Int(3))
self.assertEqual(eval_exp({}, exp), Int(0))

def test_eval_with_binop_floor_div(self) -> None:
exp = Binop(BinopKind.FLOOR_DIV, Int(2), Int(3))
self.assertEqual(eval_exp({}, exp), Int(0))

def test_eval_with_binop_exp(self) -> None:
exp = Binop(BinopKind.EXP, Int(2), Int(3))
self.assertEqual(eval_exp({}, exp), Int(8))
Expand Down