Skip to content

Commit f342537

Browse files
committed
Refactoring
1 parent fab265e commit f342537

File tree

7 files changed

+46
-34
lines changed

7 files changed

+46
-34
lines changed

Library/CppLang/Generator.cs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ void ProcessClasses(YNamespace @namespace, NamespaceDeclarationSyntax inputNames
172172
}
173173
}
174174

175-
// todo here could be generics or reflection
175+
// todo good place to use generics or reflection
176176

177177
YStatement ProcessStatement(StatementSyntax statement)
178178
{
@@ -205,6 +205,8 @@ YExpr ProcessExpr(ExpressionSyntax expr)
205205
return ProcessExpr((MemberAccessExpressionSyntax)expr);
206206
} else if (expr is ThisExpressionSyntax) {
207207
return ProcessExpr((ThisExpressionSyntax)expr);
208+
} else if (expr is AssignmentExpressionSyntax) {
209+
return ProcessExpr((AssignmentExpressionSyntax)expr);
208210
}
209211

210212
throw new TException("Unable to process expression");
@@ -220,7 +222,7 @@ YExpr ProcessExpr(LiteralExpressionSyntax expr)
220222
if (expr.Token.IsKind(SyntaxKind.NullKeyword)) {
221223
return YLiteralExpr.Null;
222224
} else if (expr.Token.IsKind(SyntaxKind.NumericLiteralToken)) {
223-
return new YLiteralExpr(expr.Token.Value); // looks same as YConstExpr
225+
return new YLiteralExpr(expr.Token.Value); // Looks like YConstExpr, wtf?!
224226
}
225227

226228
throw new TException("Unable to process expr");
@@ -267,17 +269,21 @@ YExpr ProcessExpr(BinaryExpressionSyntax binaryExpression)
267269
return new YBinaryExpr(left, right, operation);
268270
}
269271

272+
YExpr ProcessExpr(AssignmentExpressionSyntax assignmentExpression)
273+
{
274+
YExpr left = ProcessExpr(assignmentExpression.Left);
275+
YExpr right = ProcessExpr(assignmentExpression.Right);
276+
277+
return new YAssign(left, right);
278+
}
279+
270280
YStatement ProcessStatement(IfStatementSyntax ifStatement)
271281
{
272282
YExpr condition = ProcessExpr(ifStatement.Condition);
273283
YStatement statement = ProcessStatement(ifStatement.Statement);
274284
YStatement elseStatement = ProcessStatement(ifStatement.Else.Statement);
275285

276-
if (condition != null && statement != null) {
277-
return new YIf(condition, statement, elseStatement);
278-
}
279-
280-
throw new TException("Unable to process statement");
286+
return new YIf(condition, statement, elseStatement);
281287
}
282288

283289
YStatement ProcessStatement(ReturnStatementSyntax statement)
@@ -298,18 +304,7 @@ YStatement ProcessStatement(BlockSyntax statement)
298304

299305
YStatement ProcessStatement(ExpressionStatementSyntax statement)
300306
{
301-
if (statement.Expression is AssignmentExpressionSyntax) { // ?!
302-
var assignmentExpression = (AssignmentExpressionSyntax)statement.Expression;
303-
304-
YExpr left = ProcessExpr(assignmentExpression.Left);
305-
YExpr right = ProcessExpr(assignmentExpression.Right);
306-
307-
if (left != null && right != null) {
308-
return new YAssign(left, right); // expression or statement?!
309-
}
310-
}
311-
312-
throw new TException("Unable to process statement");
307+
return new YExprStatement(ProcessExpr(statement.Expression));
313308
}
314309

315310
YOperator ProcessOperator(SyntaxToken token)

Library/CppLang/SourceCompiler.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,11 @@ static public void AppendEx(this StringBuilder builder, YStatement statement)
124124
AppendEx(builder, @return.Value);
125125
builder.Append(";");
126126

127-
} else if (statement is YAssign) {
128-
var assign = (YAssign)statement;
127+
} else if (statement is YExprStatement) {
129128

130-
AppendEx(builder, assign.Left);
131-
builder.Append("=");
132-
AppendEx(builder, assign.Right);
129+
AppendEx(builder, ((YExprStatement)statement).Expression);
133130
builder.Append(";");
131+
134132
} else if (statement is YIf) {
135133
var @if = (YIf)statement;
136134

@@ -189,6 +187,12 @@ static public void AppendEx(this StringBuilder builder, YExpr expr)
189187

190188
// no arguments
191189
builder.Append("()");
190+
} else if (expr is YAssign) {
191+
var assign = (YAssign)expr;
192+
193+
AppendEx(builder, assign.Left);
194+
builder.Append("=");
195+
AppendEx(builder, assign.Right);
192196
}
193197
}
194198

Library/Library.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,20 @@
7878
<Compile Include="YLang\YParameter.cs" />
7979
<Compile Include="YLang\Statements\YBlock.cs" />
8080
<Compile Include="YLang\Statements\YVar.cs" />
81-
<Compile Include="YLang\Statements\YAssign.cs" />
8281
<Compile Include="YLang\Expresions\YMemberAccessExpr.cs" />
8382
<Compile Include="YLang\Expresions\YThisExpr.cs" />
8483
<Compile Include="YLang\Expresions\YIdentifierExpr.cs" />
8584
<Compile Include="YLang\YDestructor.cs" />
8685
<Compile Include="TUnsupportedException.cs" />
8786
<Compile Include="YLang\Types\YRefType.cs" />
88-
<Compile Include="YLang\Operations\YOperator.cs" />
8987
<Compile Include="YLang\Statements\YIf.cs" />
9088
<Compile Include="YLang\Expresions\YBinaryExpr.cs" />
9189
<Compile Include="YLang\Expresions\YLiteralExpr.cs" />
9290
<Compile Include="YLang\Expresions\YPrefixUnaryExpr.cs" />
9391
<Compile Include="YLang\Expresions\YInvocation.cs" />
92+
<Compile Include="YLang\YOperator.cs" />
93+
<Compile Include="YLang\Statements\YExprStatement.cs" />
94+
<Compile Include="YLang\Expresions\YAssign.cs" />
9495
</ItemGroup>
9596
<ItemGroup>
9697
<None Include="packages.config" />
@@ -101,7 +102,6 @@
101102
<Folder Include="YLang\Expresions\" />
102103
<Folder Include="YLang\Types\" />
103104
<Folder Include="YLang\Statements\" />
104-
<Folder Include="YLang\Operations\" />
105105
</ItemGroup>
106106
<ItemGroup>
107107
<EmbeddedResource Include="Configs\uncrustify.cfg" />

Library/Prettifier.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ namespace SharpCpp
77
{
88
public class Prettifier : IDisposable
99
{
10-
private const string UncrustifyCommandName = "uncrustify";
10+
const string UncrustifyCommandName = "uncrustify";
1111

12-
private const string UncrustifyConfigId = "SharpCpp.Configs.uncrustify.cfg";
12+
const string UncrustifyConfigId = "SharpCpp.Configs.uncrustify.cfg";
1313

14-
private readonly string _uncrustifyFileName;
14+
readonly string _uncrustifyFileName;
1515

16-
private readonly string _uncrustifyConfigFileName;
16+
readonly string _uncrustifyConfigFileName;
1717

18-
private bool _disposed = false;
18+
bool _disposed = false;
1919

2020
public Prettifier()
2121
{
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
namespace SharpCpp
33
{
4-
public class YAssign : YStatement
4+
public class YAssign : YExpr
55
{
66
public YExpr Left;
77
public YExpr Right;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
namespace SharpCpp
3+
{
4+
public class YExprStatement : YStatement
5+
{
6+
public YExpr Expression;
7+
8+
public YExprStatement(YExpr expression)
9+
{
10+
Expression = expression;
11+
}
12+
}
13+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class YOperator
77

88
public static readonly YOperator Minus = new YOperator();
99

10-
private YOperator()
10+
YOperator()
1111
{
1212
}
1313
}

0 commit comments

Comments
 (0)