Skip to content

Commit e3cf67e

Browse files
committed
Add support for parallel assignment of other operators
e.g., assignment to mutable hash tables and lists and installing methods
1 parent 857584d commit e3cf67e

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

M2/Macaulay2/d/binding.d

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ bindParallelAssignmentItem(e:ParseTree,dictionary:Dictionary,colon:bool):void :=
678678
if token.word.typecode != TCid then makeErrorTree(token,"syntax error: parallel assignment expected symbol")
679679
else bindToken(token,dictionary,colon);
680680
)
681-
else makeErrorTree(e,"syntax error: parallel assignment expected symbol"));
681+
else bind(e, dictionary));
682682
bindParallelAssignmentList(e:ParseTree,dictionary:Dictionary,colon:bool):void := (
683683
when e
684684
is binary:Binary do (
@@ -688,7 +688,7 @@ bindParallelAssignmentList(e:ParseTree,dictionary:Dictionary,colon:bool):void :=
688688
bindop(binary.Operator,dictionary);
689689
bindParallelAssignmentItem(binary.rhs,dictionary,colon);
690690
)
691-
else makeErrorTree(e,"syntax error: parallel assignment expected symbol list")
691+
else bind(e, dictionary)
692692
)
693693
else bindParallelAssignmentItem(e,dictionary,colon));
694694
bindassignment(assn:Binary,dictionary:Dictionary,colon:bool):void := (
@@ -699,6 +699,7 @@ bindassignment(assn:Binary,dictionary:Dictionary,colon:bool):void := (
699699
bindParallelAssignmentList(p.contents,dictionary,colon);
700700
bind(body,dictionary);
701701
)
702+
is p:EmptyParentheses do bind(body,dictionary)
702703
is token:Token do (
703704
if token.word.typecode != TCid then (
704705
makeErrorTree(assn.Operator, "expected a symbol to left of '"+assn.Operator.entry.word.name+"'");

M2/Macaulay2/d/convertr.d

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@ export convert0(e:ParseTree):Code := (
167167
is t:Token do convertTokenAssignment(t, b.rhs)
168168
-- e.g. (x,y,z) = (...)
169169
is p:Parentheses do convertParallelAssignment(false, p, b.rhs)
170+
is p:EmptyParentheses do Code(parallelAssignmentCode(
171+
false,
172+
CodeSequence(),
173+
convert(b.rhs),
174+
combinePositionR(p.left.position, treePosition(b.rhs))))
170175
-- Note: usable, but not used anywhere yet
171176
is u:Unary do convertUnaryInstallCode(UnaryInstallValueFun,
172177
convertGlobalOperator(u.Operator), convert(u.rhs), convert(b.rhs), pos)
@@ -203,6 +208,11 @@ export convert0(e:ParseTree):Code := (
203208
is t:Token do convertTokenAssignment(t, b.rhs)
204209
-- e.g. (x,y,z) := (...)
205210
is p:Parentheses do convertParallelAssignment(true, p, b.rhs)
211+
is p:EmptyParentheses do Code(parallelAssignmentCode(
212+
true,
213+
CodeSequence(),
214+
convert(b.rhs),
215+
combinePositionR(p.left.position, treePosition(b.rhs))))
206216
-- e.g. - Matrix := ...
207217
-- TODO: can #T be implemented here?
208218
is u:Unary do convertUnaryInstallCode(UnaryInstallMethodFun,

M2/Macaulay2/d/evaluate.d

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,8 @@ globalAssignmentFun(x:globalAssignmentCode):Expr := (
11951195
11961196
ParallelAssignmentError(n:int):Expr := buildErrorPacket(
11971197
"parallel assignment: expected a sequence of " + tostring(n) + " values");
1198+
ParallelAssignmentErrorAt(n:int):Expr := buildErrorPacket(
1199+
"parallel assignment: failure at argument " + tostring(n));
11981200
parallelAssignmentFun(x:parallelAssignmentCode):Expr := (
11991201
nlhs := length(x.lhs);
12001202
value := eval(x.rhs);
@@ -1203,16 +1205,77 @@ parallelAssignmentFun(x:parallelAssignmentCode):Expr := (
12031205
if nlhs == 1 then value = seq(value);
12041206
when value
12051207
is values:Sequence do (
1206-
if nlhs == length(values)
1207-
then Expr(new Sequence len nlhs at i do provide (
1208-
when x.lhs.i
1209-
is y:globalMemoryReferenceCode do (
1210-
globalAssignment(y.var.frameindex, y.var, values.i))
1211-
is y:localMemoryReferenceCode do (
1212-
localAssignment(y.nestingDepth, y.frameindex, values.i))
1213-
is y:threadMemoryReferenceCode do (
1214-
globalAssignment(y.var.frameindex, y.var, values.i))
1215-
else nullE)) -- TODO: handle other cases
1208+
if nlhs == length(values) then (
1209+
pos := codePosition(x.rhs);
1210+
e := nullE;
1211+
result := new Sequence len nlhs at i do provide (
1212+
when e is Error do nullE
1213+
else (
1214+
c := Code(evaluatedCode(values.i, pos));
1215+
r := when x.lhs.i
1216+
is y:globalMemoryReferenceCode do (
1217+
globalAssignment(y.var.frameindex, y.var, values.i))
1218+
is y:localMemoryReferenceCode do (
1219+
localAssignment(y.nestingDepth, y.frameindex, values.i))
1220+
is y:threadMemoryReferenceCode do (
1221+
globalAssignment(y.var.frameindex, y.var, values.i))
1222+
is y:binaryCode do (
1223+
codeseq := CodeSequence(
1224+
convertGlobalOperator(y.oper), y.lhs, y.rhs, c);
1225+
if x.colon then InstallMethodFun(codeseq)
1226+
else (
1227+
if y.oper == DotS.symbol || y.oper == SharpS.symbol
1228+
then AssignElemFun(y.lhs, y.rhs, c)
1229+
else InstallValueFun(codeseq)))
1230+
is y:adjacentCode do (
1231+
codeseq := CodeSequence(
1232+
convertGlobalOperator(AdjacentS.symbol),
1233+
y.lhs, y.rhs, c);
1234+
if x.colon then InstallMethodFun(codeseq)
1235+
else InstallValueFun(codeseq))
1236+
is y:unaryCode do (
1237+
oper := convertGlobalOperator(y.oper);
1238+
if x.colon then UnaryInstallMethodFun(oper, y.rhs, c)
1239+
else UnaryInstallValueFun(oper, y.rhs, c))
1240+
is y:sequenceCode do (
1241+
parallelAssignmentFun(parallelAssignmentCode(x.colon,
1242+
y.x, c, y.position)))
1243+
is y:listCode do (
1244+
parallelAssignmentFun(parallelAssignmentCode(x.colon,
1245+
y.y, c, y.position)))
1246+
is y:arrayCode do (
1247+
parallelAssignmentFun(parallelAssignmentCode(x.colon,
1248+
y.z, c, y.position)))
1249+
is y:angleBarListCode do (
1250+
parallelAssignmentFun(parallelAssignmentCode(x.colon,
1251+
y.t, c, y.position)))
1252+
is y:newCode do (
1253+
if x.colon
1254+
then AssignNewFun(y.newClause, c)
1255+
else ParallelAssignmentErrorAt(i + 1))
1256+
is y:newOfCode do (
1257+
if x.colon
1258+
then AssignNewOfFun(y.newClause, y.ofClause, c)
1259+
else ParallelAssignmentErrorAt(i + 1))
1260+
is y:newFromCode do (
1261+
if x.colon
1262+
then AssignNewFromFun(y.newClause, y.fromClause, c)
1263+
else ParallelAssignmentErrorAt(i + 1))
1264+
is y:newOfFromCode do (
1265+
if x.colon
1266+
then AssignNewOfFromFun(CodeSequence(
1267+
y.newClause, y.ofClause, y.fromClause, c))
1268+
else ParallelAssignmentErrorAt(i + 1))
1269+
is nullCode do nullE
1270+
else ParallelAssignmentErrorAt(i + 1);
1271+
when r
1272+
is Error do (
1273+
e = r;
1274+
nullE)
1275+
else r));
1276+
when e
1277+
is Error do e
1278+
else if nlhs == 1 then result.0 else Expr(result))
12161279
else ParallelAssignmentError(nlhs))
12171280
else ParallelAssignmentError(nlhs));
12181281

0 commit comments

Comments
 (0)