@@ -13,83 +13,91 @@ export default function generate(program) {
1313
1414 const gen = node => generators ?. [ node ?. kind ] ?. ( node ) ?? node
1515
16- // const gen = (node) => {
17- // if (!node) return '';
18- // const handler = generators[node.kind];
19- // if (handler) {
20- // return handler(node);
21- // }
22- // throw new Error(`No generator for node type: ${node.kind}`);
23- // };
16+ const instantiatedMutableRanges = [ ]
2417
25- let currentFunction = { name : null , param : null } ;
18+ // let currentFunction = { name: null, param: null };
2619
2720 const generators = {
2821 Program ( p ) {
22+ /**
23+ * Standard generator code used in every file.
24+ */
25+ const range = p . globalRange ?. [ 0 ] ?. range ;
26+ const timestep = p . globalRange ?. [ 0 ] ?. timestep ?. [ 0 ] ;
27+ const start = range ? gen ( range . start ) : 1 ;
28+ const end = range ? gen ( range . end [ 0 ] ) : 5 ;
29+ const step = timestep ? gen ( timestep . value ) :
30+ start <= end ? 1 : - 1 ;
2931 output . push ( `
30- function generateRange(start, end, step) {
31- const range = [];
32- if (step === 0) step = 1;
33- if (start <= end) {
34- for (let i = start; i <= end; i += step) {
35- range.push(i);
36- }
37- }
38- else {
39- for (let i = start; i >= end; i -= step) {
40- range.push(i);
41- }
42- }
43- return range;
32+ function generateRange(start = ${ start } , end = ${ end } , step = ${ step } ) {
33+ if (end < start) step *= -1;
34+ return {
35+ start,
36+ end,
37+ step
38+ };
39+ }
40+
41+ function initializeMutableRange(timestepRange = generateRange()) {
42+ return {
43+ timestepRange,
44+ values: [],
45+ index: -1,
46+ size: 0
47+ };
4448 }
4549
4650 function funktionPrint(value) {
4751 if (Array.isArray(value)) {
4852 console.log(value.join('\\n'));
53+ }
54+ else if (typeof value === "object") {
55+ console.log(value.values.join('\\n'));
4956 }
5057 else {
5158 console.log(value);
5259 }
5360 }
61+
62+ function applyFunction(gen, iterations, f) {
63+ let currentVal = gen.timestepRange.start + gen.timestepRange.step * (gen.index + 1);
64+ if (gen.size === 0) {
65+ gen.size++;
66+ gen.index++;
67+ gen.values.push(f(currentVal));
68+ currentVal += gen.timestepRange.step;
69+ }
70+ if (gen.timestepRange.step > 0) {
71+ while (currentVal <= gen.timestepRange.end && iterations > 0) {
72+ gen.size++;
73+ gen.index++;
74+ gen.values.push(f(currentVal));
75+ currentVal += gen.timestepRange.step;
76+ iterations--;
77+ }
78+ } else {
79+ while (currentVal >= gen.timestepRange.end && iterations > 0) {
80+ gen.size++;
81+ gen.index++;
82+ gen.values.push(f(currentVal));
83+ currentVal += gen.timestepRange.step;
84+ iterations--;
85+ }
86+ }
87+ }
5488 ` ) ;
55- if ( p . globalRange ) {
56- const start = gen ( p . globalRange [ 0 ] . range . start ) ;
57- const end = gen ( p . globalRange [ 0 ] . range . end [ 0 ] ) ;
58- const step = p . globalRange [ 0 ] . timestep ? gen ( p . globalRange [ 0 ] . timestep [ 0 ] . value ) : ( start <= end ? 1 : - 1 ) ;
59- output . push ( `const globalRange = generateRange(${ start } , ${ end } , ${ step } );` ) ;
60- }
61- else {
62- output . push ( `const globalRange = [];` ) ;
63- }
6489 p . statements . forEach ( gen ) ;
6590 } ,
6691
67- // FuncDef(d) {
68- // const funcName = targetName(d);
69- // const param = d.param;
70- // output.push(`const ${funcName} = [];`);
71- // output.push(`let previous_${funcName} = 1;`);
72- // output.push(`for (const ${param} of globalRange) {`);
73- // const body = gen(d.body);
74- // output.push(` ${funcName}.push(${body});`);
75- // output.push(` previous_${funcName} = ${funcName}[${funcName}.length - 1];`);
76- // output.push(` }`);
77- // },
78-
7992 FuncDef ( d ) {
8093 const funcName = targetName ( d . name ) ;
81- const param = d . param ;
82- currentFunction . name = funcName ;
83- currentFunction . param = param ;
84- output . push ( `const ${ funcName } = [];` ) ;
85- output . push ( `let previous_${ funcName } = 1;` ) ;
86- output . push ( `for (const ${ param } of globalRange) {` ) ;
94+ const param = targetName ( d . param ) ;
8795 const body = gen ( d . body ) ;
88- output . push ( ` ${ funcName } .push( ${ body } );` ) ;
89- output . push ( ` previous_ ${ funcName } = ${ funcName } [ ${ funcName } .length - 1]; ` ) ;
90- output . push ( `}` ) ;
91- currentFunction . name = null ;
92- currentFunction . param = null ;
96+ const lastStmt = body . pop ( ) ;
97+ output . push ( `function ${ funcName } ( ${ param } ) {\n ${ body } \nreturn ${ lastStmt } ;\n} ` ) ;
98+ if ( instantiatedMutableRanges . indexOf ( param ) === - 1 ) {
99+ output . push ( `let ${ param } = initializeMutableRange();` ) ;
100+ }
93101 } ,
94102
95103 PrintStmt ( s ) {
@@ -98,12 +106,9 @@ export default function generate(program) {
98106
99107 StepCall ( s ) {
100108 const expr = gen ( s . expr ) ;
101- let stepValue = s . stepValue ? gen ( s . stepValue ) : 1 ;
102- stepValue = Number ( stepValue ) || 1 ;
103- if ( currentFunction . name && expr === `${ currentFunction . name } (${ currentFunction . param } )` ) {
104- return `previous_${ currentFunction . name } ` ;
105- }
106- return `${ expr } [${ stepValue - 1 } ]` ;
109+ const stepValue = gen ( s . stepValue ) ;
110+ const arg = targetName ( s . expr . arg ) ;
111+ output . push ( `applyFunction(${ arg } , ${ stepValue } , ${ expr } );` ) ;
107112 } ,
108113
109114 Expr ( e ) {
@@ -117,7 +122,9 @@ export default function generate(program) {
117122 CondExpr ( e ) {
118123 if ( e . thenBranch ) {
119124 const condleft = gen ( e . leftCond ) ;
120- const op = e . op ;
125+ const op = e . op === "==" ? "===" :
126+ e . op === "!=" ? "!==" :
127+ e . op ;
121128 const condright = gen ( e . rightCond ) ;
122129 const thenBranch = gen ( e . thenBranch ) ;
123130 const elseBranch = gen ( e . elseBranch ) ;
@@ -178,7 +185,7 @@ export default function generate(program) {
178185 } ,
179186
180187 TimeCall ( e ) {
181- return gen ( e . funcCall ) ;
188+ return ` ${ gen ( e . id ) } .values.slice(0, ${ gen ( e . timeValue ) } )` ;
182189 } ,
183190
184191 num ( n ) {
@@ -194,11 +201,11 @@ export default function generate(program) {
194201 } ,
195202
196203 id ( i ) {
197- return i . name ;
204+ return targetName ( i . name ) ;
198205 } ,
199206
200207 FuncCall ( c ) {
201- return `${ targetName ( c . name ) } ( ${ c . arg } ) ` ;
208+ return `${ targetName ( c . name ) } ` ;
202209 }
203210 } ;
204211
0 commit comments