Skip to content

Commit eb9b04c

Browse files
committed
cleanup
rename IsAttrib to HasAttrib add string.Brace
1 parent bd5681d commit eb9b04c

File tree

10 files changed

+64
-77
lines changed

10 files changed

+64
-77
lines changed

backend/Core/Decl.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public bool IsTemplateUp {
4545
}
4646
}
4747

48-
public bool IsInline => IsAttrib( "inline" ) || IsTemplateUp;
48+
public bool IsInline => HasAttrib( "inline" ) || IsTemplateUp;
4949
public bool IsInStruct => scope.parent.decl is Structural;
5050

5151
// TODO Symbol?
@@ -114,9 +114,9 @@ public class Func : Decl, ITplParams
114114
public Stmt block;
115115
public Typespec retType;
116116

117-
public bool IsVirtual => IsAttrib( "virtual" );
118-
public bool IsConst => IsAttrib( "const" ) || IsAttrib( "pure" );
119-
public bool IsOverride => IsAttrib( "override" );
117+
public bool IsVirtual => HasAttrib( "virtual" );
118+
public bool IsConst => HasAttrib( "const" ) || HasAttrib( "pure" );
119+
public bool IsOverride => HasAttrib( "override" );
120120

121121
// TODO: analyze, for void or auto return type of funcs
122122
public bool IsReturningSomething => false;
@@ -140,8 +140,8 @@ public enum Kind
140140
public List<Param> paras;
141141
public Stmt block;
142142

143-
public bool IsVirtual => IsAttrib( "virtual" );
144-
public bool IsImplicit => IsAttrib( "implicit" );
143+
public bool IsVirtual => HasAttrib( "virtual" );
144+
public bool IsImplicit => HasAttrib( "implicit" );
145145

146146
// TODO: initlist
147147

@@ -221,7 +221,7 @@ public class Enumeration : Hierarchical
221221
{
222222
public TypespecBasic basetype;
223223

224-
public bool IsFlags => IsAttrib( "flags" );
224+
public bool IsFlags => HasAttrib( "flags" );
225225
public bool IsOpBitwise => IsAttrib( "operators", "bitwise" );
226226

227227
// TODO this needs to be in the Generator Folder

backend/Core/Expr.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -197,17 +197,15 @@ public class UnOp : Expr
197197
public override string Gen( bool doBrace = false )
198198
{
199199
if( op == Operand.Parens )
200-
return Format( "({0})", expr.Gen() );
200+
return expr.Gen().Brace( true );
201201

202202
bool isPreOp = op.Between( Operand.PreOps_Begin, Operand.PreOps_End );
203203
bool isPostOp = op.Between( Operand.PostOps_Begin, Operand.PostOps_End );
204204
bool divPrecedence = IsDivergentPrecedence;
205205
bool doBraceExpr = (divPrecedence || expr.IsDivergentPrecedence)
206206
&& OriginalPrecedenceLevel < expr.OriginalPrecedenceLevel;
207207

208-
string opFormat = doBrace
209-
? "(" + op.GetFormat() + ")"
210-
: op.GetFormat();
208+
string opFormat = op.GetFormat().Brace( doBrace );
211209

212210
return Format(
213211
opFormat,
@@ -236,9 +234,7 @@ public override string Gen( bool doBrace = false )
236234
bool doBraceRight = (divPrecedence || right.IsDivergentPrecedence)
237235
&& OriginalPrecedenceLevel < right.OriginalPrecedenceLevel;
238236

239-
string opFormat = doBrace
240-
? "(" + op.GetFormat() + ")"
241-
: op.GetFormat();
237+
string opFormat = op.GetFormat().Brace( doBrace );
242238

243239
return Format(
244240
opFormat,
@@ -247,7 +243,7 @@ public override string Gen( bool doBrace = false )
247243
}
248244
}
249245

250-
/// Ternary Operation - three operands, right now only: if ? then : else
246+
/// Ternary Operation - three operands, currently only: if ? then : else
251247
public class TernOp : Expr
252248
{
253249
public Expr left { get; set; }
@@ -264,10 +260,10 @@ public override string Gen( bool doBrace = false )
264260
bool doBraceRight = (divPrecedence || right.IsDivergentPrecedence)
265261
&& OriginalPrecedenceLevel < right.OriginalPrecedenceLevel;
266262

263+
string opFormat = "{0} ? {1} : {2}".Brace( doBrace );
264+
267265
return Format(
268-
doBrace
269-
? "({0} ? {1} : {2})"
270-
: "{0} ? {1} : {2}",
266+
opFormat,
271267
left.Gen( doBraceLeft ),
272268
mid.Gen( doBraceMid ),
273269
right.Gen( doBraceRight ) );
@@ -284,9 +280,7 @@ public override string Gen( bool doBrace = false )
284280
.Select( s => s.Gen() )
285281
.Join( "::" );
286282

287-
return doBrace
288-
? "(" + ret + ")"
289-
: ret;
283+
return ret.Brace( doBrace );
290284
}
291285
}
292286

@@ -336,9 +330,7 @@ public class FuncCallExpr : UnOp
336330
public override string Gen( bool doBrace = false )
337331
{
338332
string ret = expr.Gen() + funcCall.Gen();
339-
return doBrace
340-
? "(" + ret + ")"
341-
: ret;
333+
return ret.Brace( doBrace );
342334
}
343335
}
344336

@@ -363,9 +355,7 @@ public override string Gen( bool doBrace = false )
363355
throw new Exception( Format( "Invalid cast of {0} to {1}", exprText, typeText ) );
364356

365357
string ret = Format( format, exprText, typeText );
366-
return doBrace
367-
? "(" + ret + ")"
368-
: ret;
358+
return ret.Brace( doBrace );
369359
}
370360
}
371361

@@ -377,9 +367,7 @@ public class NewExpr : Expr
377367
public override string Gen( bool doBrace = false )
378368
{
379369
string ret = "new " + type.Gen() + funcCall.Gen();
380-
return doBrace
381-
? "(" + ret + ")"
382-
: ret;
370+
return ret.Brace( doBrace );
383371
}
384372
}
385373

@@ -390,7 +378,7 @@ public class Literal : Expr
390378

391379
public override string Gen( bool doBrace = false )
392380
{
393-
return /*doBrace ? "(" + text + ")" :*/ text;
381+
return text; //.Brace( doBrace )
394382
}
395383
}
396384
}

backend/Core/Extensions.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ public static class Extensions
1111
{
1212
public static bool In<T>( this T val, params T[] values )
1313
//where T : struct
14-
{
15-
return values.Contains( val );
16-
}
14+
=> values.Contains( val );
1715

1816
// public static bool In( this string val, params string[] values )
1917
// {
@@ -22,30 +20,30 @@ public static bool In<T>( this T val, params T[] values )
2220

2321
public static bool Between<T>( this T value, T min, T max )
2422
where T : IComparable //<T>
25-
{
26-
return min.CompareTo( value ) <= 0
27-
&& value.CompareTo( max ) <= 0;
28-
}
23+
=> min.CompareTo( value ) <= 0
24+
&& value.CompareTo( max ) <= 0;
2925

3026
// handles negative counts gracefully, returning an empty string
3127
public static string Repeat( this string value, int count )
32-
{
33-
return count <= 0 || string.IsNullOrEmpty( value )
28+
=> count <= 0 || string.IsNullOrEmpty( value )
3429
? string.Empty
3530
: new StringBuilder( value.Length * count )
3631
.Insert( 0, value, count )
3732
.ToString();
38-
}
33+
34+
public static string Brace( this string value, bool doBrace )
35+
=> doBrace
36+
? string.Format( "({0})", value )
37+
: value;
3938

4039
// why was this ICollection instead of IEnumerable?
4140
public static string Join( this IEnumerable<string> values, string delimiter )
42-
{
4341
// TODO: this might be the bottleneck in the end, bench when finished
4442
// there is a specialized string.Join with string[]
45-
return string.Join( delimiter, values );
46-
}
43+
=> string.Join( delimiter, values );
4744

4845
[Pure]
49-
public static bool IsEmpty<T>( [NotNull] this IEnumerable<T> values ) => !values.Any();
46+
public static bool IsEmpty<T>( [NotNull] this IEnumerable<T> values )
47+
=> !values.Any();
5048
}
5149
}

backend/Core/Stmt.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public class Stmt
1717
public SrcPos srcPos;
1818
private Attribs attribs { get; set; }
1919

20-
public bool IsStatic => IsAttrib( "static" );
20+
public bool IsStatic => HasAttrib( "static" );
2121

22-
public bool IsAttrib( string key )
22+
public bool HasAttrib( string key )
2323
=> attribs != null
2424
&& attribs.ContainsKey( key );
2525

@@ -40,7 +40,7 @@ protected virtual void AttribsAssigned() {}
4040

4141
public override string ToString()
4242
{
43-
var sb = new StringBuilder();
43+
StringBuilder sb = new();
4444
foreach( var info in GetType().GetProperties() ) {
4545
object value = info.GetValue( this, null )
4646
?? "(null)";
@@ -171,7 +171,7 @@ public override Strings Gen( int level )
171171
throw new NotImplementedException(
172172
"no depth except 1 supported directly, analyze step must take care of this!" );
173173

174-
return new Strings {
174+
return new() {
175175
Format( BreakFormat, IndentString.Repeat( level ) )
176176
};
177177
}
@@ -192,8 +192,8 @@ public class IfStmt : Stmt
192192

193193
public override Strings Gen( int level )
194194
{
195+
Strings ret = new();
195196
string indent = IndentString.Repeat( level );
196-
Strings ret = new Strings();
197197
int index = 0;
198198
foreach( CondThen ifThen in ifThens ) {
199199
ret.Add( Format( IfFormat[index], indent, ifThen.condExpr.Gen() ) );
@@ -233,8 +233,8 @@ public class LoopStmt : Stmt
233233

234234
public override Strings Gen( int level )
235235
{
236+
Strings ret = new();
236237
string indent = IndentString.Repeat( level );
237-
Strings ret = new Strings();
238238
ret.Add( Format( LoopFormat[1], indent, "true" ) );
239239
ret.Add( Format( CurlyOpen, indent ) );
240240
ret.AddRange( bodyStmt.GenWithoutCurly( level + 1 ) );
@@ -260,8 +260,8 @@ public override Strings Gen( int level )
260260
if( inits.Count > 1 )
261261
throw new NotImplementedException( "for statement does not support more than one initializer yet" );
262262

263+
Strings ret = new();
263264
string indent = IndentString.Repeat( level );
264-
Strings ret = new Strings();
265265
ret.Add( Format( LoopFormat[0], indent, inits.Count == 0 ? ";" : inits.First(), condExpr.Gen(), iterExpr.Gen() ) );
266266
ret.Add( Format( CurlyOpen, indent ) );
267267
ret.AddRange( bodyStmt.GenWithoutCurly( level + 1 ) );
@@ -281,8 +281,8 @@ public override Strings Gen( int level )
281281
if( elseStmt != null )
282282
throw new NotImplementedException( "implement else for while-loop" );
283283

284+
Strings ret = new();
284285
string indent = IndentString.Repeat( level );
285-
Strings ret = new Strings();
286286
ret.Add( Format( LoopFormat[1], indent, condExpr.Gen() ) );
287287
ret.Add( Format( CurlyOpen, indent ) );
288288
ret.AddRange( bodyStmt.GenWithoutCurly( level + 1 ) );
@@ -297,8 +297,8 @@ public class DoWhileStmt : LoopStmt
297297

298298
public override Strings Gen( int level )
299299
{
300+
Strings ret = new();
300301
string indent = IndentString.Repeat( level );
301-
Strings ret = new Strings();
302302
ret.Add( Format( LoopFormat[2], indent ) );
303303
ret.Add( Format( CurlyOpen, indent ) );
304304
ret.AddRange( bodyStmt.GenWithoutCurly( level + 1 ) );
@@ -319,9 +319,9 @@ public class TimesStmt : LoopStmt
319319

320320
public override Strings Gen( int level )
321321
{
322+
Strings ret = new();
322323
string varName = name ?? randomName;
323324
string indent = IndentString.Repeat( level );
324-
Strings ret = new Strings();
325325
ret.Add(
326326
Format(
327327
LoopFormat[0],
@@ -381,8 +381,8 @@ public override Strings Gen( int level )
381381
{
382382
// Block to Block needs to indent further else it's ok to remain same level
383383
// The curly braces need to be outdentented one level
384+
Strings ret = new();
384385
string indent = IndentString.Repeat( level /*- 1*/ );
385-
Strings ret = new Strings();
386386
ret.Add( Format( CurlyOpen, indent ) );
387387
ret.AddRange( stmts.SelectMany( s => s.Gen( level + 1 /*(s is Block ? 1 : 0)*/ ) ) );
388388
ret.Add( Format( CurlyClose, indent ) );

backend/Core/Typespec.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@ public override string GenType()
135135
public override string Gen( string name = "" )
136136
{
137137
string center = PointerizeName( "", name );
138-
center = (center == "") ? "" : Format( "({0})", center );
139138
return Format(
140139
"{0}{1}({2})",
141140
TargetGen(),
142-
center,
141+
center.Brace( center != "" ),
143142
paras
144143
.Select( p => p.Gen() )
145144
.Join( ", " ) );

backend/Visitor/VDecl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public override Decl VisitFuncDef( FuncDefContext c )
7676
// what was that for?
7777
//bool wasOK = NotifyObservers( ret );
7878
// validator
79-
//ret.IsAttrib( "blah" );
79+
//ret.HasAttrib( "blah" );
8080

8181
return ret;
8282
}

backend/Visitor/VTypes.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ private static readonly Dictionary<int, int>
6565
};
6666

6767
Typespec ret;
68-
if( c.typespecBasic() != null ) ret = VisitTypespecBasic( c.typespecBasic() );
69-
else if( c.typespecFunc() != null ) ret = VisitTypespecFunc( c.typespecFunc() );
70-
else if( c.typespecNested() != null ) ret = VisitTypespecNested( c.typespecNested() );
68+
if( c.typespecBasic() != null ) ret = VisitTypespecBasic( c.typespecBasic() );
69+
else if( c.typespecFunc() != null ) ret = VisitTypespecFunc( c.typespecFunc() );
70+
else if( c.typespecNested() != null ) ret = VisitTypespecNested( c.typespecNested() );
7171
else throw new Exception( "unknown typespec" );
7272
ret.srcPos = c.ToSrcPos();
7373
ret.qual = c.qual().Visit();

frontend/tests/gol/game_of_life.myll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ class GameOfLife
2525
{
2626
do sizeX times x
2727
{
28-
//if( 0 < y < sizeY-1
29-
// && 0 < x < sizeX-1 )
30-
if( 0 < y && y < sizeY-1
31-
&& 0 < x && x < sizeX-1 )
28+
if( 0 < y < sizeY-1
29+
&& 0 < x < sizeX-1 )
3230
{
3331
dstMap[y][x] = (rand() % 4 == 0) ? 'o' : ' ';
3432
}

0 commit comments

Comments
 (0)