@@ -97,8 +97,6 @@ class BackendLua : CompilerBackend {
9797 output ~= " regA = 0;\n " ;
9898 output ~= " regB = 0;\n " ;
9999 output ~= " dspRestore = 0;\n " ;
100- output ~= format(" exception = %d;\n " , globalStack);
101- globalStack += 4 ;
102100
103101 output ~= "
104102 function cal_pop()
@@ -124,9 +122,15 @@ class BackendLua : CompilerBackend {
124122 );
125123 }
126124
127- output ~= " end\n " ;
125+ if (output.mode != OutputMode.Module) {
126+ output ~= " end\n " ;
127+ }
128+
129+ // end top level code
130+ output.FinishSection();
128131
129132 // create arrays
133+ output.StartSection(SectionType.Data);
130134 foreach (ref array ; arrays) {
131135 // create metadata
132136 auto arrayExtra = cast (ArrayExtra* ) array.extra;
@@ -153,13 +157,19 @@ class BackendLua : CompilerBackend {
153157 end
154158 " , array.values .length, arrayExtra.elements);
155159 }
160+ output.FinishSection();
156161
157- output ~= " regA = 0\n " ;
158- output ~= " calmain();\n " ;
162+ if (output.mode != OutputMode.Module) {
163+ output ~= " regA = 0\n " ;
164+ output ~= " calmain();\n " ;
165+ }
159166 }
160167
161168 override void BeginMain () {
162- output ~= " function calmain()\n " ;
169+ output.StartSection(SectionType.TopLevel);
170+ if (output.mode != OutputMode.Module) {
171+ output ~= " function calmain()\n " ;
172+ }
163173
164174 // call constructors
165175 foreach (global ; globals) {
@@ -376,7 +386,8 @@ class BackendLua : CompilerBackend {
376386 }
377387
378388 if (node.inline) {
379- output ~= format(" mem[%d] = 0\n " , exception);
389+ // why was this here??
390+ // output ~= format("mem[%d] = 0\n", exception);
380391
381392 words ~= Word(
382393 output.GetModName(), node.name, WordType.Callisto, false , true ,
@@ -688,6 +699,8 @@ class BackendLua : CompilerBackend {
688699 " Anonymous variables can only be created inside a function"
689700 );
690701 }
702+
703+ output.AddGlobal(global);
691704 }
692705 }
693706
@@ -963,6 +976,13 @@ class BackendLua : CompilerBackend {
963976 assert (! inScope);
964977 inScope = true ;
965978
979+ output.StartSection(SectionType.Implement);
980+ if (output.mode == OutputMode.Module) {
981+ auto sect = cast (ImplementSection) output.sect;
982+ sect.type = type.name;
983+ sect.method = node.method;
984+ }
985+
966986 output ~= format(" function %s()\n " , labelName);
967987
968988 foreach (ref inode ; node.nodes) {
@@ -989,6 +1009,8 @@ class BackendLua : CompilerBackend {
9891009
9901010 inScope = false ;
9911011 variables = [];
1012+
1013+ output.FinishSection();
9921014 }
9931015
9941016 void SetGlobal (
0 commit comments