@@ -23,12 +23,18 @@ export default function generate(program) {
2323 /**
2424 * Standard generator code used in every file.
2525 */
26+ this . functions = new Map ( ) ;
27+ p . statements . forEach ( s => {
28+ if ( s . kind === "FuncDef" ) {
29+ this . functions . set ( s . name , s ) ;
30+ }
31+ } ) ;
2632 const range = p . globalRange ?. [ 0 ] ?. range ;
2733 const timestep = p . globalRange ?. [ 0 ] ?. timestep ?. [ 0 ] ;
2834 const start = range ? gen ( range . start ) : 1 ;
2935 const end = range ? gen ( range . end [ 0 ] ) : 5 ;
3036 const step = timestep ? gen ( timestep . value ) :
31- start <= end ? 1 : - 1 ;
37+ start <= end ? 1 : - 1 ;
3238 inputCode . push ( `
3339 import { createInterface } from "node:readline/promises";
3440 import { stdin as input, stdout as output } from "node:process";
@@ -108,17 +114,8 @@ export default function generate(program) {
108114
109115 // check for slices in the body of the function
110116 if ( d . body . kind === "SliceExpr" ) {
111- const sliceExpressions = d . body . expressions . map ( expr => {
112- return `${ gen ( expr ) } ` ;
113- } ) . join ( ", " ) ;
114-
115- output . push ( `function ${ funcName } (${ param } ) {
116- return [${ sliceExpressions } ];
117- }` ) ;
118- } else {
119- // handle same as before if not a slice
120- const lastStmt = body . pop ( ) ;
121- output . push ( `function ${ funcName } (${ param } ) {\n${ body } \nreturn ${ lastStmt } ;\n} ` ) ;
117+ const sliceExpressions = d . body . expressions . map ( expr => gen ( expr ) ) . join ( ", " ) ;
118+ output . push ( `function ${ funcName } (${ param } ) { return [${ sliceExpressions } ]; }` ) ;
122119 }
123120 if ( ! instantiatedMutableRanges . has ( param ) ) {
124121 output . push ( `let ${ param } = initializeMutableRange();` ) ;
@@ -135,6 +132,7 @@ export default function generate(program) {
135132 const stepValue = gen ( s . stepValue ) ;
136133 const arg = targetName ( s . expr . arg ) ;
137134 output . push ( `applyFunction(${ arg } , ${ stepValue } , ${ expr } );` ) ;
135+ return `${ arg } .values[${ arg } .index]` ;
138136 } ,
139137
140138 Expr ( e ) {
@@ -221,13 +219,13 @@ export default function generate(program) {
221219 return `${ gen ( e . id ) } .values.slice(0, ${ gen ( e . timeValue ) } )` ;
222220 } ,
223221
224- InputStmt ( e ) {
225- inputCode . push ( `
226- console.log(${ gen ( e . prompt [ 0 ] ) } );
227- const inputVar__${ inputIndex } = await rl.question("Input: ");
228- ` ) ;
229- return `inputVar__${ inputIndex ++ } `
230- } ,
222+ // InputStmt(e) {
223+ // inputCode.push(`
224+ // console.log(${gen(e.prompt[0])});
225+ // const inputVar__${inputIndex} = await rl.question("Input: ");
226+ // `);
227+ // return `inputVar__${inputIndex++}`
228+ // },
231229
232230 num ( n ) {
233231 return n . value ;
0 commit comments