Skip to content

Commit

Permalink
Initial draft of CSPrinter + printing components architecture (simi…
Browse files Browse the repository at this point in the history
…lar to CSCompiler and its components)
  • Loading branch information
jeremyfa committed Apr 14, 2024
1 parent 2dd8ab2 commit 3321c36
Show file tree
Hide file tree
Showing 22 changed files with 533 additions and 155 deletions.
10 changes: 5 additions & 5 deletions src/cscompiler/CSCompiler.hx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package cscompiler;

import cscompiler.ast.CSTypePath;
import cscompiler.ast.CSExpr;
import cscompiler.ast.CSArg;
import cscompiler.ast.CSTopLevel;
import reflaxe.optimization.ExprOptimizer;
#if (macro || cs_runtime)

import sys.io.File;
Expand All @@ -23,6 +18,7 @@ import reflaxe.data.EnumOptionData;
import reflaxe.helpers.Context;
import reflaxe.output.DataAndFileInfo;
import reflaxe.output.StringOrBytes;
import reflaxe.optimization.ExprOptimizer;

using reflaxe.helpers.SyntaxHelper;
using reflaxe.helpers.ModuleTypeHelper;
Expand All @@ -33,6 +29,10 @@ using reflaxe.helpers.TypeHelper;

// ---

import cscompiler.ast.CSTypePath;
import cscompiler.ast.CSExpr;
import cscompiler.ast.CSArg;
import cscompiler.ast.CSTopLevel;
import cscompiler.ast.CSClass;
import cscompiler.ast.CSEnum;
import cscompiler.ast.CSStatement;
Expand Down
98 changes: 98 additions & 0 deletions src/cscompiler/CSPrinter.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cscompiler;

#if (macro || cs_runtime)

import cscompiler.helpers.Printer;
import cscompiler.ast.*;
import cscompiler.components.CSPrinter_Type;
import cscompiler.components.CSPrinter_Class;

/**
A class that prints actual C# code from a C# AST
**/
class CSPrinter extends Printer {

/**
Handles printing of C# types AST
**/
public var typePrinter(default, null): CSPrinter_Type;

/**
Handles printing of C# class AST
**/
public var classPrinter(default, null): CSPrinter_Class;

/**
Constructor.
**/
public function new() {
super();
createComponents();
}

/**
Constructs all the components of the printer.
See the `cscompiler.components` package for more info.
**/
inline function createComponents() {
// Bypass Haxe null-safety not allowing `this` usage.
@:nullSafety(Off) var self = this;

typePrinter = new CSPrinter_Type(self);
classPrinter = new CSPrinter_Class(self);
// TODO more printer components

}

public function printTopLevel(topLevel: CSTopLevel) {

if (topLevel.nameSpace != null) {
write('namespace ');
write(topLevel.nameSpace);
write(' {');
newline();
indent();
}

switch topLevel.def {
case CSTopLevelClass(c):
printClass(c);
case CSTopLevelEnum(e):
printEnum(e);
}

if (topLevel.nameSpace != null) {
unindent();
line('}');
}

}

inline public function printClass(cls: CSClass) {

classPrinter.printClass(cls);

}

public function printEnum(enm: CSEnum) {

// TODO

}

inline public function printType(type: CSType) {

typePrinter.printType(type);

}

inline public function printTypeParams(typeParams: Array<CSType>) {

typePrinter.printTypeParams(typeParams);

}

}

#end
2 changes: 2 additions & 0 deletions src/cscompiler/ast/CSArg.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cscompiler.ast;

#if (macro || cs_runtime)
import cscompiler.ast.CSExpr;

typedef CSArg = {
Expand All @@ -13,3 +14,4 @@ typedef CSArg = {
public var ?value(default, null):Null<CSExpr>;

}
#end
2 changes: 2 additions & 0 deletions src/cscompiler/ast/CSClass.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ package cscompiler.ast;
class CSClass {
public var name(default, null): String;

public var typeParams(default, null): Array<CSType> = [];

public var superClass(default, null): Null<CSTypePath> = null;

public var superClassTypeParams(default, null): Array<CSType> = [];
Expand Down
76 changes: 1 addition & 75 deletions src/cscompiler/ast/CSExpr.hx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cscompiler.ast;

import cscompiler.ast.CSTypePath;
#if (macro || cs_runtime)

import cscompiler.ast.CSTypePath;
import haxe.macro.Expr;
import haxe.macro.Type;

Expand All @@ -22,78 +22,4 @@ class CSExpr {
}
}

/**
TODO: Give each case a better description.
**/
enum CSExprDef {
/**
A constant.
**/
CSConst(constant: CSConstant);

/**
Reference to a local variable `varData`.
**/
CSLocalVar(varData: CSVar);

/**
Array access `baseExpr[indexExpr]`.
**/
CSArray(baseExpr: CSExpr, indexExpr: CSExpr);

/**
Binary operator `leftExpr op rightExpr`.
**/
// TODO Binop to CSBinop (because some operators are not supported by C#)
CSBinop(op: Binop, leftExpr: CSExpr, rightExpr: CSExpr);

/**
Field access on `e` of name `fieldName`.
**/
CSField(e: CSExpr, fieldAccess: CSFieldAccess);

/**
Reference to a C# type (class, enum...).
**/
CSTypeExpr(type: CSType);

/**
Parentheses `(e)`.
**/
CSParenthesis(e: CSExpr);

/**
An array declaration `{ expressions }`.
**/
CSArrayDecl(expressions: Array<CSExpr>);

/**
A call `baseExpr<typeParams>(arguments)`.
**/
CSCall(baseExpr: CSExpr, typeParams: Array<CSType>, arguments: Array<CSExpr>);

/**
A constructor call `new cls<typeParams>(arguments)`.
**/
CSNew(cls: CSTypePath, typeParams: Array<CSType>, arguments: Array<CSExpr>);

/**
An unary operator `op` on `baseExpr`.
TODO:
Is postfix necessary?
**/
CSUnop(op: Unop, postFix: Bool, baseExpr: CSExpr);

/**
A function declaration.
**/
CSFunctionExpr(tfunc: CSFunction);

/**
An unknown identifier.
**/
CSIdent(s: String);
}

#end
82 changes: 82 additions & 0 deletions src/cscompiler/ast/CSExprDef.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cscompiler.ast;

#if (macro || cs_runtime)

import haxe.macro.Expr;
import haxe.macro.Type;

/**
TODO: Give each case a better description.
**/
enum CSExprDef {
/**
A constant.
**/
CSConst(constant: CSConstant);

/**
Reference to a local variable `varData`.
**/
CSLocalVar(varData: CSVar);

/**
Array access `baseExpr[indexExpr]`.
**/
CSArray(baseExpr: CSExpr, indexExpr: CSExpr);

/**
Binary operator `leftExpr op rightExpr`.
**/
// TODO Binop to CSBinop (because some operators are not supported by C#)
CSBinop(op: Binop, leftExpr: CSExpr, rightExpr: CSExpr);

/**
Field access on `e` of name `fieldName`.
**/
CSField(e: CSExpr, fieldAccess: CSFieldAccess);

/**
Reference to a C# type (class, enum...).
**/
CSTypeExpr(type: CSType);

/**
Parentheses `(e)`.
**/
CSParenthesis(e: CSExpr);

/**
An array declaration `{ expressions }`.
**/
CSArrayDecl(expressions: Array<CSExpr>);

/**
A call `baseExpr<typeParams>(arguments)`.
**/
CSCall(baseExpr: CSExpr, typeParams: Array<CSType>, arguments: Array<CSExpr>);

/**
A constructor call `new cls<typeParams>(arguments)`.
**/
CSNew(cls: CSTypePath, typeParams: Array<CSType>, arguments: Array<CSExpr>);

/**
An unary operator `op` on `baseExpr`.
TODO:
Is postfix necessary?
**/
CSUnop(op: Unop, postFix: Bool, baseExpr: CSExpr);

/**
A function declaration.
**/
CSFunctionExpr(tfunc: CSFunction);

/**
An unknown identifier.
**/
CSIdent(s: String);
}

#end
4 changes: 4 additions & 0 deletions src/cscompiler/ast/CSField.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cscompiler.ast;

#if (macro || cs_runtime)

@:structInit
class CSField {
/**
Expand All @@ -17,3 +19,5 @@ class CSField {
**/
public var kind:CSFieldKind;
}

#end
4 changes: 4 additions & 0 deletions src/cscompiler/ast/CSFieldAccess.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cscompiler.ast;

#if (macro || cs_runtime)

import cscompiler.ast.CSTypePath;

enum CSFieldAccess {
Expand All @@ -17,3 +19,5 @@ enum CSFieldAccess {
CSFStatic(c:CSTypePath, params:Array<CSType>, cf:String);

}

#end
4 changes: 4 additions & 0 deletions src/cscompiler/ast/CSFieldKind.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cscompiler.ast;

#if (macro || cs_runtime)

import cscompiler.ast.CSExpr;
import cscompiler.ast.CSStatement;
import cscompiler.ast.CSFunction;
Expand All @@ -14,3 +16,5 @@ enum CSFieldKind {
CSProp(type:CSType, get:Null<CSStatement>, set:Null<CSStatement>);

}

#end
6 changes: 5 additions & 1 deletion src/cscompiler/ast/CSModifier.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cscompiler.ast;

#if (macro || cs_runtime)

enum CSModifier {

CSStatic;
Expand All @@ -18,4 +20,6 @@ enum CSModifier {

CSInternal;

}
}

#end
Loading

0 comments on commit 3321c36

Please sign in to comment.