Skip to content

Commit de4b21d

Browse files
committed
Fix location report for record assignment and disallow reassignment
1 parent 9e5b799 commit de4b21d

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

grammar.y

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -204,26 +204,32 @@ Stmnt : EOL { $$ = new Empty; prev->link($$); prev = $$; }
204204
}
205205
}
206206
| ID ASSIGN ID LCURL ExpList RCURL EOL {
207-
if (!table->check($3).first || table->type($3) != ExpI::RecordT || !std::get<RecordT>(table->value($3)).second) {
208-
error(@1, "no such record definition: " + $3);
209-
YYERROR;
210-
}
211-
if ($5.size() != std::get<RecordT>(table->value($3)).first.size()) {
212-
error(@2, "wrong number of fields for record type: " + $3);
213-
YYERROR;
214-
}
215-
for (size_t i{ 0 }; auto& e : $5) {
216-
if (e->type() != table->fieldtype($3, i++)) {
217-
error(@2, "wrong type(s) of field value(s) for record type: " + $3);
207+
if (!table->check($1).first) {
208+
if (!table->check($3).first || table->type($3) != ExpI::RecordT || !std::get<RecordT>(table->value($3)).second) {
209+
error(@3, "no such record definition: " + $3);
218210
YYERROR;
219211
}
212+
if ($5.size() != std::get<RecordT>(table->value($3)).first.size()) {
213+
error(@5, "wrong number of fields for record type: " + $3);
214+
YYERROR;
215+
}
216+
for (size_t i{ 0 }; auto& e : $5) {
217+
if (e->type() != table->fieldtype($3, i++)) {
218+
error(@5, "wrong type(s) of field value(s) for record type: " + $3);
219+
YYERROR;
220+
}
221+
}
222+
RecordT rec = std::get<RecordT>(table->value($3));
223+
rec.second = false;
224+
table->store($1, ExpT{ rec });
225+
$$ = new RecordAssign($1, std::move($5), std::move(rec));
226+
prev->link($$);
227+
prev = $$;
228+
}
229+
else {
230+
error(@1, "variable previously assigned: " + $1);
231+
YYERROR;
220232
}
221-
RecordT rec = std::get<RecordT>(table->value($3));
222-
rec.second = false;
223-
table->store($1, ExpT{ rec });
224-
$$ = new RecordAssign($1, std::move($5), std::move(rec));
225-
prev->link($$);
226-
prev = $$;
227233
}
228234
| ID LBRAK Exp RBRAK ASSIGN Exp EOL {
229235
if ($3->type() != ExpI::IntT) {

0 commit comments

Comments
 (0)