Skip to content

Commit 65f2225

Browse files
committed
Done with FooBar
1 parent f46e82e commit 65f2225

File tree

13 files changed

+314
-60
lines changed

13 files changed

+314
-60
lines changed

App/TestData.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public void SetNumber(int number) {
5656
5757
}
5858
}";
59-
59+
60+
// done
6061
const string InputCode4 = @"namespace CSharpFooBarLibrary
6162
{
6263
public class Bar

Library/Configs/uncrustify.cfg

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ newlines = lf # auto/lf/crlf/cr
1010
input_tab_size = 4 # number
1111

1212
# The size of tabs in the output (only used if align_with_tabs=true)
13-
output_tab_size = 3 # number
13+
output_tab_size = 2 # number
1414

1515
# The ascii value of the string escape char, usually 92 (\). (Pawn)
1616
string_escape_char = 92 # number
@@ -38,7 +38,7 @@ indent_brace = 0 # number
3838
indent_braces = false # false/true
3939

4040
# Indent based on the size of the brace parent, ie 'if' => 3 spaces, 'for' => 4 spaces, etc.
41-
indent_brace_parent = true # false/true
41+
indent_brace_parent = false # false/true
4242

4343
# Whether the 'namespace' body is indented
4444
indent_namespace = true # false/true
@@ -115,22 +115,22 @@ sp_before_byref = remove # ignore/add/remove/force
115115
sp_after_byref = ignore # ignore/add/remove/force
116116

117117
# Add or remove space before '<>'
118-
sp_before_angle = force # ignore/add/remove/force
118+
sp_before_angle = remove # ignore/add/remove/force
119119

120120
# Add or remove space after '<>'
121121
sp_after_angle = force # ignore/add/remove/force
122122

123123
# Add or remove space before '(' of 'if', 'for', 'switch', and 'while'
124-
sp_before_sparen = remove # ignore/add/remove/force
124+
sp_before_sparen = add # ignore/add/remove/force
125125

126126
# Add or remove space inside if-condition '(' and ')'
127127
sp_inside_sparen = remove # ignore/add/remove/force
128128

129129
# Add or remove space after ')' of 'if', 'for', 'switch', and 'while'
130-
sp_after_sparen = remove # ignore/add/remove/force
130+
sp_after_sparen = add # ignore/add/remove/force
131131

132132
# Add or remove space between ')' and '{' of 'if', 'for', 'switch', and 'while'
133-
sp_sparen_brace = remove # ignore/add/remove/force
133+
sp_sparen_brace = add # ignore/add/remove/force
134134

135135
# Add or remove space before empty statement ';' on 'if', 'for' and 'while'
136136
sp_special_semi = ignore # ignore/add/remove/force
@@ -211,10 +211,10 @@ sp_macro = ignore # ignore/add/remove/force
211211
sp_macro_func = ignore # ignore/add/remove/force
212212

213213
# Add or remove space between 'else' and '{' if on the same line
214-
sp_else_brace = remove # ignore/add/remove/force
214+
sp_else_brace = add # ignore/add/remove/force
215215

216216
# Add or remove space between '}' and 'else' if on the same line
217-
sp_brace_else = remove # ignore/add/remove/force
217+
sp_brace_else = add # ignore/add/remove/force
218218

219219
#
220220
# Code alignment (not left column spaces/tabs)
@@ -336,7 +336,7 @@ nl_union_brace = add # ignore/add/remove/force
336336
nl_if_brace = remove # ignore/add/remove/force
337337

338338
# Add or remove newline between '}' and 'else'
339-
nl_brace_else = force # ignore/add/remove/force
339+
nl_brace_else = remove # ignore/add/remove/force
340340

341341
# Add or remove newline between two open or close braces.
342342
# Due to general newline/brace handling, REMOVE may not work.

Library/CppLang/Generator.cs

Lines changed: 139 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -165,71 +165,162 @@ void ProcessClasses(YNamespace @namespace, NamespaceDeclarationSyntax inputNames
165165

166166
// Body:
167167

168-
var block = new YBlock();
168+
method.Visibility = inputMethod.Modifiers.GetYVisibility();
169+
method.Body = ProcessStatement(inputMethod.Body);
170+
@class.AddChild(method);
171+
}
172+
}
173+
}
169174

170-
foreach (var s in inputMethod.Body.Statements) {
171-
if (s is ReturnStatementSyntax) { // return
172-
var returnStatement = (ReturnStatementSyntax)s;
173-
YExpr returnExpression = null;
175+
// todo here could be generics or reflection
174176

175-
if (returnStatement.Expression is IdentifierNameSyntax) {
176-
returnExpression = new YIdentifierExpr((IdentifierNameSyntax)returnStatement.Expression);
177-
} else {
178-
//throw new TUnsupportedException();
179-
}
177+
YStatement ProcessStatement(StatementSyntax statement)
178+
{
179+
if (statement is IfStatementSyntax) {
180+
return ProcessStatement((IfStatementSyntax)statement);
181+
} else if (statement is BlockSyntax) {
182+
return ProcessStatement((BlockSyntax)statement);
183+
} else if (statement is ReturnStatementSyntax) {
184+
return ProcessStatement((ReturnStatementSyntax)statement);
185+
} else if (statement is ExpressionStatementSyntax) {
186+
return ProcessStatement((ExpressionStatementSyntax)statement);
187+
}
180188

181-
if (returnExpression != null) {
182-
block.Statements.Add(new YReturn(returnExpression));
183-
}
189+
throw new TException("Unable to process statement");
190+
}
184191

185-
} else if (s is ExpressionStatementSyntax) { // assignment
186-
var expressionStatement = (ExpressionStatementSyntax)s;
192+
YExpr ProcessExpr(ExpressionSyntax expr)
193+
{
194+
if (expr is BinaryExpressionSyntax) {
195+
return ProcessExpr((BinaryExpressionSyntax)expr);
196+
} else if (expr is IdentifierNameSyntax) {
197+
return ProcessExpr((IdentifierNameSyntax)expr);
198+
} else if (expr is LiteralExpressionSyntax) {
199+
return ProcessExpr((LiteralExpressionSyntax)expr);
200+
} else if (expr is PrefixUnaryExpressionSyntax) {
201+
return ProcessExpr((PrefixUnaryExpressionSyntax)expr);
202+
} else if (expr is InvocationExpressionSyntax) {
203+
return ProcessExpr((InvocationExpressionSyntax)expr);
204+
} else if (expr is MemberAccessExpressionSyntax) {
205+
return ProcessExpr((MemberAccessExpressionSyntax)expr);
206+
} else if (expr is ThisExpressionSyntax) {
207+
return ProcessExpr((ThisExpressionSyntax)expr);
208+
}
187209

188-
if (expressionStatement.Expression is AssignmentExpressionSyntax) {
189-
var assignmentExpression = (AssignmentExpressionSyntax)expressionStatement.Expression;
210+
throw new TException("Unable to process expression");
211+
}
190212

191-
YExpr left = null;
213+
YExpr ProcessExpr(IdentifierNameSyntax expr)
214+
{
215+
return new YIdentifierExpr(expr);
216+
}
192217

193-
if (assignmentExpression.Left is MemberAccessExpressionSyntax) {
194-
var memberAccessExpression = (MemberAccessExpressionSyntax)assignmentExpression.Left;
218+
YExpr ProcessExpr(LiteralExpressionSyntax expr)
219+
{
220+
if (expr.Token.IsKind(SyntaxKind.NullKeyword)) {
221+
return YLiteralExpr.Null;
222+
} else if (expr.Token.IsKind(SyntaxKind.NumericLiteralToken)) {
223+
return new YLiteralExpr(expr.Token.Value); // looks same as YConstExpr
224+
}
195225

196-
YExpr expr = null;
226+
throw new TException("Unable to process expr");
227+
}
197228

198-
if (memberAccessExpression.Expression is ThisExpressionSyntax) {
199-
expr = YExpr.This;
200-
}
229+
YExpr ProcessExpr(PrefixUnaryExpressionSyntax expr)
230+
{
231+
var operand = ProcessExpr(expr.Operand);
232+
var @operator = ProcessOperator(expr.OperatorToken);
201233

202-
left = new YMemberAccessExpr(
203-
expr,
204-
memberAccessExpression.Name.Identifier.ToString()
205-
);
206-
} else if (assignmentExpression.Left is IdentifierNameSyntax) {
207-
left = new YIdentifierExpr((IdentifierNameSyntax)assignmentExpression.Left);
208-
}
234+
return new YPrefixUnaryExpr(@operator, operand);
235+
}
209236

210-
YExpr right = null;
237+
YExpr ProcessExpr(MemberAccessExpressionSyntax memberAccessExpression)
238+
{
239+
YExpr expr = ProcessExpr(memberAccessExpression.Expression);
211240

212-
if (assignmentExpression.Right is IdentifierNameSyntax) {
213-
right = new YIdentifierExpr((IdentifierNameSyntax)assignmentExpression.Right);
214-
}
241+
return new YMemberAccessExpr(
242+
expr,
243+
memberAccessExpression.Name.Identifier.ToString()
244+
);
245+
}
215246

216-
if (left != null && right != null) {
217-
block.Statements.Add(new YAssign(left, right));
218-
}
219-
} else {
220-
//throw new TUnsupportedException();
221-
}
247+
YExpr ProcessExpr(ThisExpressionSyntax expr)
248+
{
249+
return YExpr.This;
250+
}
222251

223-
} else {
224-
//throw new TUnsupportedException();
225-
}
226-
}
252+
YExpr ProcessExpr(InvocationExpressionSyntax expr)
253+
{
254+
if (expr.ArgumentList.ChildNodes().Count() > 0) {
255+
throw new TException("Unable to process expression");
256+
}
227257

228-
method.Visibility = inputMethod.Modifiers.GetYVisibility();
229-
method.Body = block;
230-
@class.AddChild(method);
258+
return new YInvocation(ProcessExpr(expr.Expression));
259+
}
260+
261+
YExpr ProcessExpr(BinaryExpressionSyntax binaryExpression)
262+
{
263+
var left = ProcessExpr(binaryExpression.Left);
264+
var right = ProcessExpr(binaryExpression.Right);
265+
var operation = ProcessOperator(binaryExpression.OperatorToken);
266+
267+
return new YBinaryExpr(left, right, operation);
268+
}
269+
270+
YStatement ProcessStatement(IfStatementSyntax ifStatement)
271+
{
272+
YExpr condition = ProcessExpr(ifStatement.Condition);
273+
YStatement statement = ProcessStatement(ifStatement.Statement);
274+
YStatement elseStatement = ProcessStatement(ifStatement.Else.Statement);
275+
276+
if (condition != null && statement != null) {
277+
return new YIf(condition, statement, elseStatement);
278+
}
279+
280+
throw new TException("Unable to process statement");
281+
}
282+
283+
YStatement ProcessStatement(ReturnStatementSyntax statement)
284+
{
285+
return new YReturn(ProcessExpr(statement.Expression));
286+
}
287+
288+
YStatement ProcessStatement(BlockSyntax statement)
289+
{
290+
var block = new YBlock();
291+
292+
foreach (var s in statement.Statements) {
293+
block.Statements.Add(ProcessStatement(s));
294+
}
295+
296+
return block;
297+
}
298+
299+
YStatement ProcessStatement(ExpressionStatementSyntax statement)
300+
{
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?!
231309
}
232310
}
311+
312+
throw new TException("Unable to process statement");
313+
}
314+
315+
YOperator ProcessOperator(SyntaxToken token)
316+
{
317+
if (token.IsKind(SyntaxKind.EqualsEqualsToken)) {
318+
return YOperator.EqualsEquals;
319+
} else if (token.IsKind(SyntaxKind.MinusToken)) {
320+
return YOperator.Minus;
321+
}
322+
323+
throw new TException("Unable to process opeartion");
233324
}
234325
}
235326

Library/CppLang/SourceCompiler.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ static public void AppendEx(this StringBuilder builder, YStatement statement)
131131
builder.Append("=");
132132
AppendEx(builder, assign.Right);
133133
builder.Append(";");
134+
} else if (statement is YIf) {
135+
var @if = (YIf)statement;
136+
137+
builder.Append("if (");
138+
AppendEx(builder, @if.Condition);
139+
builder.Append(") ");
140+
AppendEx(builder, @if.Statement);
141+
if (@if.ElseStatement != null) {
142+
builder.Append(" else ");
143+
AppendEx(builder, @if.ElseStatement);
144+
}
134145
}
135146
}
136147

@@ -151,8 +162,43 @@ static public void AppendEx(this StringBuilder builder, YExpr expr)
151162
builder.Append(memberAccess.Name);
152163
} else if (expr is YIdentifierExpr) {
153164
builder.Append(((YIdentifierExpr)expr).Name);
165+
} else if (expr is YBinaryExpr) {
166+
var binary = (YBinaryExpr)expr;
167+
168+
AppendEx(builder, binary.Left);
169+
builder.Append(" ");
170+
AppendEx(builder, binary.Operator);
171+
builder.Append(" ");
172+
AppendEx(builder, binary.Right);
173+
} else if (expr is YLiteralExpr) {
174+
var literal = (YLiteralExpr)expr;
175+
if (literal == YLiteralExpr.Null) {
176+
builder.Append("nullptr");
177+
} else {
178+
builder.Append(literal.Value);
179+
}
180+
} else if (expr is YPrefixUnaryExpr) {
181+
var prefixUnari = (YPrefixUnaryExpr)expr;
182+
183+
AppendEx(builder, prefixUnari.Operator);
184+
AppendEx(builder, prefixUnari.Operand);
185+
} else if (expr is YInvocation) {
186+
var invocation = (YInvocation)expr;
187+
188+
AppendEx(builder, invocation.Expression);
189+
190+
// no arguments
191+
builder.Append("()");
154192
}
155193
}
156194

195+
static public void AppendEx(this StringBuilder builder, YOperator op)
196+
{
197+
if (op == YOperator.EqualsEquals) {
198+
builder.Append("==");
199+
} else if (op == YOperator.Minus) {
200+
builder.Append("-");
201+
}
202+
}
157203
}
158204
}

Library/CppLang/TypeMapper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,15 @@ public string ValueOf(YType type)
3737
} else if (type is YRefType) {
3838
var name = ((YRefType)type).Name;
3939

40-
include = _includeFinder.FindInclude(name);
40+
include = _includeFinder.FindInclude("shared_ptr");
4141
if (include != null) {
4242
_includes.Add(include);
43-
return $"std::shared_ptr<{name}>";
43+
44+
include = _includeFinder.FindInclude(name);
45+
if (include != null) {
46+
_includes.Add(include);
47+
return $"std::shared_ptr<{name}>";
48+
}
4449
}
4550

4651
throw new TException("Can't find include for referenced type");

Library/CppLang/UnitCompiler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public string FindInclude(string type)
8080
// No check for accessibility: if the type is visible from the specific class or not.
8181

8282
switch (type) {
83+
case "shared_ptr":
84+
return "<memory>";
8385
case "int32_t":
8486
return "<cstdint>";
8587
default:

Library/Library.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
<Compile Include="YLang\YDestructor.cs" />
8686
<Compile Include="TUnsupportedException.cs" />
8787
<Compile Include="YLang\Types\YRefType.cs" />
88+
<Compile Include="YLang\Operations\YOperator.cs" />
89+
<Compile Include="YLang\Statements\YIf.cs" />
90+
<Compile Include="YLang\Expresions\YBinaryExpr.cs" />
91+
<Compile Include="YLang\Expresions\YLiteralExpr.cs" />
92+
<Compile Include="YLang\Expresions\YPrefixUnaryExpr.cs" />
93+
<Compile Include="YLang\Expresions\YInvocation.cs" />
8894
</ItemGroup>
8995
<ItemGroup>
9096
<None Include="packages.config" />
@@ -95,6 +101,7 @@
95101
<Folder Include="YLang\Expresions\" />
96102
<Folder Include="YLang\Types\" />
97103
<Folder Include="YLang\Statements\" />
104+
<Folder Include="YLang\Operations\" />
98105
</ItemGroup>
99106
<ItemGroup>
100107
<EmbeddedResource Include="Configs\uncrustify.cfg" />

0 commit comments

Comments
 (0)