Skip to content

Commit

Permalink
add support for more nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbevain committed Jun 23, 2010
1 parent b88e0d7 commit e800cdf
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 17 deletions.
70 changes: 60 additions & 10 deletions Mono.Linq.Expressions/CSharpWriter.cs
Expand Up @@ -11,19 +11,36 @@ public CSharpWriter (IFormatter formatter)
{
}

public override void Write (LambdaExpression expression)
{
VisitLambdaSignature (expression);
VisitLambdaBody (expression);
}

protected override Expression VisitLambda<T> (Expression<T> node)
{
VisitLambdaSignature (node);
VisitParameters (node);
WriteSpace ();
WriteToken ("=>");
WriteLine ();

VisitLambdaBody (node);

return node;
}

void VisitLambdaSignature<T> (Expression<T> node)
void VisitLambdaSignature (LambdaExpression node)
{
VisitType (node.ReturnType);
WriteSpace ();
WriteIdentifier (node.Name, node);
VisitParameters (node);

WriteLine ();
}

void VisitParameters (LambdaExpression node)
{
VisitParenthesizedList (node.Parameters, parameter => {
VisitType (parameter.Type);
Expand All @@ -32,19 +49,17 @@ void VisitLambdaSignature<T> (Expression<T> node)
WriteIdentifier (parameter.Name, parameter);
}
});

WriteLine ();
}

void VisitLambdaBody<T> (Expression<T> node)
void VisitLambdaBody (LambdaExpression node)
{
if (node.Body.NodeType != ExpressionType.Block)
VisitSingleExpressionBody (node);
else
VisitBlockExpressionBody (node);
}

void VisitBlockExpressionBody<T> (Expression<T> node)
void VisitBlockExpressionBody (LambdaExpression node)
{
VisitBlockExpression ((BlockExpression) node.Body);
}
Expand Down Expand Up @@ -80,7 +95,7 @@ static bool IsActualStatement (Expression expression)
}


void VisitSingleExpressionBody<T> (Expression<T> node)
void VisitSingleExpressionBody (LambdaExpression node)
{
VisitBlock (() => {
if (node.ReturnType != typeof (void) && !IsStatement (node.Body)) {
Expand Down Expand Up @@ -221,7 +236,6 @@ void VisitBlock (Action action)

Dedent ();
WriteToken ("}");
WriteLine ();
}

void VisitBlockExpression (BlockExpression node)
Expand Down Expand Up @@ -488,7 +502,6 @@ static bool IsChecked (ExpressionType type)
switch (type) {
case ExpressionType.AddAssignChecked:
case ExpressionType.AddChecked:
case ExpressionType.ConvertChecked:
case ExpressionType.MultiplyAssignChecked:
case ExpressionType.MultiplyChecked:
case ExpressionType.NegateChecked:
Expand Down Expand Up @@ -541,6 +554,16 @@ protected override Expression VisitUnary (UnaryExpression node)
case ExpressionType.PostIncrementAssign:
VisitPostIncrementAssign (node);
break;
case ExpressionType.ConvertChecked:
VisitConvertChecked (node);
break;
case ExpressionType.Convert:
case ExpressionType.Unbox:
VisitConvert (node);
break;
case ExpressionType.Quote:
Visit (node.Operand);
break;
default:
VisitSimpleUnary (node);
break;
Expand All @@ -549,6 +572,20 @@ protected override Expression VisitUnary (UnaryExpression node)
return node;
}

void VisitConvert (UnaryExpression node)
{
WriteToken ("(");
VisitType (node.Type);
WriteToken (")");

Visit (node.Operand);
}

void VisitConvertChecked (UnaryExpression node)
{
VisitChecked (() => VisitConvert (node));
}

void VisitPostIncrementAssign (UnaryExpression node)
{
Visit (node.Operand);
Expand Down Expand Up @@ -1040,6 +1077,7 @@ protected override Expression VisitTry (TryExpression node)
void VisitAsBlock (Expression node)
{
Visit (node.Is (ExpressionType.Block) ? node : Expression.Block (node));
WriteLine ();
}

protected override CatchBlock VisitCatchBlock (CatchBlock node)
Expand Down Expand Up @@ -1080,7 +1118,7 @@ protected override Expression VisitLoop (LoopExpression node)
WriteToken (")");
WriteLine ();

Visit (node.Body);
VisitAsBlock (node.Body);

return node;
}
Expand All @@ -1107,6 +1145,8 @@ protected override Expression VisitSwitch (SwitchExpression node)
}
});

WriteLine ();

return node;
}

Expand All @@ -1124,5 +1164,15 @@ protected override SwitchCase VisitSwitchCase (SwitchCase node)

return node;
}

protected override Expression VisitDefault (DefaultExpression node)
{
WriteKeyword ("default");
WriteToken ("(");
VisitType (node.Type);
WriteToken (")");

return node;
}
}
}
5 changes: 5 additions & 0 deletions Mono.Linq.Expressions/ExpressionWriter.cs
Expand Up @@ -11,6 +11,11 @@ protected ExpressionWriter (IFormatter formatter)
this.formatter = formatter;
}

public virtual void Write (LambdaExpression expression)
{
Visit (expression.Body);
}

public virtual void Write (Expression expression)
{
Visit (expression);
Expand Down
81 changes: 80 additions & 1 deletion Test/Mono.Linq.Expressions/CSharpWriterTest.cs
Expand Up @@ -868,6 +868,85 @@ int IncrementDecrement(int i)
", body, i);
}

[Test]
public void Default ()
{
var body = Expression.Default (typeof (int));

AssertLambda<Func<int>> (@"
int Default()
{
return default(int);
}
", body);
}

[Test]
public void Convert ()
{
var o = Expression.Parameter (typeof (object), "o");

var body = Expression.Convert (o, typeof (string));

AssertLambda<Func<object, string>> (@"
string Convert(object o)
{
return (string)o;
}
", body, o);
}

[Test]
public void ConvertChecked ()
{
var i = Expression.Parameter (typeof (int), "i");

var body = Expression.ConvertChecked (i, typeof (short));

AssertLambda<Func<int, short>> (@"
short ConvertChecked(int i)
{
return checked { (short)i };
}
", body, i);
}

[Test]
public void Unbox ()
{
var o = Expression.Parameter (typeof (object), "o");

var body = Expression.Unbox (o, typeof (int));

AssertLambda<Func<object, int>> (@"
int Unbox(object o)
{
return (int)o;
}
", body, o);
}

[Test]
public void QuoteLambda ()
{
var s = Expression.Parameter (typeof (string), "s");

var lambda = Expression.Lambda<Func<string, Expression<Func<string>>>> (
Expression.Quote (
Expression.Lambda<Func<string>> (s, new ParameterExpression [0])),
s);

AssertExpression (@"
Expression<Func<string>> (string s)
{
return () =>
{
return s;
};
}
", lambda);
}

[MethodImpl (MethodImplOptions.NoInlining)]
static void AssertLambda<TDelegate> (string expected, Expression body, params ParameterExpression [] parameters) where TDelegate : class
{
Expand All @@ -885,7 +964,7 @@ static string GetTestCaseName ()
return stack_frame.GetMethod ().Name;
}

static void AssertExpression (string expected, Expression expression)
static void AssertExpression (string expected, LambdaExpression expression)
{
var result = new StringWriter ();
var csharp = new CSharpWriter (new TextFormatter (result));
Expand Down
12 changes: 6 additions & 6 deletions nodes.txt
@@ -1,14 +1,8 @@
TODO:
Convert
ConvertChecked
Lambda
Quote
DebugInfo
Dynamic
Default
Extension
RuntimeVariables
Unbox

DONE:

Expand Down Expand Up @@ -87,3 +81,9 @@ DONE:
NegateChecked
Not
OnesComplement
Default
Convert
ConvertChecked
Unbox
Lambda
Quote

0 comments on commit e800cdf

Please sign in to comment.