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

Added try .. then .. clause #3375

Merged
merged 2 commits into from
Aug 7, 2024
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
4 changes: 4 additions & 0 deletions M2/Macaulay2/d/binding.d
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,10 @@ export bind(e:ParseTree,dictionary:Dictionary):void := (
-- i.alternate = bindnewdictionary(i.alternate,dictionary);
bind(i.alternate,dictionary);
)
is i:TryThen do (
bind(i.primary,dictionary);
bind(i.sequel,dictionary);
)
is i:TryThenElse do (
bind(i.primary,dictionary);
bind(i.sequel,dictionary);
Expand Down
7 changes: 4 additions & 3 deletions M2/Macaulay2/d/convertr.d
Original file line number Diff line number Diff line change
Expand Up @@ -432,9 +432,10 @@ export convert(e:ParseTree):Code := (
pos := treePosition(e);
nd := nestingDepth(sym.frameID,token.dictionary);
Code(localSymbolClosureCode(nd,sym,pos)))
is i:TryThenElse do Code(tryCode(convert(i.primary),convert(i.sequel),convert(i.alternate),treePosition(e)))
is i:TryElse do Code(tryCode(convert(i.primary),NullCode,convert(i.alternate),treePosition(e)))
is i:Try do Code(tryCode(convert(i.primary),NullCode,NullCode,treePosition(e)))
is i:TryThenElse do Code(tryCode(convert(i.primary), convert(i.sequel), convert(i.alternate), treePosition(e)))
is i:TryThen do Code(tryCode(convert(i.primary), convert(i.sequel), NullCode, treePosition(e)))
is i:TryElse do Code(tryCode(convert(i.primary), NullCode, convert(i.alternate), treePosition(e)))
is i:Try do Code(tryCode(convert(i.primary), NullCode, NullCode, treePosition(e)))
is i:Catch do Code(catchCode(convert(i.primary),treePosition(e)))
is u:Postfix do Code(unaryCode(u.Operator.entry.postfix,convert(u.lhs),treePosition(e)))
is d:dummy do dummyCode
Expand Down
13 changes: 5 additions & 8 deletions M2/Macaulay2/d/evaluate.d
Original file line number Diff line number Diff line change
Expand Up @@ -1474,14 +1474,11 @@ export evalraw(c:Code):Expr := (
is c:globalSymbolClosureCode do return Expr(SymbolClosure(globalFrame,c.symbol))
is c:threadSymbolClosureCode do return Expr(SymbolClosure(threadFrame,c.symbol))
is c:tryCode do (
p := tryEval(c.code);
if tryEvalSuccess
then (
when p is Error do p
else if c.thenClause == NullCode then p
else eval(c.thenClause))
else (
eval(c.elseClause)))
ret := tryEval(c.code);
if tryEvalSuccess then
when ret is Error do ret
else if c.thenClause == NullCode then ret else eval(c.thenClause)
else if c.elseClause == NullCode then nullE else eval(c.elseClause))
is c:catchCode do (
p := eval(c.code);
when p is err:Error do if err.message == throwMessage then err.value else p
Expand Down
7 changes: 4 additions & 3 deletions M2/Macaulay2/d/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ export For := {+ forToken:Token, variable:ParseTree, inClause:ParseTree, fromCla
export WhileDo := {+ whileToken:Token, predicate:ParseTree, dotoken:Token, doClause:ParseTree};
export WhileList := {+ whileToken:Token, predicate:ParseTree, listtoken:Token, listClause:ParseTree};
export WhileListDo := {+ whileToken:Token, predicate:ParseTree, listtoken:Token, listClause:ParseTree, dotoken:Token, doClause:ParseTree };
export TryElse := {+ tryToken:Token, primary:ParseTree, elseToken:Token, alternate:ParseTree};
export TryThenElse := {+ tryToken:Token, primary:ParseTree, thenToken:Token, sequel:ParseTree, elseToken:Token, alternate:ParseTree};
export Try := {+ tryToken:Token, primary:ParseTree};
export TryThen := {+ tryToken:Token, primary:ParseTree, thenToken:Token, sequel:ParseTree};
export TryElse := {+ tryToken:Token, primary:ParseTree, elseToken:Token, alternate:ParseTree};
export Try := {+ tryToken:Token, primary:ParseTree};
export Catch := {+ catchToken:Token, primary:ParseTree};
export IfThen := {+ ifToken:Token, predicate:ParseTree, thenclause:ParseTree };
export IfThenElse := {+ ifToken:Token, predicate:ParseTree, thenclause:ParseTree, elseClause:ParseTree};
Expand All @@ -150,7 +151,7 @@ export ParseTree := (
Token or Adjacent or Binary or Unary or Postfix or Parentheses
or EmptyParentheses or IfThen or IfThenElse
or Quote or GlobalQuote or ThreadQuote or LocalQuote
or TryThenElse or TryElse or Try or Catch or WhileDo or For or WhileList or WhileListDo or Arrow or New or dummy );
or TryThenElse or TryThen or TryElse or Try or Catch or WhileDo or For or WhileList or WhileListDo or Arrow or New or dummy );


-- Code
Expand Down
12 changes: 6 additions & 6 deletions M2/Macaulay2/d/parser.d
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,7 @@ export unarytry(tryToken:Token,file:TokenFile,prec:int,obeylines:bool):ParseTree
elseClause := parse(file,elseW.parse.unaryStrength,obeylines);
if elseClause == errorTree then return errorTree;
accumulate(ParseTree(TryThenElse(tryToken,primary,thenToken,thenClause,elseToken,elseClause)),file,prec,obeylines))
else (
printErrorMessage(tryToken,"syntax error : expected 'else' to match this 'try'");
errorTree))
else accumulate(ParseTree(TryThen(tryToken,primary,thenToken,thenClause)),file,prec,obeylines))
else accumulate(ParseTree(Try(tryToken,primary)),file,prec,obeylines));
export unarycatch(catchToken:Token,file:TokenFile,prec:int,obeylines:bool):ParseTree := (
primary := parse(file,catchToken.word.parse.unaryStrength,obeylines);
Expand Down Expand Up @@ -532,6 +530,7 @@ export treePosition(e:ParseTree):Position := (
is ee:EmptyParentheses do return position(ee.left)
is i:IfThen do return position(i.ifToken)
is i:TryThenElse do return position(i.tryToken)
is i:TryThen do return position(i.tryToken)
is i:TryElse do return position(i.tryToken)
is i:Try do return position(i.tryToken)
is i:Catch do return position(i.catchToken)
Expand Down Expand Up @@ -564,9 +563,10 @@ export size(e:ParseTree):int := (
is x:EmptyParentheses do Ccode(int,"sizeof(*",x,")") + size(x.left) + size(x.right)
is x:IfThen do Ccode(int,"sizeof(*",x,")") + size(x.ifToken) + size(x.predicate) + size(x.thenclause)
is x:IfThenElse do Ccode(int,"sizeof(*",x,")") + size(x.ifToken) + size(x.predicate) + size(x.thenclause) + size(x.elseClause)
is x:TryThenElse do Ccode(int,"sizeof(*",x,")") + size(x.tryToken) + size(x.primary) + size(x.thenToken) + size(x.sequel) + size(x.elseToken) + size(x.alternate)
is x:TryElse do Ccode(int,"sizeof(*",x,")") + size(x.tryToken) + size(x.primary) + size(x.elseToken) + size(x.alternate)
is x:Try do Ccode(int,"sizeof(*",x,")") + size(x.tryToken) + size(x.primary)
is x:TryThenElse do Ccode(int,"sizeof(*",x,")") + size(x.tryToken) + size(x.primary) + size(x.thenToken) + size(x.sequel) + size(x.elseToken) + size(x.alternate)
is x:TryThen do Ccode(int,"sizeof(*",x,")") + size(x.tryToken) + size(x.primary) + size(x.thenToken) + size(x.sequel)
is x:TryElse do Ccode(int,"sizeof(*",x,")") + size(x.tryToken) + size(x.primary) + size(x.elseToken) + size(x.alternate)
is x:Try do Ccode(int,"sizeof(*",x,")") + size(x.tryToken) + size(x.primary)
is x:Catch do Ccode(int,"sizeof(*",x,")") + size(x.catchToken) + size(x.primary)
is x:For do Ccode(int,"sizeof(*",x,")")+ size(x.forToken) + size(x.variable) + size(x.inClause) + size(x.fromClause) + size(x.toClause) + size(x.whenClause) + size(x.listClause) + size(x.doClause)
is x:WhileDo do Ccode(int,"sizeof(*",x,")") + size(x.whileToken) + size(x.predicate) + size(x.dotoken) + size(x.doClause)
Expand Down
10 changes: 7 additions & 3 deletions M2/Macaulay2/m2/basictests/A01.m2
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,15 @@ assert( not not true )
assert( not true === false )
assert( not false === true )

-- test try
assert( try error "" else true )
-- test try .. then .. else clauses
assert( true === try 1/0 then false else true )
assert( null === try 1/0 then false )
assert( true === try 1/0 else true )
assert( null === try 1/0 )
assert( try true then true else false )
assert( try true else false )
assert( try true then true )
assert( try true )
assert( null === try error "" )
--i = 5
--try i := 6 else i :=7
--assert(i === 5)
Expand Down
4 changes: 4 additions & 0 deletions M2/Macaulay2/packages/Macaulay2Doc/ov_language.m2
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,10 @@ document {
PARA{},
"The clause '", TT "then y", "' may be omitted, in which case the return value is the value returned by ", TT "x", ", if there is no error or alarm.",
PARA{},
"The clause '", TT "else z", "' may be omitted,
in which case the return value is the value returned by ", TT "y", ",
unless an error or alarm occurs, in which case ", TO "null", " is returned.",
PARA{},
"The clauses '", TT "then y else z", "' may both be omitted, in which case the return value is the value returned by ", TT "x", ", unless an error or
alarm occurs, in which case ", TO "null", " is returned.",
PARA{},
Expand Down