@@ -189,14 +189,97 @@ public override LuaSyntaxNode VisitTypeOfExpression(TypeOfExpressionSyntax node)
189189 return invocation ;
190190 }
191191
192+ public override LuaSyntaxNode VisitThrowStatement ( ThrowStatementSyntax node ) {
193+ LuaInvocationExpressionSyntax invocationExpression = new LuaInvocationExpressionSyntax ( LuaIdentifierNameSyntax . Throw ) ;
194+ if ( node . Expression != null ) {
195+ var expression = ( LuaExpressionSyntax ) node . Expression . Accept ( this ) ;
196+ invocationExpression . AddArgument ( expression ) ;
197+ }
198+ else {
199+ var curTryFunction = ( LuaTryBlockAdapterExpressSyntax ) CurFunction ;
200+ Contract . Assert ( curTryFunction . CatchTemp != null ) ;
201+ invocationExpression . AddArgument ( curTryFunction . CatchTemp ) ;
202+ }
203+ return new LuaExpressionStatementSyntax ( invocationExpression ) ;
204+ }
205+
192206 private LuaTryBlockAdapterExpressSyntax VisitTryCatchesExpress ( SyntaxList < CatchClauseSyntax > catches ) {
193207 LuaTryBlockAdapterExpressSyntax functionExpress = new LuaTryBlockAdapterExpressSyntax ( ) ;
194208 PushFunction ( functionExpress ) ;
195209 var temp = GetTempIdentifier ( catches . First ( ) ) ;
210+ functionExpress . CatchTemp = temp ;
196211 functionExpress . AddParameter ( temp ) ;
212+
213+ LuaIfStatementSyntax ifHeadStatement = null ;
214+ LuaIfStatementSyntax ifTailStatement = null ;
215+ bool hasCatchRoot = false ;
197216 foreach ( var catchNode in catches ) {
217+ LuaExpressionSyntax ifCondition = null ;
218+ if ( catchNode . Filter != null ) {
219+ ifCondition = ( LuaExpressionSyntax ) catchNode . Filter . FilterExpression . Accept ( this ) ;
220+ }
221+ if ( catchNode . Declaration != null ) {
222+ var typeName = ( LuaIdentifierNameSyntax ) catchNode . Declaration . Type . Accept ( this ) ;
223+ if ( typeName . ValueText != "System.Exception" ) {
224+ var mathcTypeInvocation = new LuaInvocationExpressionSyntax ( LuaIdentifierNameSyntax . Is ) ;
225+ mathcTypeInvocation . AddArgument ( temp ) ;
226+ mathcTypeInvocation . AddArgument ( typeName ) ;
227+ if ( ifCondition != null ) {
228+ ifCondition = new LuaBinaryExpressionSyntax ( ifCondition , LuaSyntaxNode . Tokens . And , mathcTypeInvocation ) ;
229+ }
230+ else {
231+ ifCondition = mathcTypeInvocation ;
232+ }
233+ }
234+ else {
235+ hasCatchRoot = true ;
236+ }
237+ }
238+ else {
239+ hasCatchRoot = true ;
240+ }
198241
242+ var block = ( LuaBlockSyntax ) catchNode . Block . Accept ( this ) ;
243+ if ( ifCondition != null ) {
244+ LuaIfStatementSyntax statement = new LuaIfStatementSyntax ( ifCondition ) ;
245+ if ( catchNode . Declaration != null && ! catchNode . Declaration . Identifier . IsKind ( SyntaxKind . None ) ) {
246+ var variableDeclarator = new LuaVariableDeclaratorSyntax ( new LuaIdentifierNameSyntax ( catchNode . Declaration . Identifier . ValueText ) ) ;
247+ variableDeclarator . Initializer = new LuaEqualsValueClauseSyntax ( temp ) ;
248+ statement . Body . Statements . Add ( new LuaLocalVariableDeclaratorSyntax ( variableDeclarator ) ) ;
249+ }
250+ statement . Body . Statements . AddRange ( block . Statements ) ;
251+ if ( ifTailStatement != null ) {
252+ ifTailStatement . Else = new LuaElseClauseSyntax ( statement ) ;
253+ }
254+ else {
255+ ifHeadStatement = statement ;
256+ }
257+ ifTailStatement = statement ;
258+ }
259+ else {
260+ if ( ifTailStatement != null ) {
261+ ifTailStatement . Else = new LuaElseClauseSyntax ( block ) ;
262+ }
263+ else {
264+ functionExpress . Body . Statements . AddRange ( block . Statements ) ;
265+ }
266+ break ;
267+ }
268+ }
269+
270+ if ( ifHeadStatement != null ) {
271+ if ( ! hasCatchRoot ) {
272+ Contract . Assert ( ifTailStatement . Else == null ) ;
273+ LuaMultipleReturnStatementSyntax rethrowStatement = new LuaMultipleReturnStatementSyntax ( ) ;
274+ rethrowStatement . Expressions . Add ( LuaIdentifierNameSyntax . One ) ;
275+ rethrowStatement . Expressions . Add ( temp ) ;
276+ LuaBlockSyntax block = new LuaBlockSyntax ( ) ;
277+ block . Statements . Add ( rethrowStatement ) ;
278+ ifTailStatement . Else = new LuaElseClauseSyntax ( block ) ;
279+ }
280+ functionExpress . Body . Statements . Add ( ifHeadStatement ) ;
199281 }
282+
200283 PopFunction ( ) ;
201284 return functionExpress ;
202285 }
0 commit comments