Skip to content

Commit 3ad5578

Browse files
committed
Move Printer components to printer/ and fix null-safety issues
1 parent 5ec7b8f commit 3ad5578

17 files changed

+393
-172
lines changed

src/cscompiler/CSCompiler.hx

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package cscompiler;
22

33
#if(macro || cs_runtime)
4-
import sys.io.File;
4+
import cscompiler.ast.CSTypePath;
5+
import cscompiler.ast.CSExpr;
6+
import cscompiler.ast.CSArg;
7+
import cscompiler.ast.CSTopLevel;
8+
import cscompiler.ast.CSClass;
9+
import cscompiler.ast.CSEnum;
10+
import cscompiler.ast.CSStatement;
11+
import cscompiler.ast.CSType;
12+
import cscompiler.components.*;
13+
import cscompiler.config.Define;
514

615
import haxe.io.Path;
716
import haxe.macro.Expr;
817
import haxe.macro.Type;
918

10-
// ---
19+
import sys.io.File;
20+
1121
import reflaxe.BaseCompiler;
1222
import reflaxe.GenericCompiler;
1323
import reflaxe.data.ClassVarData;
@@ -17,25 +27,15 @@ import reflaxe.helpers.Context;
1727
import reflaxe.output.DataAndFileInfo;
1828
import reflaxe.output.StringOrBytes;
1929

20-
using reflaxe.helpers.SyntaxHelper;
30+
// ---
2131
using reflaxe.helpers.ModuleTypeHelper;
2232
using reflaxe.helpers.NameMetaHelper;
2333
using reflaxe.helpers.NullableMetaAccessHelper;
34+
using reflaxe.helpers.NullHelper;
2435
using reflaxe.helpers.OperatorHelper;
36+
using reflaxe.helpers.SyntaxHelper;
2537
using reflaxe.helpers.TypeHelper;
2638

27-
// ---
28-
import cscompiler.ast.CSTypePath;
29-
import cscompiler.ast.CSExpr;
30-
import cscompiler.ast.CSArg;
31-
import cscompiler.ast.CSTopLevel;
32-
import cscompiler.ast.CSClass;
33-
import cscompiler.ast.CSEnum;
34-
import cscompiler.ast.CSStatement;
35-
import cscompiler.ast.CSType;
36-
import cscompiler.components.*;
37-
import cscompiler.config.Define;
38-
3939
/**
4040
The class that manages the generation of the C# code.
4141
@@ -134,15 +134,17 @@ class CSCompiler extends reflaxe.GenericCompiler<CSTopLevel, CSTopLevel, CSState
134134
Store `args` to use with `Sys.args()` later.
135135
**/
136136
function haxeBootContent(csCode: String) {
137-
return StringTools.trim('
137+
return StringTools.trim(
138+
'
138139
namespace Haxe {
139140
class HaxeBoot {
140141
static void Main(string[] args) {
141142
${csCode};
142143
}
143144
}
144145
}
145-
');
146+
'
147+
);
146148
}
147149

148150
/**
@@ -171,7 +173,8 @@ namespace Haxe {
171173
Returns the default content of the .csproj file.
172174
**/
173175
function csProjDefaultContent() {
174-
return StringTools.trim('
176+
return StringTools.trim(
177+
'
175178
<Project Sdk="Microsoft.NET.Sdk">
176179

177180
<PropertyGroup>
@@ -183,7 +186,8 @@ namespace Haxe {
183186
</PropertyGroup>
184187

185188
</Project>
186-
');
189+
'
190+
);
187191
}
188192

189193
/**
@@ -198,31 +202,24 @@ namespace Haxe {
198202
TODO.
199203
**/
200204
public function generateOutputIterator(): Iterator<DataAndFileInfo<StringOrBytes>> {
201-
// TODO: Print all classes and enums using these vars from `GenericCompiler`:
202-
// var classes: Array<CSClass>
203-
// var enums: Array<CSEnum>
204-
205-
return {
206-
hasNext: () -> false,
207-
next: () -> {
208-
return new DataAndFileInfo(StringOrBytes.fromString(""), @:nullSafety(Off) null, null, null);
209-
}
210-
};
205+
return new CSOutputIterator(this);
211206
}
212207

213208
// ---
214209

215210
/**
216211
Generate the C# output given the Haxe class information.
217212
**/
218-
public function compileClassImpl(classType: ClassType, varFields: Array<ClassVarData>, funcFields: Array<ClassFuncData>): Null<CSTopLevel> {
213+
public function compileClassImpl(classType: ClassType, varFields: Array<ClassVarData>,
214+
funcFields: Array<ClassFuncData>): Null<CSTopLevel> {
219215
return classComp.compile(classType, varFields, funcFields);
220216
}
221217

222218
/**
223219
Generate the C# output given the Haxe enum information.
224220
**/
225-
public function compileEnumImpl(enumType: EnumType, options: Array<EnumOptionData>): Null<CSTopLevel> {
221+
public function compileEnumImpl(enumType: EnumType,
222+
options: Array<EnumOptionData>): Null<CSTopLevel> {
226223
return enumComp.compile(enumType, options);
227224
}
228225

@@ -257,14 +254,23 @@ namespace Haxe {
257254
return typeComp.compileClassName(classType);
258255
}
259256

257+
/**
258+
Get the name of the `EnumType` as it should appear in
259+
the C# output.
260+
**/
261+
public function compileEnumName(enumType: EnumType): String {
262+
return typeComp.compileEnumName(enumType);
263+
}
264+
260265
// ---
261266

262267
/**
263268
Generate the C# output for a function argument.
264269
265270
Note: it's possible for an argument to be optional but not have an `expr`.
266271
**/
267-
public function compileFunctionArgument(t: Type, name: String, pos: Position, optional: Bool, expr: Null<TypedExpr> = null): CSArg {
272+
public function compileFunctionArgument(t: Type, name: String, pos: Position, optional: Bool,
273+
expr: Null<TypedExpr> = null): CSArg {
268274
return {
269275
name: compileVarName(name),
270276
type: compileType(t, pos),
@@ -314,7 +320,7 @@ namespace Haxe {
314320
// a block, and make this method not needed anymore
315321

316322
final lines = s.split("\n");
317-
for (i in 0...lines.length) {
323+
for(i in 0...lines.length) {
318324
lines[i] = StringTools.rtrim(lines[i]);
319325
}
320326
return lines.join("\n");
@@ -327,8 +333,8 @@ namespace Haxe {
327333
**/
328334
public function nameToHash(name: String): Int {
329335
var h: Int = 0;
330-
for (i in 0...name.length) {
331-
h = 223 * h + name.charCodeAt(i);
336+
for(i in 0...name.length) {
337+
h = 223 * h + name.charCodeAt(i).trustMe();
332338
}
333339
h %= 0x1FFFFF7B;
334340

src/cscompiler/CSOutputIterator.hx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cscompiler;
2+
3+
#if(macro || cs_runtime)
4+
import reflaxe.output.DataAndFileInfo;
5+
import reflaxe.output.StringOrBytes;
6+
7+
import cscompiler.ast.CSTopLevel;
8+
9+
/**
10+
The iterator returned from `CSCompiler.generateOutputIterator`.
11+
12+
This iterates through all the `DataAndFileInfo<CSTopLevel>` objects and uses the `CSPrinter`
13+
to generate the output for each as a `DataAndFileInfo<StringOrBytes>`.
14+
**/
15+
@:access(cscompiler.CSCompiler)
16+
class CSOutputIterator {
17+
var compiler: CSCompiler;
18+
var printer: CSPrinter;
19+
20+
var index: Int;
21+
var maxIndex: Int;
22+
23+
public function new(compiler: CSCompiler) {
24+
this.compiler = compiler;
25+
printer = new CSPrinter();
26+
27+
index = 0;
28+
// TODO: Include typedefs and abstracts?
29+
maxIndex = compiler.classes.length + compiler.enums.length;
30+
}
31+
32+
public function hasNext() {
33+
return index < maxIndex;
34+
}
35+
36+
public function next(): DataAndFileInfo<StringOrBytes> {
37+
// TODO: Include typedefs and abstracts?
38+
final topLevelAst: DataAndFileInfo<CSTopLevel> = if(index < compiler.classes.length) {
39+
compiler.classes[index];
40+
} else {
41+
compiler.enums[index - compiler.classes.length];
42+
}
43+
44+
final printer = new CSPrinter();
45+
printer.printTopLevel(topLevelAst.data);
46+
return topLevelAst.withOutput(printer.toString());
47+
}
48+
}
49+
#end

src/cscompiler/CSPrinter.hx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package cscompiler;
33
#if(macro || cs_runtime)
44
import cscompiler.helpers.Printer;
55
import cscompiler.ast.*;
6-
import cscompiler.components.CSPrinter_Type;
7-
import cscompiler.components.CSPrinter_Class;
6+
import cscompiler.printer.CSPrinter_Type;
7+
import cscompiler.printer.CSPrinter_Class;
88

99
/**
1010
A class that prints actual C# code from a C# AST
@@ -52,10 +52,8 @@ class CSPrinter extends Printer {
5252
}
5353

5454
switch topLevel.def {
55-
case CSTopLevelClass(c):
56-
printClass(c);
57-
case CSTopLevelEnum(e):
58-
printEnum(e);
55+
case CSTopLevelClass(c): printClass(c);
56+
case CSTopLevelEnum(e): printEnum(e);
5957
}
6058

6159
if(topLevel.nameSpace != null) {
@@ -72,6 +70,10 @@ class CSPrinter extends Printer {
7270
// TODO
7371
}
7472

73+
public function printField(field: CSField) {
74+
// TODO
75+
}
76+
7577
inline public function printType(type: CSType) {
7678
typePrinter.printType(type);
7779
}

src/cscompiler/ast/CSClass.hx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cscompiler.ast;
22

33
#if(macro || cs_runtime)
4+
import haxe.macro.Type;
5+
46
/**
57
Represents a class in C#.
68

src/cscompiler/ast/CSEnum.hx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@ import haxe.macro.Type;
1111
@:structInit
1212
class CSEnum {
1313
public var name(default, null): String;
14-
15-
public var haxeType(default, null): Type;
1614
}
1715
#end

src/cscompiler/ast/CSExpr.hx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class CSExpr {
1515
public var haxeExpr(default, null): Null<TypedExpr> = null;
1616
public var type(default, null): Null<CSType> = null;
1717

18-
public function new(def: CSExprDef, haxeExpr: Null<TypedExpr> = null, type: Null<CSType> = null) {
18+
public function new(def: CSExprDef, haxeExpr: Null<TypedExpr> = null,
19+
type: Null<CSType> = null) {
1920
this.def = def;
2021
this.haxeExpr = haxeExpr;
2122
this.type = type;

src/cscompiler/ast/CSFunction.hx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ package cscompiler.ast;
88
class CSFunction {
99
public var args(default, null): Array<CSArg>;
1010

11-
public var ret(default, null): CSType;
11+
public var returnKind(default, null): CSFunctionReturnKind;
1212

1313
public var statement(default, null): Null<CSStatement> = null;
1414

15-
public function new(args: Array<CSArg>, ret: CSType, ?statement: CSStatement) {
15+
public function new(args: Array<CSArg>, returnKind: CSFunctionReturnKind,
16+
?statement: CSStatement) {
1617
this.args = args;
17-
this.ret = ret;
18+
this.returnKind = returnKind;
1819
this.statement = statement;
1920
}
21+
} enum CSFunctionReturnKind {
22+
23+
Constructor;
24+
ReturnVoid;
25+
ReturnType(returnType: CSType);
2026
}
2127
#end

src/cscompiler/ast/CSStatementDef.hx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,19 @@ enum CSStatementDef {
2626
/**
2727
C# Switch
2828
**/
29-
CSSwitch(subject: CSExpr, cases: Array<{value: CSExpr, content: Null<Array<CSStatement>>}>, edef: Null<Array<CSStatement>>);
29+
CSSwitch(subject: CSExpr, cases: Array<{
30+
value: CSExpr,
31+
content: Null<Array<CSStatement>>
32+
}>, edef: Null<Array<CSStatement>>);
3033

3134
/**
3235
C# Try/Catch
3336
**/
34-
CSTry(content: Array<CSStatement>, catches: Array<{name: String, type: CSType, content: Array<CSStatement>}>);
37+
CSTry(content: Array<CSStatement>, catches: Array<{
38+
name: String,
39+
type: CSType,
40+
content: Array<CSStatement>
41+
}>);
3542

3643
CSBreak;
3744

src/cscompiler/ast/CSType.hx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import haxe.macro.Type.Ref;
1515
**/
1616
enum CSType {
1717
/**
18-
Both haxe TInst and TEnum will be transpiled to this because
18+
Both haxe TInst and TEnum will be transpiled to this because
1919
Haxe enum instances will become C# class instances anyway
2020
**/
2121
CSInst(typePath: CSTypePath, params: Array<CSType>);
@@ -34,12 +34,17 @@ enum CSType {
3434
CSArray(typePath: CSTypePath, params: Array<CSType>);
3535

3636
/**
37-
Function type, that may be translated into
38-
an `Action<T1,T2,...>` or `Func<T1,T2,...>`
39-
when used as an object type.
37+
Function type that may be translated into `Func<T1,T2,...>` when
38+
used as an object type.
4039
**/
4140
CSFunction(args: Array<CSArg>, ret: CSType);
4241

42+
/**
43+
Function type with no return type that may be translated into an
44+
`Action<T1,T2,...>` when used as an object type.
45+
**/
46+
CSFunctionNoReturn(args: Array<CSArg>);
47+
4348
/**
4449
A C# value type (primitives like `int`, `bool`... or `struct` types) type. Optionally nullable (`int?` etc...)
4550
**/

0 commit comments

Comments
 (0)