Skip to content

Commit 1a06974

Browse files
committed
Introduce EXPR_ELM_MAT_LEV and simplify EXPR_ELMS_LIST_LEV
Since 2e850b0, at most two indices in "[]" are allowed. Handling the two possible cases explicitly allows to simplify some parts of the code and makes one's life easier when manipulating syntax trees. However, this obviously is a breaking change.
1 parent 0b5bf02 commit 1a06974

File tree

4 files changed

+77
-21
lines changed

4 files changed

+77
-21
lines changed

src/code.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2450,8 +2450,13 @@ void CodeElmListLevel(CodeState * cs, Int narg, UInt level)
24502450
{
24512451
Expr ref; // reference, result
24522452

2453+
GAP_ASSERT(narg == 1 || narg == 2);
2454+
24532455
// allocate the reference and enter the level
2454-
ref = NewExpr(cs, EXPR_ELM_LIST_LEV, (narg + 2) * sizeof(Expr));
2456+
if (narg == 1)
2457+
ref = NewExpr(cs, EXPR_ELM_LIST_LEV, 3 * sizeof(Expr));
2458+
else // if (narg == 2)
2459+
ref = NewExpr(cs, EXPR_ELM_MAT_LEV, 4 * sizeof(Expr));
24552460
WRITE_EXPR(cs, ref, narg + 1, level);
24562461

24572462
// let 'CodeElmListUniv' do the rest

src/code.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ enum EXPR_TNUM {
534534
EXPR_ELM_MAT,
535535
EXPR_ELMS_LIST,
536536
EXPR_ELM_LIST_LEV,
537+
EXPR_ELM_MAT_LEV,
537538
EXPR_ELMS_LIST_LEV,
538539
EXPR_ISB_LIST,
539540

src/syntaxtree.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,14 @@ static const CompilerT Compilers[] = {
904904
COMPILER_(
905905
EXPR_ELM_MAT, ARG_EXPR_("list"), ARG_EXPR_("row"), ARG_EXPR_("col")),
906906
COMPILER_(EXPR_ELMS_LIST, ARG_EXPR_("list"), ARG_EXPR_("poss")),
907+
COMPILER_(EXPR_ELM_MAT_LEV,
908+
ARG_EXPR_("matrices"),
909+
ARG_EXPR_("row"),
910+
ARG_EXPR_("col"),
911+
ARG_EXPR("level", ObjInt_UInt, SyntaxTreeCodeObjInt)),
907912
COMPILER_(EXPR_ELM_LIST_LEV,
908913
ARG_EXPR_("lists"),
909-
ARGS_EXPR("pos"),
914+
ARG_EXPR_("pos"),
910915
ARG_EXPR("level", ObjInt_UInt, SyntaxTreeCodeObjInt)),
911916
COMPILER_(EXPR_ELMS_LIST_LEV,
912917
ARG_EXPR_("lists"),

src/vars.c

Lines changed: 64 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -825,24 +825,19 @@ static Obj EvalElmListLevel(Expr expr)
825825
{
826826
Obj lists; // lists, left operand
827827
Obj pos; // position, right operand
828-
Obj ixs;
829828
UInt level; // level
830-
Int narg;
831-
Int i;
829+
Obj ixs;
832830

833831
// evaluate lists (if this works, then <lists> is nested <level> deep,
834832
// checking it is nested <level>+1 deep is done by 'ElmListLevel')
835833
lists = EVAL_EXPR(READ_EXPR(expr, 0));
836-
narg = SIZE_EXPR(expr)/sizeof(Expr) -2;
837-
ixs = NEW_PLIST(T_PLIST, narg);
838-
for (i = 1; i <= narg; i++) {
839-
pos = EVAL_EXPR( READ_EXPR(expr, i));
840-
SET_ELM_PLIST(ixs, i, pos);
841-
CHANGED_BAG(ixs);
842-
}
843-
SET_LEN_PLIST(ixs, narg);
844-
// get the level
845-
level = READ_EXPR(expr, narg + 1);
834+
pos = EVAL_EXPR(READ_EXPR(expr, 1));
835+
level = READ_EXPR(expr, 2);
836+
837+
ixs = NEW_PLIST(T_PLIST, 1);
838+
SET_ELM_PLIST(ixs, 1, pos);
839+
CHANGED_BAG(ixs);
840+
SET_LEN_PLIST(ixs, 1);
846841

847842
// select the elements from several lists (store them in <lists>)
848843
ElmListLevel( lists, ixs, level );
@@ -852,6 +847,49 @@ static Obj EvalElmListLevel(Expr expr)
852847
}
853848

854849

850+
/****************************************************************************
851+
**
852+
*F EvalElmMatLevel(<expr>) . . . . . . select elements of several matrices
853+
**
854+
** 'EvalElmMatLevel' evaluates the matrix element expression <expr> of the
855+
** form '<list>...{<positions>}...[<row>,<col>]', where there may actually
856+
** be several '{<positions>}' selections between <list> and '[<position>]'.
857+
** The number of those is called the level. 'EvalElmMatLevel' goes that
858+
** deep into the left operand and selects the element at <row>,<col> from
859+
** each of those matrices. For example, if the level is 1, the left operand
860+
** must be a list of matrices and 'EvalElmMatLevel' selects the element at
861+
** <row>,<col> from each of the matrices and returns the list of those
862+
** values.
863+
*/
864+
static Obj EvalElmMatLevel(Expr expr)
865+
{
866+
Obj matrices; // matrices
867+
Obj row; // row position
868+
Obj col; // column position
869+
UInt level; // level
870+
Obj ixs;
871+
872+
// evaluate matrices (if this works, then <matrices> is nested <level>
873+
// deep, checking it is nested <level>+1 deep is done by 'ElmListLevel')
874+
matrices = EVAL_EXPR(READ_EXPR(expr, 0));
875+
row = EVAL_EXPR(READ_EXPR(expr, 1));
876+
col = EVAL_EXPR(READ_EXPR(expr, 2));
877+
level = READ_EXPR(expr, 3);
878+
879+
ixs = NEW_PLIST(T_PLIST, 2);
880+
SET_ELM_PLIST(ixs, 1, row);
881+
SET_ELM_PLIST(ixs, 2, col);
882+
CHANGED_BAG(ixs);
883+
SET_LEN_PLIST(ixs, 2);
884+
885+
// select the elements from several matrices (store them in <matrices>)
886+
ElmListLevel( matrices, ixs, level );
887+
888+
// return the elements
889+
return matrices;
890+
}
891+
892+
855893
/****************************************************************************
856894
**
857895
*F EvalElmsListLevel(<expr>) . . . select several elements of several lists
@@ -1036,16 +1074,21 @@ static void PrintElmMat(Expr expr)
10361074

10371075
static void PrintElmListLevel(Expr expr)
10381076
{
1039-
Int i;
1040-
Int narg = SIZE_EXPR(expr)/sizeof(Expr) -2 ;
10411077
Pr("%2>", 0, 0);
10421078
PrintExpr(READ_EXPR(expr, 0));
10431079
Pr("%<[", 0, 0);
10441080
PrintExpr(READ_EXPR(expr, 1));
1045-
for (i = 2; i <= narg; i++) {
1046-
Pr("%<, %>", 0, 0);
1047-
PrintExpr(READ_EXPR(expr, i));
1048-
}
1081+
Pr("%<]", 0, 0);
1082+
}
1083+
1084+
static void PrintElmMatLevel(Expr expr)
1085+
{
1086+
Pr("%2>", 0, 0);
1087+
PrintExpr(READ_EXPR(expr, 0));
1088+
Pr("%<[", 0, 0);
1089+
PrintExpr(READ_EXPR(expr, 1));
1090+
Pr("%<, %>", 0, 0);
1091+
PrintExpr(READ_EXPR(expr, 2));
10491092
Pr("%<]", 0, 0);
10501093
}
10511094

@@ -2248,8 +2291,10 @@ static Int InitKernel (
22482291
// install executors, evaluators, and printers for matrix elements
22492292
InstallExecStatFunc(STAT_ASS_MAT, ExecAssMat);
22502293
InstallEvalExprFunc(EXPR_ELM_MAT, EvalElmMat);
2294+
InstallEvalExprFunc(EXPR_ELM_MAT_LEV, EvalElmMatLevel);
22512295
InstallPrintStatFunc(STAT_ASS_MAT, PrintAssMat);
22522296
InstallPrintExprFunc(EXPR_ELM_MAT, PrintElmMat);
2297+
InstallPrintExprFunc(EXPR_ELM_MAT_LEV, PrintElmMatLevel);
22532298

22542299
// install executors, evaluators, and printers for record elements
22552300
InstallExecStatFunc( STAT_ASS_REC_NAME , ExecAssRecName);

0 commit comments

Comments
 (0)