Skip to content

Commit c550aa7

Browse files
committed
try & block
1 parent 5e695b7 commit c550aa7

6 files changed

Lines changed: 88 additions & 8 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Core.lua‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ local function try(try, catch, finally)
5353
ok, status, result = true, catch(status)
5454
end
5555
if ok then
56-
if status == -1 then
56+
if status == 1 then
5757
ok = false
5858
status = result
5959
end

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Exception.lua‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ System.define("System.KeyNotFoundException", {
135135
})
136136

137137
System.define("System.ArithmeticException", {
138+
__tostring = toString,
138139
__inherits__ = { System.Exception },
139140

140141
__ctor__ = function(this, message, innerException)

‎CSharp.lua/LuaAst/LuaFunctionExpressSyntax.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ public sealed class LuaConstructorAdapterExpressSyntax : LuaFunctionExpressSynta
3232

3333
public sealed class LuaTryBlockAdapterExpressSyntax : LuaFunctionExpressSyntax {
3434
public bool IsReturnExists { get; set; }
35+
public LuaIdentifierNameSyntax CatchTemp { get; set; }
3536
}
3637
}

‎CSharp.lua/LuaAst/LuaIdentifierNameSyntax.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public class LuaIdentifierNameSyntax : LuaExpressionSyntax {
5454
public readonly static LuaIdentifierNameSyntax ShiftRight = new LuaIdentifierNameSyntax("System.sr");
5555
public readonly static LuaIdentifierNameSyntax ShiftLeft = new LuaIdentifierNameSyntax("System.sl");
5656
public readonly static LuaIdentifierNameSyntax Try = new LuaIdentifierNameSyntax("System.try");
57+
public readonly static LuaIdentifierNameSyntax Is = new LuaIdentifierNameSyntax("System.is");
5758

5859
public LuaIdentifierNameSyntax(string valueText) {
5960
ValueText = valueText;

‎CSharp.lua/LuaSyntaxNodeTransfor.Object.cs‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎CSharp.lua/LuaSyntaxNodeTransfor.cs‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ public override LuaSyntaxNode VisitIdentifierName(IdentifierNameSyntax node) {
929929
name = symbol.Name;
930930
break;
931931
}
932+
case SymbolKind.Namespace:
932933
case SymbolKind.NamedType: {
933934
name = symbol.ToString();
934935
break;
@@ -1325,13 +1326,6 @@ public override LuaSyntaxNode VisitPostfixUnaryExpression(PostfixUnaryExpression
13251326
}
13261327
}
13271328

1328-
public override LuaSyntaxNode VisitThrowStatement(ThrowStatementSyntax node) {
1329-
LuaInvocationExpressionSyntax invocationExpression = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Throw);
1330-
var expression = (LuaExpressionSyntax)node.Expression.Accept(this);
1331-
invocationExpression.AddArgument(expression);
1332-
return new LuaExpressionStatementSyntax(invocationExpression);
1333-
}
1334-
13351329
private bool IsContinueExists(SyntaxNode node) {
13361330
ContinueSearcher searcher = new ContinueSearcher();
13371331
searcher.Visit(node);

0 commit comments

Comments
 (0)