Skip to content

Commit fa3e292

Browse files
committed
reorder some Stmt
add extension method to indent a list of strings
1 parent 377fdb4 commit fa3e292

File tree

3 files changed

+57
-37
lines changed

3 files changed

+57
-37
lines changed

backend/Core/Extensions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics.Contracts;
55
using System.Linq;
66
using System.Text;
7+
using Myll.Generator;
78

89
namespace Myll.Core
910
{
@@ -42,6 +43,15 @@ public static string Join( this IEnumerable<string> values, string delimiter )
4243
// there is a specialized string.Join with string[]
4344
=> string.Join( delimiter, values );
4445

46+
public static IEnumerable<string> Indent( this IEnumerable<string> values, string indent )
47+
=> values.Select( l => string.Format( "{0}{1}", indent, l ) );
48+
49+
public static IEnumerable<string> Indent( this IEnumerable<string> values, int level )
50+
{
51+
string indent = StmtFormatting.IndentString.Repeat( level );
52+
return values.Select( l => string.Format( "{0}{1}", indent, l ) );
53+
}
54+
4555
[Pure]
4656
public static bool IsEmpty<T>( [NotNull] this IEnumerable<T> values )
4757
=> !values.Any();

backend/Core/Stmt.cs

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,44 @@ public class AggrAssign : Stmt
173173
public Expr leftExpr;
174174
public Expr rightExpr;
175175

176+
public override Strings Gen( int level )
177+
=> new Strings { Format( op.GetAssignFormat(), leftExpr.Gen(), rightExpr.Gen() ) }
178+
.Indent( level )
179+
.ToList();
180+
}
181+
182+
public class ExprStmt : Stmt
183+
{
184+
public Expr expr;
185+
186+
public override Strings Gen( int level )
187+
=> new Strings { Format( "{0};", expr.Gen() ) }
188+
.Indent( level )
189+
.ToList();
190+
}
191+
192+
public class EmptyStmt : Stmt
193+
{
176194
public override Strings Gen( int level )
177195
{
178-
string indent = IndentString.Repeat( level );
179-
return new Strings {
180-
indent + Format( op.GetAssignFormat(), leftExpr.Gen(), rightExpr.Gen() ) + ";"
181-
};
196+
throw new NotImplementedException( "Would this just work as a semicolon?" );
182197
}
183198
}
184199

200+
// This class should be phased out in the far future, but for now it's just too useful
201+
public class FreetextStmt : Stmt
202+
{
203+
public Strings lines;
204+
205+
public FreetextStmt( string text )
206+
=> lines = new Strings { text };
207+
208+
public override Strings Gen( int level )
209+
=> lines
210+
.Indent( level )
211+
.ToList();
212+
}
213+
185214
/// =!= Stmt which contain other Stmt themselves =!=
186215

187216
// TODO move or remove?
@@ -484,23 +513,4 @@ public override Strings Gen( int level )
484513
return ret;
485514
}
486515
}
487-
488-
public class ExprStmt : Stmt
489-
{
490-
public Expr expr;
491-
492-
public override Strings Gen( int level )
493-
{
494-
string indent = IndentString.Repeat( level );
495-
return new Strings { Format( "{0}{1};", indent, expr.Gen() ) };
496-
}
497-
}
498-
499-
public class EmptyStmt : Stmt
500-
{
501-
public override Strings Gen( int level )
502-
{
503-
throw new NotImplementedException( "Would this just work as a semicolon?" );
504-
}
505-
}
506516
}

backend/Generator/ExprFormatting.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ private static readonly IDictionary<Operand, string>
5252

5353
private static readonly IDictionary<Operand, string>
5454
ToAssignFormat = new Dictionary<Operand, string> {
55-
{ Operand.Pow, "{0} = pow( {0}, {1} )" },
56-
{ Operand.Multiply, "{0} *= {1}" },
57-
{ Operand.EuclideanDivide, "{0} /= {1}" },
58-
{ Operand.Modulo, "{0} %= {1}" },
59-
{ Operand.Dot, "{0} = dot( {0}, {1} )" },
60-
{ Operand.Cross, "{0} = cross( {0}, {1} )" },
61-
{ Operand.Divide, "{0} = (double){0} / (double){1}" }, // TODO: this for integral, div() for others
62-
{ Operand.Add, "{0} += {1}" },
63-
{ Operand.Subtract, "{0} -= {1}" },
64-
{ Operand.BitAnd, "{0} &= {1}" },
65-
{ Operand.BitXor, "{0} ^= {1}" },
66-
{ Operand.BitOr, "{0} |= {1}" },
67-
{ Operand.LeftShift, "{0} <<= {1}"},
68-
{ Operand.RightShift,"{0} >>= {1}"},
55+
{ Operand.Pow, "{0} = pow( {0}, {1} );" },
56+
{ Operand.Multiply, "{0} *= {1};" },
57+
{ Operand.EuclideanDivide, "{0} /= {1};" },
58+
{ Operand.Modulo, "{0} %= {1};" },
59+
{ Operand.Dot, "{0} = dot( {0}, {1} );" },
60+
{ Operand.Cross, "{0} = cross( {0}, {1} );" },
61+
{ Operand.Divide, "{0} = (double){0} / (double){1};" }, // TODO: this for integral, div() for others
62+
{ Operand.Add, "{0} += {1};" },
63+
{ Operand.Subtract, "{0} -= {1};" },
64+
{ Operand.BitAnd, "{0} &= {1};" },
65+
{ Operand.BitXor, "{0} ^= {1};" },
66+
{ Operand.BitOr, "{0} |= {1};" },
67+
{ Operand.LeftShift, "{0} <<= {1};" },
68+
{ Operand.RightShift, "{0} >>= {1};" },
6969
};
7070

7171
public static string GetFormat( this Operand op )

0 commit comments

Comments
 (0)