Skip to content

Commit 0ecdfad

Browse files
committed
Handle unary minus
1 parent 6a225d5 commit 0ecdfad

File tree

4 files changed

+11
-4
lines changed

4 files changed

+11
-4
lines changed

interpreter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def visitTransposeExpression(self, ctx: MyParser.TransposeExpressionContext):
7171
return self.visitChildren(ctx) # todo
7272

7373
def visitMinusExpression(self, ctx: MyParser.MinusExpressionContext):
74-
return self.visitChildren(ctx) # todo
74+
return -self.visit(ctx.getChild(1))
7575

7676
def visitSingleExpression(self, ctx: MyParser.SingleExpressionContext):
7777
return self.visit(ctx.getChild(0))

test_main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ def test_sem_errors(name: str, line_numbers: list[int], additional: str):
6868
@pytest.mark.parametrize(
6969
"name,output",
7070
[
71-
("simple_math", [23, 35, 1, 1.0, 1]),
71+
("simple_math", [23, 35, 1, 1.0, 1, -2]),
7272
],
7373
)
7474
def test_interpreter(name: str, output: str):
7575
result = runner.invoke(app, ["run", f"tests/interpreter/{name}.txt"])
7676
assert result.exit_code == 0
77-
assert result.stdout.strip() == "\n".join(map(str, output))
77+
assert result.stdout == "\n".join(map(str, output)) + "\n"

tests/interpreter/simple_math.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
print 3 + 4 * 5, (3 + 4) * 5;
2-
print 2 / 2, 1.0 * 1.0, 2 - 1;
2+
print 2 / 2, 1.0 * 1.0, 2 - 1;
3+
print -1 * 2;

utils/values.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ def __truediv__(self, other):
2929
return Float(self.value / other.value)
3030
raise TypeError()
3131

32+
def __neg__(self):
33+
return Int(-self.value)
34+
3235

3336
class Float(Value):
3437
def __init__(self, value):
@@ -54,6 +57,9 @@ def __truediv__(self, other):
5457
return Float(self.value / other.value)
5558
raise TypeError()
5659

60+
def __neg__(self):
61+
return Float(-self.value)
62+
5763

5864
class String(Value):
5965
def __init__(self, value):

0 commit comments

Comments
 (0)