Skip to content
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

Fix printing of expressions including non-zero list levels #5453

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
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
40 changes: 37 additions & 3 deletions src/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,26 @@ static void PrintAsssList(Stat stat)
}


/****************************************************************************
**
*F ExprHasNonZeroListLevel(<expr>) . . . . . . . . . . . . . . . . . . . . .
**
** Every 'EXPR_ELMS_LIST' or 'EXPR_ELMS_LIST_LEV' increments the list level.
** 'EXPR_ELM_LIST_LEV' has a non-zero list level.
** Every other expression should have level 0.
**
** If a list access happens at level zero ('EXPR_ELM_LIST', 'EXPR_ELM_MAT'
** and 'EXPR_ELMS_LIST') but the level of the list is non-zero, the list
** must be put into parentheses during printing to reset the level to 0.
*/
static BOOL ExprHasNonZeroListLevel(Expr list)
{
return TNUM_EXPR(list) == EXPR_ELMS_LIST ||
TNUM_EXPR(list) == EXPR_ELM_LIST_LEV ||
TNUM_EXPR(list) == EXPR_ELMS_LIST_LEV;
}


/****************************************************************************
**
*F PrintElmList(<expr>) . . . . . print a selection of an element of a list
Expand All @@ -1016,17 +1036,31 @@ static void PrintAsssList(Stat stat)
*/
static void PrintElmList(Expr expr)
{
Expr list = READ_EXPR(expr, 0);
Pr("%2>", 0, 0);
PrintExpr(READ_EXPR(expr, 0));
if (ExprHasNonZeroListLevel(list)) {
Pr("(", 0, 0);
PrintExpr(list);
Pr(")", 0, 0);
} else {
PrintExpr(list);
}
Pr("%<[", 0, 0);
PrintExpr(READ_EXPR(expr, 1));
Pr("%<]", 0, 0);
}

static void PrintElmMat(Expr expr)
{
Expr matrix = READ_EXPR(expr, 0);
Pr("%2>", 0, 0);
PrintExpr(READ_EXPR(expr, 0));
if (ExprHasNonZeroListLevel(matrix)) {
Pr("(", 0, 0);
PrintExpr(matrix);
Pr(")", 0, 0);
} else {
PrintExpr(matrix);
}
Pr("%<[", 0, 0);
PrintExpr(READ_EXPR(expr, 1));
Pr("%<, %>", 0, 0);
Expand Down Expand Up @@ -1081,7 +1115,7 @@ static void PrintElmsList(Expr expr)
{
Expr list = READ_EXPR(expr, 0);
Pr("%2>", 0, 0);
if (TNUM_EXPR(list) == EXPR_ELMS_LIST || TNUM_EXPR(list) == EXPR_ELMS_LIST_LEV) {
if (ExprHasNonZeroListLevel(list)) {
Pr("(", 0, 0);
PrintExpr(list);
Pr(")", 0, 0);
Expand Down
26 changes: 26 additions & 0 deletions tst/testinstall/function.tst
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,30 @@ function ( x ) return ([ [ x ] ]{[ 1 ]}){[ 1 ]}{[ 1 ]}; end
# four extractions
gap> funcloop(x -> ([ [ x ] ]{[ 1 ]}{[ 1 ]}){[ 1 ]}{[ 1 ]});
function ( x ) return ([ [ x ] ]{[ 1 ]}{[ 1 ]}){[ 1 ]}{[ 1 ]}; end

# list access at level 0 after EXPR_ELMS_LIST
gap> funcloop(x -> ([ x ]{[ 1 ]})[1]); # EXPR_ELM_LIST
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably also want to test the unbracketed case -- unless that is already somewhere else and I missed it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unbracketed case [ x ]{[ 1 ]}[1] is an expression of type EXPR_ELM_LIST_LEV and thus uses a code path which I have not touched at all. The "other" case is the default case L[i] which I expect to be sufficiently covered. That's why I have not added further tests. But of course I could add further tests if you prefer.

function ( x ) return ([ x ]{[ 1 ]})[1]; end
gap> funcloop(x -> ([ [ x ] ]{[ 1 ]})[1, 1]); # EXPR_ELM_MAT
function ( x ) return ([ [ x ] ]{[ 1 ]})[1, 1]; end
gap> funcloop(x -> ([ x ]{[ 1 ]}){[ 1 ]}); # EXPR_ELMS_LIST
function ( x ) return ([ x ]{[ 1 ]}){[ 1 ]}; end

# list access at level 0 after EXPR_ELM_LEV
gap> funcloop(x -> ([ [ x ] ]{[ 1 ]}[1])[1]); # EXPR_ELM_LIST
function ( x ) return ([ [ x ] ]{[ 1 ]}[1])[1]; end
gap> funcloop(x -> ([ [ [ x ] ] ]{[ 1 ]}[1])[1, 1]); # EXPR_ELM_MAT
function ( x ) return ([ [ [ x ] ] ]{[ 1 ]}[1])[1, 1]; end
gap> funcloop(x -> ([ [ x ] ]{[ 1 ]}[1]){[ 1 ]}); # EXPR_ELMS_LIST
function ( x ) return ([ [ x ] ]{[ 1 ]}[1]){[ 1 ]}; end

# list access at level 0 after EXPR_ELMS_LEV
gap> funcloop(x -> ([ [ x ] ]{[ 1 ]}{[ 1 ]})[1]); # EXPR_ELM_LIST
function ( x ) return ([ [ x ] ]{[ 1 ]}{[ 1 ]})[1]; end
gap> funcloop(x -> ([ [ x ] ]{[ 1 ]}{[ 1 ]})[1, 1]); # EXPR_ELM_MAT
function ( x ) return ([ [ x ] ]{[ 1 ]}{[ 1 ]})[1, 1]; end
gap> funcloop(x -> ([ [ x ] ]{[ 1 ]}{[ 1 ]}){[ 1 ]}); # EXPR_ELMS_LIST
function ( x ) return ([ [ x ] ]{[ 1 ]}{[ 1 ]}){[ 1 ]}; end

#
gap> STOP_TEST("function.tst", 1);