Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Feb 4, 2019. It is now read-only.
/PashPublic archive

Commit78587b8

Browse files
committed
Merge pull request#413 from sburnicki/finally_clause
Support for "finally" clauses
2 parentsbfb3d84 +c391bd1 commit78587b8

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
usingSystem;
2+
usingNUnit.Framework;
3+
4+
namespaceReferenceTests.Language
5+
{
6+
[TestFixture]
7+
publicclassTryCatchFinallyTests:ReferenceTestBase
8+
{
9+
[Test]
10+
publicvoidTryFinallyWithoutCatch()
11+
{
12+
ExecuteAndCompareTypedResult(
13+
"try { 2 + 2 } finally { 0 }",
14+
4,
15+
0
16+
);
17+
}
18+
19+
[Test]
20+
publicvoidTryCatchFinally()
21+
{
22+
ExecuteAndCompareTypedResult(
23+
"try { 2 + 2 } catch { -1 } finally { 0 }",
24+
4,
25+
0
26+
);
27+
}
28+
29+
[Test]
30+
publicvoidTryCatchFinallyWithException()
31+
{
32+
ExecuteAndCompareTypedResult(
33+
"try { 2 / 0 } catch { -1 } finally { 3 }",
34+
-1,
35+
3
36+
);
37+
}
38+
}
39+
}
40+

‎Source/ReferenceTests/ReferenceTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<CompileInclude="TestUtil.cs" />
139139
<CompileInclude="Commands\ConvertToSecureStringTests.cs" />
140140
<CompileInclude="Language\ExpressionTests.cs" />
141+
<CompileInclude="Language\TryCatchFinallyTests.cs" />
141142
</ItemGroup>
142143
<ItemGroup>
143144
<ProjectReferenceInclude="..\System.Management\System.Management.csproj">

‎Source/System.Management/Pash/Implementation/ExecutionVisitor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,8 +1322,16 @@ public override AstVisitAction VisitTryStatement(TryStatementAst tryStatementAst
13221322
{
13231323
SetUnderscoreVariable(ex);
13241324

1325+
// not yet considered: no catches, special catches!
13251326
tryStatementAst.CatchClauses.Last().Body.Visit(this);
13261327
}
1328+
finally
1329+
{
1330+
if(tryStatementAst.Finally!=null)
1331+
{
1332+
tryStatementAst.Finally.Visit(this);
1333+
}
1334+
}
13271335

13281336
returnAstVisitAction.SkipChildren;
13291337
}

‎Source/System.Management/Pash/ParserIntrinsics/AstBuilder.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,20 @@ private StatementAst BuildBreakStatementAst(ParseTreeNode parseTreeNode)
424424

425425
StatementAstBuildTryStatementAst(ParseTreeNodeparseTreeNode)
426426
{
427+
varsubClauses=parseTreeNode.ChildNodes[0].ChildNodes;
428+
varcatchClauses=subClauses[2];
429+
varfinallyClause=subClauses.Count>3?subClauses[3]:null;
430+
// there might be no catch clauses, then a finally clause could follow directly
431+
if(catchClauses.Term==this._grammar.finally_clause)
432+
{
433+
finallyClause=catchClauses;
434+
catchClauses=null;
435+
}
427436
returnnewTryStatementAst(
428437
newScriptExtent(parseTreeNode),
429-
BuildStatementBlockAst(parseTreeNode.ChildNodes[0].ChildNodes[1]),
430-
BuildCatchClausesAst(parseTreeNode.ChildNodes[0].ChildNodes[2]),
431-
null
438+
BuildStatementBlockAst(subClauses[1]),
439+
catchClauses==null?Enumerable.Empty<CatchClauseAst>():BuildCatchClausesAst(catchClauses),
440+
finallyClause==null?null:BuildFinallyClauseAst(finallyClause)
432441
);
433442
}
434443

@@ -452,6 +461,13 @@ private CatchClauseAst BuildCatchClauseAst(ParseTreeNode parseTreeNode)
452461
);
453462
}
454463

464+
StatementBlockAstBuildFinallyClauseAst(ParseTreeNodefinallyClause)
465+
{
466+
VerifyTerm(finallyClause,this._grammar.finally_clause);
467+
468+
returnBuildStatementBlockAst(finallyClause.ChildNodes[1]);
469+
}
470+
455471
StatementAstBuildDataStatementAst(ParseTreeNodeparseTreeNode)
456472
{
457473
thrownewNotImplementedException();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp