@@ -15,14 +15,14 @@ var IN_SWITCH: Bool = false;
1515
1616@:keep
1717class Instructions {
18- static function root (instrs : Array <Instruction >) {
18+ static function instr_root (instrs : Array <Instruction >) {
1919 var out = [];
2020 for (instr in instrs )
2121 out .push ( callInstruction (instr .name , instr .args ) );
2222 return out .join (' \n\n ' ).replaceAll (" \t\n " , ' ' );
2323 }
2424
25- static function call (name : String , kvargs : Map <Dynamic , Instruction >, iargs : Array <Instruction >) {
25+ static function instr_call (name : String , kvargs : Map <Dynamic , Instruction >, iargs : Array <Instruction >) {
2626 var args = [];
2727 if (iargs != null ) {
2828 // Function was called with arguments
@@ -35,13 +35,13 @@ class Instructions {
3535 return ' $name ()' ;
3636 }
3737
38- static function methodcall (meta_fname : String , meta_obj : Instruction , iargs : Array <Instruction >)
38+ static function instr_methodcall (meta_fname : String , meta_obj : Instruction , iargs : Array <Instruction >)
3939 return ' ${ callInline (meta_obj ) }: $meta_fname ( ${ (iargs != null ) ? iargs .map (x -> callInline (x )).join (" , " ) : ' ' })' ;
4040
41- static function literal (value : E2Type , raw : String )
41+ static function instr_literal (value : E2Type , raw : String )
4242 return raw ;
4343
44- static function _if (cond : Instruction , block : Instruction , is_elseif : Bool , ifeif : Instruction ) {
44+ static function instr_if (cond : Instruction , block : Instruction , is_elseif : Bool , ifeif : Instruction ) {
4545 if (is_elseif ) {
4646 return ' is_elseif' ;
4747 }
@@ -68,7 +68,7 @@ class Instructions {
6868 'end [${ ifeif != null ? callBlock(ifeif, false) : "" }]';*/
6969 }
7070
71- static function _for (varname : String , start : Instruction , end : Instruction , ? inc : Instruction , block : Instruction ) {
71+ static function instr_for (varname : String , start : Instruction , end : Instruction , ? inc : Instruction , block : Instruction ) {
7272 final startv = callInline (start );
7373 final endv = callInline (end );
7474
@@ -80,14 +80,14 @@ class Instructions {
8080 + ' end' ;
8181 }
8282
83- static function foreach (keyname : String , ? keytype : String , valname : String , valtype : String , tblexpr : Instruction , block : Instruction ) {
83+ static function instr_foreach (keyname : String , ? keytype : String , valname : String , valtype : String , tblexpr : Instruction , block : Instruction ) {
8484 return ' for $keyname , $valname in pairs( ${ callInline (tblexpr ) }) do \n ' +
8585 ' \t ${callBlock (block , true )}\n ' +
8686 ' \t ::continue:: \n ' +
8787 ' end' ;
8888 }
8989
90- static function _while (cond : Instruction , block : Instruction , is_dowhile : Bool ) {
90+ static function instr_while (cond : Instruction , block : Instruction , is_dowhile : Bool ) {
9191 if (is_dowhile ) {
9292 // Not using a repeat until
9393 return ' while true do\n ' +
@@ -102,10 +102,10 @@ class Instructions {
102102 ' end' ;
103103 }
104104
105- static function variable (name : String )
105+ static function instr_variable (name : String )
106106 return name ;
107107
108- static function _switch (topexpr : Instruction , cases : Array <{? match : Instruction , block : Instruction }>) {
108+ static function instr_switch (topexpr : Instruction , cases : Array <{? match : Instruction , block : Instruction }>) {
109109 IN_SWITCH = true ;
110110 var topvar = callInline (topexpr );
111111
@@ -126,65 +126,73 @@ class Instructions {
126126 return out ;
127127 }
128128
129- static function _break ()
129+ static function instr_break ()
130130 return IN_SWITCH ? ' ' : " break" ;
131131
132- static function _continue ()
132+ static function instr_continue ()
133133 return ' goto continue' ;
134134
135- static function _return (val : Instruction )
135+ static function instr_return (val : Instruction )
136136 return ' return ${ callInline (val ) }' ;
137137
138- static function increment (varname : String )
138+ static function instr_increment (varname : String )
139139 return ' $varname = $varname + 1' ;
140140
141- static function decrement (varname : String )
141+ static function instr_decrement (varname : String )
142142 return ' $varname = $varname - 1' ;
143143
144- static function add (v : Instruction , addend : Instruction )
144+ static function instr_add (v : Instruction , addend : Instruction )
145145 return ' ${ callInline (v ) } + ${ callInline (addend ) }' ;
146146
147- static function sub (v : Instruction , addend : Instruction )
147+ static function instr_sub (v : Instruction , addend : Instruction )
148148 return ' ${ callInline (v ) } - ${ callInline (addend ) }' ;
149149
150- static function div (v : Instruction , addend : Instruction )
150+ static function instr_div (v : Instruction , addend : Instruction )
151151 return ' ${ callInline (v ) } / ${ callInline (addend ) }' ;
152152
153- static function mul (v : Instruction , addend : Instruction )
153+ static function instr_mul (v : Instruction , addend : Instruction )
154154 return ' ${ callInline (v ) } * ${ callInline (addend ) }' ;
155155
156- static function assign (varname : String , to : Instruction )
157- return ' $varname = ${ callInline (to ) }' ;
158-
159- static function eq (v1 : Instruction , v2 : Instruction )
156+ static function instr_eq (v1 : Instruction , v2 : Instruction )
160157 return ' ${ callInline (v1 ) } == ${ callInline (v2 ) }' ;
161158
162- static function neq (v1 : Instruction , v2 : Instruction )
159+ static function instr_neq (v1 : Instruction , v2 : Instruction )
163160 return ' ${ callInline (v1 ) } ~= ${ callInline (v2 ) }' ;
164161
165- static function leq (v1 : Instruction , v2 : Instruction )
162+ static function instr_leq (v1 : Instruction , v2 : Instruction )
166163 return ' ${ callInline (v1 ) } <= ${ callInline (v2 ) }' ;
167164
168- static function geq (v1 : Instruction , v2 : Instruction )
165+ static function instr_geq (v1 : Instruction , v2 : Instruction )
169166 return ' ${ callInline (v1 ) } >= ${ callInline (v2 ) }' ;
170167
171- static function grouped_equation (v1 : Instruction )
168+ static function instr_grouped_equation (v1 : Instruction )
172169 return ' ( ${ callInline (v1 ) })' ;
173170
174- static function assignlocal (varname : String , to : Instruction )
171+ static function instr_assign (varname : String , to : Instruction )
172+ return ' $varname = ${ callInline (to ) }' ;
173+
174+ static function instr_assignlocal (varname : String , to : Instruction )
175175 return ' local $varname = ${ callInline (to ) }' ;
176176
177- static function function_decl (name : String , ret_type : String , meta_type : String , sig : String , args : Array <{name : String , type : String }>, decl : Instruction ) {
177+ static function instr_fndecl (name : String , ret_type : String , meta_type : String , sig : String , args : Array <{name : String , type : String }>, decl : Instruction ) {
178178 return ' function ${(meta_type != null ) ? ' ${meta_type }_' : ' ' }$name ( ${ args .map ( (v ) -> v .name ).join (" , " ) }) \n ' +
179179 ' \t ${ callBlock (decl , true ) }\n ' +
180180 ' end' ;
181181 }
182182
183- static function ternary (cond : Instruction , success : Instruction , fallback : Instruction )
183+ static function instr_ternary (cond : Instruction , success : Instruction , fallback : Instruction )
184184 return ' ${ callInline (cond ) } and ${ callInline (success ) } or ${ callInline (fallback ) }' ;
185185
186+ static function instr_try (block : Instruction , var_name : String , catch_block : Instruction ) {
187+ return ' xpcall(function()\n ' +
188+ ' \t ${ callBlock (block , true ) }\n ' +
189+ ' end, function( $var_name ) \n ' +
190+ ' \t ${ callBlock (catch_block , true ) }\n ' +
191+ ' end)' ;
192+ }
193+
186194 // Bitwise ops
187- static function bor (v1 : Instruction , v2 : Instruction ) {
195+ static function instr_bor (v1 : Instruction , v2 : Instruction ) {
188196 #if LUA54
189197 return ' ${ callInline (v1 ) } | ${ callInline (v2 ) }' ;
190198 #else
@@ -195,6 +203,8 @@ class Instructions {
195203
196204// Expr -> Lua
197205function callInstruction (name : String , args : Array <Dynamic >): String {
206+ var name = ' instr_' + name ;
207+
198208 if (! Reflect .hasField (Instructions , name ))
199209 throw new NotImplementedException (' Instruction [" $name "] does not exist.' );
200210
0 commit comments