Skip to content

Commit 8784304

Browse files
committed
Check special matrix functions arguments
1 parent 1b30d68 commit 8784304

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

semantic_listener.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ class SemanticListener(MyParserListener):
1515
"""Checks break and continue statements, variable declarations,types and assignments."""
1616

1717
def __init__(self):
18-
# super().__init__()
1918
self.nested_loop_counter = 0
2019
self.variables: dict[str, Type | None] = {}
2120
self.expr_type: dict[
@@ -114,11 +113,24 @@ def exitMinusExpression(self, ctx: MyParser.MinusExpressionContext):
114113
def exitSingleExpression(self, ctx: MyParser.SingleExpressionContext):
115114
self.expr_type[ctx] = self.expr_type[ctx.getChild(0)]
116115

117-
def enterSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext):
118-
pass
119-
120116
def exitSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext):
121-
pass
117+
dimentions = ctx.children[2::2]
118+
for dim in dimentions:
119+
if self.expr_type[dim] != Type.INT:
120+
ctx.parser.notifyErrorListeners(
121+
"Matrix dimentions must be integers", ctx.getChild(0).getSymbol()
122+
)
123+
self.expr_type[ctx] = None
124+
break
125+
type_dimentions = []
126+
for dim in dimentions:
127+
if isinstance(dim, MyParser.SingleExpressionContext) and isinstance(
128+
dim.getChild(0), MyParser.IntContext
129+
):
130+
type_dimentions.append(int(dim.getText()))
131+
else:
132+
type_dimentions.append(None)
133+
self.expr_type[ctx] = (Type.INT, *type_dimentions)
122134

123135
def exitVector(self, ctx: MyParser.VectorContext):
124136
elements = ctx.children[1::2]

test_main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,11 @@ def test_sem_error_transpose():
7878
assert "line 7" in result.stdout
7979
assert "transpose" in result.stdout.lower()
8080
assert result.stdout.count("line") == 1
81+
82+
83+
def test_sem_error_special_matrix():
84+
result = runner.invoke(app, ["sem", "tests/semantic/input_special_matrix.txt"])
85+
assert result.exit_code == 0
86+
assert "line 1" in result.stdout
87+
assert "line 11" in result.stdout
88+
assert result.stdout.count("line") == 2
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
A = ones(2.0);
2+
3+
B = ones(1, 1, 1);
4+
5+
n = 10;
6+
7+
C = zeros(2, n);
8+
9+
m = 3.14;
10+
11+
D = eye(m);

0 commit comments

Comments
 (0)