66import ko .carbonel .compiler .intermediate .tinyRust .ts .ClassEntry ;
77import ko .carbonel .compiler .intermediate .tinyRust .ts .MethodEntry ;
88import ko .carbonel .compiler .intermediate .tinyRust .ts .VariableEntry ;
9+ import ko .carbonel .compiler .runner .TermFont ;
910
1011import java .util .List ;
1112import java .util .stream .Collectors ;
1213
14+ /**
15+ * Re-Generate the original source code from the TinyRustSymbolTable (ST and AST)
16+ * Known issues:
17+ * - Brace Exp does not generate braces, Example ast/33.rs
18+ * > Possible solution: Generate braces for all chaining expressions
19+ */
1320public class DeCompiler {
1421 private DeCompiler () {
1522 throw new IllegalStateException ("Utility class" );
@@ -18,76 +25,74 @@ private DeCompiler() {
1825 public static String decompile (TinyRustSymbolTable tinyRustSymbolTable ) {
1926 TinyRustSymbolTable temp = new TinyRustSymbolTable ();
2027 String classes = tinyRustSymbolTable .classEntries .stream ()
21- .filter (it -> temp .classEntries .stream ().noneMatch (primitive -> primitive .name .lexeme ().equals (it .name .lexeme ())))
28+ .filter (it -> temp .classEntries .stream ().noneMatch (primitive -> primitive .name .lexeme ().equals (it .name .lexeme ())))
2229 .map (DeCompiler ::decompileClass ).collect (Collectors .joining ("\n " ));
2330 String main = decompileMain (tinyRustSymbolTable .main );
2431 return classes + "\n " + main ;
2532 }
2633
2734 private static String decompileMain (MethodEntry main ) {
2835 String out = "" ;
29- out += "fn " + main .name .lexeme () + "(" ;
36+ out += TermFont . code_reserved ( "fn" ) + " " + TermFont . code_attr_m ( main .name .lexeme () ) + "(" ;
3037 out += main .parameters .stream ().map (DeCompiler ::decompileArgument ).collect (Collectors .joining (", " ));
3138 out += ")" ;
32- out += decompileStatement (main .block , main .variables );
39+ out += decompileStatement (main .block , main .variables , 1 );
3340 return out ;
3441 }
3542
36-
3743 private static String decompileMethod (MethodEntry method ) {
38- String out = "" ;
39- if (method .name .lexeme ().equals (MethodEntry .CONSTRUCTOR )){
40- out += method .name .lexeme () + "(" ;
44+ String out = "\t " ;
45+ if (method .name .lexeme ().equals (MethodEntry .CONSTRUCTOR )) {
46+ out += TermFont . code_attr_m ( method .name .lexeme () ) + "(" ;
4147 out += method .parameters .stream ().map (DeCompiler ::decompileArgument ).collect (Collectors .joining (", " ));
42- out += ")" ;
43- }else {
44- if (method .isStatic ) out += "static " ;
45- out += "fn " + method .name .lexeme () + "(" ;
48+ out += ") " ;
49+ } else {
50+ if (method .isStatic ) out += TermFont . code_reserved ( "static" ) + " " ;
51+ out += TermFont . code_reserved ( "fn" ) + " " + TermFont . code_attr_m ( method .name .lexeme () ) + "(" ;
4652 out += method .parameters .stream ().map (DeCompiler ::decompileArgument ).collect (Collectors .joining (", " ));
47- out += ") -> " + method .returnType .type .lexeme ();
53+ out += ") -> " + TermFont . code_type ( method .returnType .type .lexeme ()) + " " ;
4854 }
49- out += decompileStatement (method .block , method .variables );
55+ out += decompileStatement (method .block , method .variables , 2 );
5056 return out ;
5157 }
5258
53- private static String decompileStatement (BlockStatementEntry statementEntry , List <VariableEntry > vars ) {
59+ private static String decompileStatement (BlockStatementEntry statementEntry , List <VariableEntry > vars , int depth ) {
5460 String out = "" ;
61+ String indent = "\t " .repeat (depth );
62+ String indent1 = "\t " .repeat (depth - 1 );
5563 out += "{\n " ;
56- out += vars .stream ().map (DeCompiler ::decompileVariable ).collect (Collectors .joining ("\n " ));
57- out += "\n " ;
58- out += statementEntry .statements .stream ().map (DeCompiler ::decompileStatement ).collect (Collectors .joining ("\n " ));
59- out += "\n }" ;
64+ out += vars .isEmpty () ? "" : vars .stream ().map (DeCompiler ::decompileVariable ).collect (Collectors .joining ("\n " + indent , indent , "\n " ));
65+ out += indent + statementEntry .statements .stream ().map (it -> decompileStatement (it , depth + 1 )).collect (Collectors .joining ("\n " + indent ));
66+ out += "\n " + indent1 + "}" ;
6067 return out ;
6168 }
6269
63- private static String decompileVariable (VariableEntry variableEntry ) {
64- return (variableEntry .type .isArray ? "Array " : "" ) + variableEntry .type .type .lexeme ()+ ": " + variableEntry .name .lexeme () + ";" ;
65- }
66-
67- private static String decompileStatement (StatementEntry statementEntry ) {
70+ private static String decompileStatement (StatementEntry statementEntry , int depth ) {
6871 String out = "" ;
72+ String indent = "\t " .repeat (depth );
73+ String indent1 = "\t " .repeat (depth - 1 );
6974 if (statementEntry instanceof EmptyStatementEntry ) {
7075 out += ";" ;
7176 } else if (statementEntry instanceof ExpressionStatementEntry exp ) {
7277 out += "(" + decompileExpression (exp .expression ) + ");" ;
7378 } else if (statementEntry instanceof BlockStatementEntry blk ) {
7479 out += "{\n " ;
75- out += blk .statements .stream ().map (DeCompiler :: decompileStatement ) .collect (Collectors .joining ("\n " ));
76- out += "\n }" ;
80+ out += indent + blk .statements .stream ().map (it -> decompileStatement ( it , depth + 1 )) .collect (Collectors .joining ("\n " + indent ));
81+ out += "\n " + indent1 + " }" ;
7782 } else if (statementEntry instanceof IfStatementEntry iF ) {
78- out += "if (" + decompileExpression (iF .ifCondition ) + ")\n " ;
79- out += decompileStatement (iF .ifBody );
83+ out += TermFont . code_reserved ( "if" ) + " (" + decompileExpression (iF .ifCondition ) + ") " ;
84+ out += decompileStatement (iF .ifBody , depth );
8085 if (iF .elseBody != null ) {
81- out += " else\n " ;
82- out += decompileStatement (iF .elseBody );
86+ out += " " + TermFont . code_reserved ( " else" ) + " " ;
87+ out += decompileStatement (iF .elseBody , depth );
8388 }
8489 } else if (statementEntry instanceof WhileStatementEntry whl ) {
85- out += "while (" + decompileExpression (whl .whileCondition ) + ")\n " ;
86- out += decompileStatement (whl .whileBody );
90+ out += TermFont . code_reserved ( "while" ) + " (" + decompileExpression (whl .whileCondition ) + ") " ;
91+ out += decompileStatement (whl .whileBody , depth );
8792 } else if (statementEntry instanceof ReturnStatementEntry ret ) {
88- out += "return " + decompileExpression (ret .returnExpression ) + ";" ;
93+ out += TermFont . code_reserved ( "return" ) + " " + decompileExpression (ret .returnExpression ) + ";" ;
8994 } else if (statementEntry instanceof AssignStatementEntry as ) {
90- out += decompileExpression (as .lhs ()) + "= " + decompileExpression (as .rhs ()) + ";" ;
95+ out += decompileExpression (as .lhs ()) + " " + TermFont . code_operator ( "=" ) + " " + decompileExpression (as .rhs ()) + ";" ;
9196 } else throw new RuntimeException ("Unknown statement type " + statementEntry .getClass ().getName ());
9297 return out ;
9398 }
@@ -97,49 +102,53 @@ private static String decompileExpression(ExpressionEntry expression) {
97102 if (expression instanceof UnaryExpressionEntry un ) {
98103 out += un .operator .lexeme () + decompileExpression (un .expression );
99104 } else if (expression instanceof LiteralExpressionEntry lit ) {
100- out += lit .literal .lexeme ();
105+ out += TermFont . code_literal ( lit .literal .lexeme () );
101106 } else if (expression instanceof BinaryExpressionEntry bin ) {
102- out += "(" + decompileExpression (bin .left ) + " " + bin .operator .lexeme () + " " + decompileExpression (bin .right ) + ")" ;
107+ out += "(" + decompileExpression (bin .left ) + " " + TermFont . code_operator ( bin .operator .lexeme () ) + " " + decompileExpression (bin .right ) + ")" ;
103108 } else if (expression instanceof MethodCallExpressionEntry call ) {
104- out += call .methodName .lexeme () + "(" ;
109+ out += TermFont . code_attr_m ( call .methodName .lexeme () ) + "(" ;
105110 out += call .arguments .stream ().map (DeCompiler ::decompileExpression ).collect (Collectors .joining (", " ));
106111 out += ")" ;
107112 } else if (expression instanceof NewClassExpressionEntry newExp ) {
108- out += "new " + newExp .className .lexeme () + "(" ;
113+ out += TermFont . code_reserved ( "new" ) + " " + TermFont . code_type ( newExp .className .lexeme () ) + "(" ;
109114 out += newExp .constructorArguments .stream ().map (DeCompiler ::decompileExpression ).collect (Collectors .joining (", " ));
110115 out += ")" ;
111116 } else if (expression instanceof ArrayAccessExpressionEntry array ) {
112117 out += decompileExpression (array .array ) + "[" + decompileExpression (array .index ) + "]" ;
113118 } else if (expression instanceof FieldAccessEntry field ) {
114- out += field .fieldName .lexeme ();
119+ out += TermFont . code_attr_f ( field .fieldName .lexeme () );
115120 } else if (expression instanceof ChainedExpressionEntry chain ) {
116121 out += decompileExpression (chain .base ) + "." + decompileExpression (chain .chain );
117122 } else if (expression instanceof NewArrayExpressionEntry array ) {
118- out += "new " + array .type .lexeme () + "[" + decompileExpression (array .size ) + "]" ;
123+ out += TermFont . code_reserved ( "new" ) + " " + TermFont . code_type ( array .type .lexeme () ) + "[" + decompileExpression (array .size ) + "]" ;
119124 } else if (expression instanceof ClassRefEntry cls ) {
120- out += cls .name .lexeme ();
125+ out += TermFont . code_type ( cls .name .lexeme () );
121126 } else throw new RuntimeException ("Unknown expression type " + expression .getClass ().getName ());
122127 return out ;
123128 }
124129
125130 private static String decompileClass (ClassEntry clazz ) {
126- String out = "class " + clazz .name .lexeme ();
127- if (clazz .superClass != null ) out += " : " + clazz .superClass .type .lexeme ();
131+ String out = TermFont . code_reserved ( "class" ) + " " + clazz .name .lexeme ();
132+ if (clazz .superClass != null ) out += " : " + TermFont . code_type ( clazz .superClass .type .lexeme () );
128133 out += " {\n " ;
129- out += clazz .fields .stream ().map (DeCompiler ::decompileField ).collect (Collectors .joining ("\n " ));
134+ out += clazz .fields .stream ().map (DeCompiler ::decompileField ).collect (Collectors .joining ("\n " )) + " \n " ;
130135 out += clazz .methods .stream ().map (DeCompiler ::decompileMethod ).collect (Collectors .joining ("\n " ));
131136 out += "\n }\n " ;
132137 return out ;
133138 }
134139
135140 private static String decompileField (VariableEntry variableEntry ) {
136- String out = "" ;
137- if (variableEntry .isPublic ) out += "pub " ;
138- out += (variableEntry .type .isArray ? "Array " : "" ) + variableEntry .type .type .lexeme () + ": " + variableEntry .name .lexeme ();
141+ String out = "\t " ;
142+ if (variableEntry .isPublic ) out += TermFont . code_reserved ( "pub" ) + " " ;
143+ out += (variableEntry .type .isArray ? TermFont . code_reserved ( "Array" ) + " " : "" ) + ( TermFont . code_type ( variableEntry .type .type .lexeme ()) + ": " + TermFont . code_attr_f ( variableEntry .name .lexeme ()) );
139144 return out + ";" ;
140145 }
141146
147+ private static String decompileVariable (VariableEntry variableEntry ) {
148+ return (variableEntry .type .isArray ? TermFont .code_reserved ("Array" ) + " " : "" ) + (TermFont .code_type (variableEntry .type .type .lexeme ()) + ": " + TermFont .code_attr_f (variableEntry .name .lexeme ())) + ";" ;
149+ }
150+
142151 private static String decompileArgument (VariableEntry variableEntry ) {
143- return (variableEntry .type .isArray ? "Array " : "" ) + (variableEntry .type .type .lexeme () + ": " + variableEntry .name .lexeme ());
152+ return (variableEntry .type .isArray ? TermFont . code_reserved ( "Array" ) + " " : "" ) + (TermFont . code_type ( variableEntry .type .type .lexeme ()) + ": " + TermFont . code_attr_f ( variableEntry .name .lexeme () ));
144153 }
145154}
0 commit comments