@@ -15,25 +15,27 @@ const indentNum = 2
1515// A list of in-scope variables, for type-checking reasons
1616var inScopeVariables = make (map [string ]string )
1717
18+ // For do-while loops, where the location of the "while" condition occurs after the "do" block
19+ var lastWhileCondition codeparser.LineTyper
20+
1821func ClearInScopeVariables () {
1922 inScopeVariables = make (map [string ]string )
2023}
2124
2225// NewClass is set to true if the class is a nested class
23- func ParseFile (sourceFile parsing.ParsedClasses , newClass bool ) string {
26+ func ParseFile (sourceFile parsing.ParsedClasses , newClass bool , filename string ) string {
2427 var generated string
2528 if newClass {
26- generated += fmt .Sprintf ("package main \n \n " )
29+ generated += fmt .Sprintf ("package %s \n \n " , filename )
2730 generated += "func NewAssertionError(err string) error {\n return errors.New(err)\n }\n \n "
2831 }
29- fmt .Printf ("Generated %s\n " , sourceFile .GetType ())
3032 switch sourceFile .GetType () {
3133 case "class" :
32- generated += ParseClass (sourceFile .(parsing.ParsedClass )) // Parse the class into one struct
34+ generated += ParseClass (sourceFile .(parsing.ParsedClass ), filename ) // Parse the class into one struct
3335 case "interface" :
34- generated += ParseInterface (sourceFile .(parsing.ParsedInterface ))
36+ generated += ParseInterface (sourceFile .(parsing.ParsedInterface ), filename )
3537 case "enum" :
36- generated += ParseEnum (sourceFile .(parsing.ParsedEnum ))
38+ generated += ParseEnum (sourceFile .(parsing.ParsedEnum ), filename )
3739 default :
3840 panic ("Unknown class type: " + sourceFile .GetType ())
3941 }
@@ -47,7 +49,7 @@ func ParseFile(sourceFile parsing.ParsedClasses, newClass bool) string {
4749}
4850
4951// Parse a given class
50- func ParseClass (source parsing.ParsedClass ) string {
52+ func ParseClass (source parsing.ParsedClass , filename string ) string {
5153 var generated string
5254
5355 // Create a context for the class, so that the methods have some frame of reference
@@ -66,6 +68,19 @@ func ParseClass(source parsing.ParsedClass) string {
6668 // classContext.Name = ToPrivate(source.Name)
6769 // }
6870
71+
72+ // Add the implements as a comment
73+ generated += fmt .Sprintf ("//%v\n " , source .Implements )
74+
75+ // Add the extends as a comment
76+ generated += fmt .Sprintf ("//%v\n " , source .Extends )
77+
78+ // If the class has any static blocks, then generated them
79+ for _ , staticBlock := range source .StaticBlocks {
80+ generated += CreateStaticBlock (staticBlock , classContext , 0 )
81+ generated += "\n \n "
82+ }
83+
6984 // Parse the class itself as a struct
7085 // If the class is static, don't generate a struct for it
7186 if ! parsetools .Contains ("static" , source .Modifiers ) {
@@ -82,13 +97,13 @@ func ParseClass(source parsing.ParsedClass) string {
8297
8398 // Parse the nested classes
8499 for _ , nested := range source .NestedClasses {
85- generated += ParseFile (nested , false )
100+ generated += ParseFile (nested , false , filename )
86101 }
87102
88103 return generated
89104}
90105
91- func ParseEnum (source parsing.ParsedEnum ) string {
106+ func ParseEnum (source parsing.ParsedEnum , filename string ) string {
92107 var generated string
93108
94109 classContext := new (ClassContext )
@@ -103,6 +118,15 @@ func ParseEnum(source parsing.ParsedEnum) string {
103118 parsedEnums = append (parsedEnums , CreateEnumField (field , classContext ))
104119 }
105120
121+ // Add the implements as a comment
122+ generated += fmt .Sprintf ("//%v\n " , source .Implements )
123+
124+ // If the class has any static blocks, then generated them
125+ for _ , staticBlock := range source .StaticBlocks {
126+ generated += CreateStaticBlock (staticBlock , classContext , 0 )
127+ generated += "\n \n "
128+ }
129+
106130 if ! parsetools .Contains ("static" , source .Modifiers ) {
107131 generated += CreateStruct (classContext , source .ClassVariables )
108132 }
@@ -147,10 +171,15 @@ func ParseEnum(source parsing.ParsedEnum) string {
147171 generated += "\n \n " // Add some spacing in between the methods
148172 }
149173
174+ // Parse the nested classes
175+ for _ , nested := range source .NestedClasses {
176+ generated += ParseFile (nested , false , filename )
177+ }
178+
150179 return generated
151180}
152181
153- func ParseInterface (source parsing.ParsedInterface ) string {
182+ func ParseInterface (source parsing.ParsedInterface , filename string ) string {
154183 var generated string
155184
156185 // Create a context for the class, so that the methods have some frame of reference
@@ -175,7 +204,7 @@ func ParseInterface(source parsing.ParsedInterface) string {
175204
176205 // Parse the nested classes
177206 for _ , nested := range source .NestedClasses {
178- generated += ParseFile (nested , false )
207+ generated += ParseFile (nested , false , filename )
179208 }
180209
181210 return generated
@@ -301,6 +330,13 @@ func CreateMethod(classContext *ClassContext, methodSource parsing.ParsedMethod)
301330 return result
302331}
303332
333+ func CreateStaticBlock (lines []codeparser.LineTyper , classContext * ClassContext , indentation int ) string {
334+ block := fmt .Sprintf ("func init() {\n " )
335+ block += CreateBody (lines , classContext , indentation + 2 )
336+ block += fmt .Sprintf ("\n %s}" , strings .Repeat (" " , indentation ))
337+ return block
338+ }
339+
304340// Parses the lines of the body
305341func CreateBody (body []codeparser.LineTyper , classContext * ClassContext , indentation int ) string {
306342 var result string
@@ -556,17 +592,15 @@ func CreateLine(line codeparser.LineTyper, classContext *ClassContext, indentati
556592 CreateBody (line .(codeparser.LineBlock ).Lines , classContext , indentation + 2 ),
557593 strings .Repeat (" " , indentation ),
558594 )
595+ case "DoWhileCondition" : // The while condition for a do-while, comes after the do block
596+ lastWhileCondition = line // Global
559597 case "DoWhileStatement" :
560- var body string
561- for _ , line := range line .(codeparser.LineBlock ).Words ["Statement" ].([]codeparser.LineType ) {
562- body += CreateLine (line , classContext , 0 , false ) + " "
563- }
564598 result += strings .Repeat (" " , indentation ) + fmt .Sprintf (
565599 "for {%s\n %s\n %sif %s {\n break\n %s}}" ,
566600 CreateBody (line .(codeparser.LineBlock ).Lines , classContext , indentation + 2 ),
567601 strings .Repeat (" " , indentation ),
568602 strings .Repeat (" " , indentation ),
569- body ,
603+ lastWhileCondition ,
570604 strings .Repeat (" " , indentation ),
571605 )
572606 case "TypeAssertion" :
0 commit comments