@@ -12,39 +12,38 @@ export default class JSCompiler extends BaseTranspiler {
1212 }
1313
1414 transpile ( options : Partial < TranspilerOptions > = { } ) {
15- var imports = options . imports || [ ] ;
16- var js = `` ; //` "use strict";`;
17- var prevfun = "" ;
18- var prevfunpublic = false ;
19- var prevobj = "" ;
20- var prevobjpublic = false ;
21- var curlvl = 0 ;
22- var strayvar = [ ] ;
23- var took = 0 ;
24- var funcurlvls = [ ] ;
25- var errcurlvls = [ ] ;
15+ const imports = options . imports || [ ] ;
16+ let js = "" ; //' "use strict";'; (strict mode currently broken)
17+ let prevfun = "" ;
18+ let prevfunpublic = false ;
19+ let prevobj = "" ;
20+ let prevobjpublic = false ;
21+ let previsfun = false ;
22+ let curlvl = 0 ;
23+ let strayvar = [ ] ;
24+ let took = 0 ;
25+ const errcurlvls = [ ] ;
2626
2727 function getval ( x ) {
2828 if ( x === undefined ) {
2929 return "" ;
3030 }
3131 if ( x [ 0 ] == "ans" ) {
32- var ans = strayvar [ strayvar . length - 1 ] ;
32+ const ans = strayvar [ strayvar . length - 1 ] ;
3333 strayvar = [ ] ;
3434 return ans ;
3535 }
3636 return x [ 1 ] ;
3737 }
3838
39- for ( var i = 0 ; i < this . asc . length ; i ++ ) {
40- var a = this . asc [ i ] ;
39+ for ( const a of this . asc ) {
4140 if ( a . op == "var" ) {
42- for ( var j = 0 ; j < a . count ; j ++ ) {
41+ for ( let j = 0 ; j < a . count ; j ++ ) {
4342 if ( a . values [ j ] === undefined ) {
4443 a . values [ j ] = [ ] ;
4544 }
46- var name = a . names [ j ] ;
47- var value = a . values [ j ] [ 1 ] ;
45+ let name = a . names [ j ] ;
46+ let value = a . values [ j ] [ 1 ] ;
4847 if ( name === undefined ) {
4948 name = this . nextTmpVar ( ) ;
5049 strayvar . push ( name ) ;
@@ -59,57 +58,37 @@ export default class JSCompiler extends BaseTranspiler {
5958 } else if ( a . type == "bol" ) {
6059 value = "false" ;
6160 } else if ( a . type == "fun" ) {
62- value = "()=>0 " ;
61+ value = "_=>{} " ;
6362 prevfun = name ;
6463 prevfunpublic = a . public ;
6564 } else if ( a . type == "obj" ) {
6665 value = "{}" ;
6766 prevobj = name ;
6867 prevobjpublic = a . public ;
6968 } else if ( a . type == "any" ) {
70- value = "undefined " ;
69+ value = "void 0 " ;
7170 }
7271 }
73- js += `${ a . public ? `var ${ name } = this. ` : "var " } ${ name } = ${ value } ;` ;
72+ js += `var ${ name } = ${ a . public ? `this. ${ name } = ` : "" } ${ value } ;` ;
7473 }
7574 } else if ( a . op == "print" ) {
76- js += `console.log(` ;
77- for ( var j = 0 ; j < strayvar . length ; j ++ ) {
78- js += `${ strayvar [ j ] } ` ;
79- if ( j != strayvar . length - 1 ) {
80- js += "," ;
81- }
82- }
83- js += ");" ;
75+ js += `console.log(${ strayvar . join ( "," ) } );` ;
8476 strayvar = [ ] ;
8577 } else if ( a . op == "fun" ) {
86- // console.log(curlvl);
87- funcurlvls . push ( curlvl ) ;
88- js += `${
89- prevfunpublic ? `${ prevfun } = this.` : ""
90- } ${ prevfun } =function(`;
91- for ( var j = 0 ; j < a . arity ; j ++ ) {
92- js += a . args [ j ] . name ;
93- if ( j != a . arity - 1 ) {
94- js += "){return function(" ;
95- curlvl ++ ;
96- }
97- }
98- js += "){" ;
78+ js += `${ prevfun } =${ prevfunpublic ? `this.${ prevfun } =` : "" } ${
79+ a . args . length == 0
80+ ? "()=>"
81+ : a . args . map ( arg => `${ arg . name } =>` ) . join ( "" )
82+ } {`;
9983 curlvl ++ ;
10084 } else if ( a . op == "funbody" ) {
101- if ( this . asc [ i - 1 ] . op != "fun" ) {
102- funcurlvls . push ( curlvl ) ;
103- js += `${
104- prevfunpublic ? `${ prevfun } = this.` : ""
105- } ${ prevfun } =function(){`;
85+ if ( ! previsfun ) {
86+ js += `${ prevfun } =${ prevfunpublic ? `this.${ prevfun } =` : "" } ()=>{` ;
10687 curlvl ++ ;
10788 }
10889 } else if ( a . op == "funend" ) {
109- // console.log(funcurlvls, curlvl);
110- var cl = funcurlvls . pop ( ) ;
111- js += "};" . repeat ( curlvl - cl ) ;
112- curlvl = cl ;
90+ js += "};" ;
91+ curlvl -- ;
11392 } else if ( a . op == "objend" ) {
11493 js += "};" ;
11594 curlvl -- ;
@@ -119,9 +98,8 @@ export default class JSCompiler extends BaseTranspiler {
11998 } else if ( a . op == "prop" ) {
12099 js += `${ a . name } :${ a . value [ 1 ] } ,` ;
121100 } else if ( a . op == "end" ) {
122- js += "}" ;
101+ js += "}; " ;
123102 curlvl -- ;
124- js += ";" ;
125103 } else if ( a . op == "if" ) {
126104 if ( a . elseif ) {
127105 js += "}else " ;
@@ -131,7 +109,7 @@ export default class JSCompiler extends BaseTranspiler {
131109 if ( a . not ) {
132110 js += "!(" ;
133111 }
134- var j = 0 ;
112+ let j = 0 ;
135113 while ( j < a . test . length ) {
136114 if ( a . test [ j ] [ 0 ] == "cmp" ) {
137115 js += a . test [ j ] [ 1 ] ;
@@ -165,96 +143,92 @@ export default class JSCompiler extends BaseTranspiler {
165143 } else if ( a . op == "return" ) {
166144 js += `return ${ getval ( a . value ) } ;` ;
167145 } else if ( a . op . startsWith ( "op" ) ) {
168- let _a = a as ASCNodeOperator ;
169- var lhs = getval ( _a . lhs ) ;
170- var rhs = getval ( _a . rhs ) ;
171- var vname = this . nextTmpVar ( ) ;
172- js += `var ${ vname } =${ lhs } ${ a . op . slice ( 2 ) } ${ rhs } ;` ;
146+ const _a = a as ASCNodeOperator ;
147+ const lhs = getval ( _a . lhs ) ;
148+ const rhs = getval ( _a . rhs ) ;
149+ const vname = this . nextTmpVar ( ) ;
150+ js += `const ${ vname } =${ lhs } ${ a . op . slice ( 2 ) } ${ rhs } ;` ;
173151 strayvar . push ( vname ) ;
174152 } else if ( a . op == "name" ) {
175- for ( var j = 0 ; j < a . names . length ; j ++ ) {
153+ for ( let j = 0 ; j < a . names . length ; j ++ ) {
176154 js += `var ${ a . names [ j ] } =${
177155 strayvar [ strayvar . length - a . names . length + j ]
178156 } ;`;
179157 }
180158 strayvar = strayvar . slice ( 0 , strayvar . length - a . names . length ) ;
181159 } else if ( a . op == "call" ) {
182160 if ( a . pop ) {
183- var jj = "" ;
184- for ( var j = 0 ; j < took ; j ++ ) {
161+ let jj = "" ;
162+ for ( let j = 0 ; j < took ; j ++ ) {
185163 jj += `(${ strayvar [ strayvar . length - took + j ] } )` ;
186164 }
187165 strayvar = strayvar . slice ( 0 , strayvar . length - took ) ;
188166 took = 0 ;
189- var vname = this . nextTmpVar ( ) ;
167+ const vname = this . nextTmpVar ( ) ;
190168 if ( ! jj . length ) {
191169 jj = "()" ;
192170 }
193- js += `var ${ vname } =${ getval ( a . fun ) } ` + jj + ";" ;
171+ js += `const ${ vname } =${ getval ( a . fun ) } ` + jj + ";" ;
194172 strayvar . push ( vname ) ;
195173 } else {
196- var vname = this . nextTmpVar ( ) ;
197- js += `var ${ vname } =${ getval ( a . fun ) } (${ a . args
174+ const vname = this . nextTmpVar ( ) ;
175+ js += `const ${ vname } =${ getval ( a . fun ) } (${ a . args
198176 . map ( x => getval ( x ) )
199177 . join ( ")(" ) } );`;
200178 strayvar . push ( vname ) ;
201179 }
202180 } else if ( a . op == "subscript" ) {
203- var idx = getval ( a . value ) ;
181+ const idx = getval ( a . value ) ;
204182 if ( idx == "rest" ) {
205- var vname = this . nextTmpVar ( ) ;
206- js += `var ${ vname } =${ getval ( a . container ) } .slice(1);` ;
183+ const vname = this . nextTmpVar ( ) ;
184+ js += `const ${ vname } =${ getval ( a . container ) } .slice(1);` ;
207185 strayvar . push ( vname ) ;
208186 } else {
209- var vname = this . nextTmpVar ( ) ;
210- js += `var ${ vname } =${ getval ( a . container ) } [${ idx } ${
187+ const vname = this . nextTmpVar ( ) ;
188+ js += `const ${ vname } =${ getval ( a . container ) } [${ idx } ${
211189 a . value [ 0 ] == "lit" ? "" : "-1"
212190 } ];`;
213191 strayvar . push ( vname ) ;
214192 }
215193 } else if ( a . op == "cat" ) {
216- var vname = this . nextTmpVar ( ) ;
217- js +=
218- `var ${ vname } =${ getval ( a . containers [ 0 ] ) } .concat(` +
219- a . containers
220- . slice ( 1 )
221- . map ( x => x [ 1 ] )
222- . join ( ").concat(" ) +
223- ");" ;
194+ const vname = this . nextTmpVar ( ) ;
195+ js += `const ${ vname } =${ getval ( a . containers [ 0 ] ) } .concat(${ a . containers
196+ . slice ( 1 )
197+ . map ( x => x [ 1 ] )
198+ . join ( ").concat(" ) } );`;
224199 strayvar . push ( vname ) ;
225200 } else if ( a . op == "push" ) {
226201 js += `${ getval ( a . container ) } .push(${ a . values
227202 . map ( x => getval ( x ) )
228203 . join ( "," ) } );`;
229204 } else if ( a . op == "for" ) {
230- js += `for (var ${ a . iterator } of ${ getval ( a . container ) } ){` ;
205+ js += `for(let ${ a . iterator } of ${ getval ( a . container ) } ){` ;
231206 curlvl ++ ;
232207 } else if ( a . op == "whiletrue" ) {
233- js += "while (true){" ;
208+ js += "while(true){" ;
234209 curlvl ++ ;
235210 } else if ( a . op == "whilen" ) {
236- var v = this . randVar ( ) ;
237- js += `for (var ${ v } =0;${ v } <${ getval ( a . value ) } ;${ v } ++){` ;
211+ const v = this . randVar ( ) ;
212+ js += `for(let ${ v } =0;${ v } <${ getval ( a . value ) } ;${ v } ++){` ;
238213 curlvl ++ ;
239214 } else if ( a . op == "break" ) {
240215 js += "break;" ;
241216 } else if ( a . op == "continue" ) {
242217 js += "continue;" ;
243218 } else if ( a . op == "not" ) {
244- let v = getval ( a . value ) ;
245- var vname = this . nextTmpVar ( ) ;
246- js += `var ${ vname } =!${ v } ;` ;
247-
219+ const v = getval ( a . value ) ;
220+ const vname = this . nextTmpVar ( ) ;
221+ js += `const ${ vname } =!${ v } ;` ;
248222 strayvar . push ( vname ) ;
249223 } else if ( a . op == "reassign" ) {
250224 if ( a . del == true ) {
251- var lhs = getval ( a . lhs ) ;
225+ const lhs = getval ( a . lhs ) ;
252226 js += `delete ${ lhs } [${ a . lhssubs [ 1 ] } ${
253227 a . lhssubs [ 0 ] == "lit" ? "" : "-1"
254228 } ];`;
255229 } else {
256- var rhs = getval ( a . rhs ) ;
257- var lhs = getval ( a . lhs ) ;
230+ let rhs = getval ( a . rhs ) ;
231+ let lhs = getval ( a . lhs ) ;
258232 if ( a . lhssubs ) {
259233 lhs += `[${ a . lhssubs [ 1 ] } ${ a . lhssubs [ 0 ] == "lit" ? "" : "-1" } ]` ;
260234 }
@@ -264,50 +238,50 @@ export default class JSCompiler extends BaseTranspiler {
264238 js += `${ lhs } =${ rhs } ;` ;
265239 }
266240 } else if ( a . op == "length" ) {
267- var vname = this . nextTmpVar ( ) ;
268- js += `var ${ vname } =${ getval ( a . container ) } .length;` ;
241+ const vname = this . nextTmpVar ( ) ;
242+ js += `const ${ vname } =${ getval ( a . container ) } .length;` ;
269243 strayvar . push ( vname ) ;
270244 } else if ( a . op == "temp" ) {
271- var vname = this . nextTmpVar ( ) ;
272- js += `var ${ vname } =${ a . iden [ 1 ] } ;` ;
245+ const vname = this . nextTmpVar ( ) ;
246+ js += `const ${ vname } =${ a . iden [ 1 ] } ;` ;
273247 strayvar . push ( vname ) ;
274248 } else if ( a . op == "discard" ) {
275249 strayvar = [ ] ;
276250 } else if ( a . op == "take" ) {
277251 took = a . count ;
278252 } else if ( a . op == "import" ) {
279- var f = a . file . replace ( / " / g, "" ) ;
280- for ( var j = 0 ; j < a . iden . length ; j ++ ) {
281- js += `var ${ a . iden [ j ] } =${ f } .${ a . iden [ j ] } ;` ;
253+ const f = a . file . replace ( / " / g, "" ) ;
254+ for ( const id of a . iden ) {
255+ js += `var ${ id } =${ f } .${ id } ;` ;
282256 }
283257 imports . push ( f ) ;
284258 } else if ( a . op == "try" ) {
285259 js += `try{` ;
286260 curlvl ++ ;
287261 } else if ( a . op == "catch" ) {
288- var r = this . randVar ( ) ;
262+ const r = this . randVar ( ) ;
289263 errcurlvls . push ( [ curlvl , r ] ) ;
290264 js += `}catch(${ r } ){` ;
291265 strayvar = [ ] ;
292266 } else if ( a . op == "catcherr" ) {
293- var ec = errcurlvls [ errcurlvls . length - 1 ] ;
267+ const ec = errcurlvls [ errcurlvls . length - 1 ] ;
294268 if ( a . error === undefined ) {
295- var vname = this . nextTmpVar ( ) ;
269+ const vname = this . nextTmpVar ( ) ;
296270 strayvar . push ( vname ) ;
297271 if ( curlvl != ec [ 0 ] ) {
298272 js += `}else{` ;
299273 }
300- js += `var ${ vname } =${ ec [ 1 ] } .name;` ;
274+ js += `const ${ vname } =${ ec [ 1 ] } .name;` ;
301275 } else {
302276 if ( curlvl != ec [ 0 ] ) {
303277 js += `}else ` ;
304278 curlvl -- ;
305279 }
306- js += `if (${ ec [ 1 ] } .name==${ a . error [ 1 ] } ){` ;
280+ js += `if (${ ec [ 1 ] } .name=== ${ a . error [ 1 ] } ){` ;
307281 curlvl ++ ;
308282 }
309283 } else if ( a . op == "tryend" ) {
310- var ec = errcurlvls . pop ( ) ;
284+ const ec = errcurlvls . pop ( ) ;
311285 if ( curlvl != ec [ 0 ] ) {
312286 js += `}` ;
313287 curlvl -- ;
@@ -316,14 +290,14 @@ export default class JSCompiler extends BaseTranspiler {
316290 curlvl -- ;
317291 strayvar = [ ] ;
318292 } else if ( a . op == "throw" ) {
319- var r = this . randVar ( ) ;
320- js += `var ${ r } = new Error(); ${ r } .name=${ a . error [ 1 ] } ; throw ${ r } ;` ;
293+ const r = this . randVar ( ) ;
294+ js += `{const ${ r } = new Error(); ${ r } .name=${ a . error [ 1 ] } ; throw ${ r } ;} ` ;
321295 } else if ( a . op == "comment" ) {
322296 js += `/*${ getval ( a . value ) } */` ;
323297 } else {
324298 console . log ( a . op ) ;
325299 }
326- // js+="\n"
300+ previsfun = a . op == "fun" ;
327301 }
328302 return { result : js , imports } ;
329303 }
0 commit comments