-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial draft of
CSPrinter
+ printing components architecture (simi…
…lar to CSCompiler and its components)
- Loading branch information
Showing
22 changed files
with
533 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.