Skip to content

Commit 940eadc

Browse files
committed
2 parents 2d7c9a4 + 3ab235f commit 940eadc

File tree

2 files changed

+104
-35
lines changed

2 files changed

+104
-35
lines changed

src/generator.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,10 @@ export default function generate(program) {
154154
return exprs;
155155
},
156156

157-
//anywhere you see SliceExpr in the code, I added it in ~MS
158-
SliceExpr(e) {
159-
const slices = e.expressions.map(gen);
160-
// join slices with spaces in output
161-
return `[${slices.join(', ')}]`;
162-
},
157+
// SliceExpr(e) {
158+
// const slices = e.expressions.map(gen);
159+
// return `[${slices.join(', ')}]`;
160+
// },
163161

164162
CondExpr(e) {
165163
if (e.thenBranch) {

test/generator.test.js

Lines changed: 100 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const fixtures = [
7878
}
7979
8080
funktionPrint("Hello, World!");
81-
rl.close()
81+
rl.close();
8282
`),
8383
},
8484
{
@@ -152,12 +152,11 @@ const fixtures = [
152152
}
153153
}
154154
155-
applyFunction(x_2, 1, factorial_1);
156155
applyFunction(x_2, 1, factorial_1);
157156
function factorial_1(x_2) { return [(x_2 * x_2.values[x_2.index])]; }
158157
let x_2 = initializeMutableRange();
159158
funktionPrint(factorial_1(x_2));
160-
rl.close()
159+
rl.close();
161160
`),
162161
},
163162
{
@@ -229,7 +228,7 @@ const fixtures = [
229228
}
230229
231230
funktionPrint((1 + (2 * 3)));
232-
rl.close()
231+
rl.close();
233232
`)
234233
},
235234
{
@@ -301,7 +300,7 @@ const fixtures = [
301300
}
302301
303302
funktionPrint((5 & 3));
304-
rl.close()
303+
rl.close();
305304
`)
306305
},
307306
{
@@ -373,7 +372,7 @@ const fixtures = [
373372
}
374373
375374
funktionPrint((4 << 1));
376-
rl.close()
375+
rl.close();
377376
`)
378377
},
379378
{
@@ -445,7 +444,7 @@ const fixtures = [
445444
}
446445
447446
funktionPrint((5 % 2));
448-
rl.close()
447+
rl.close();
449448
`)
450449
},
451450
{
@@ -517,7 +516,7 @@ const fixtures = [
517516
}
518517
519518
funktionPrint(Math.pow(2, 3));
520-
rl.close()
519+
rl.close();
521520
`)
522521
},
523522
{
@@ -590,7 +589,7 @@ const fixtures = [
590589
}
591590
592591
funktionPrint(( 1 > 0 ? 1 : (-1)));
593-
rl.close()
592+
rl.close();
594593
`),
595594
},
596595
{
@@ -662,7 +661,7 @@ const fixtures = [
662661
}
663662
664663
funktionPrint((-1));
665-
rl.close()
664+
rl.close();
666665
`)
667666
},
668667
{
@@ -734,7 +733,7 @@ const fixtures = [
734733
}
735734
736735
funktionPrint((~1));
737-
rl.close()
736+
rl.close();
738737
`)
739738
},
740739
{
@@ -811,7 +810,7 @@ const fixtures = [
811810
let x_2 = initializeMutableRange();
812811
applyFunction(x_2, 1, f_1);
813812
funktionPrint(x_2.values[x_2.index]);
814-
rl.close()
813+
rl.close();
815814
`)
816815
},
817816
{
@@ -887,7 +886,7 @@ const fixtures = [
887886
function f_1(x_2) { return [5]; }
888887
let x_2 = initializeMutableRange();
889888
funktionPrint(f_1.values.slice(0, 10));
890-
rl.close()
889+
rl.close();
891890
`),
892891
},
893892
{
@@ -959,22 +958,21 @@ const fixtures = [
959958
}
960959
961960
funktionPrint("a");
962-
rl.close()
961+
rl.close();
963962
`),
964963
},
965964
{
966-
name: "slices",
967-
source:
968-
"`1..9` t3t\n" +
969-
"f(x) = x \\ x + 1 \\ x + 2\n" +
970-
"f(x).step(2)\n" +
971-
"print(x:7)",
965+
name: "slice with multiple elements",
966+
source: `
967+
f(x) = x \\ x * 2 \\ x ** 3
968+
print(f(x))
969+
`,
972970
expected: dedent(`
973971
import { createInterface } from "node:readline/promises";
974972
import { stdin as input, stdout as output } from "node:process";
975973
const rl = createInterface({ input, output });
976974
977-
function generateRange(start = 1, end = 9, step = 3) {
975+
function generateRange(start = 1, end = 5, step = 1) {
978976
if (end < start) step *= -1;
979977
return {
980978
start,
@@ -1033,12 +1031,10 @@ const fixtures = [
10331031
}
10341032
}
10351033
}
1036-
1037-
function f_1(x_2) { return [x_2, (x_2 + 1), (x_2 + 2)]; }
1034+
function f_1(x_2) { return [x_2, (x_2 * 2), Math.pow(x_2, 3)]; }
10381035
let x_2 = initializeMutableRange();
1039-
applyFunction(x_2, 2, f_1);
1040-
funktionPrint(x_2.values.slice(0, 7));
1041-
rl.close()
1036+
funktionPrint(f_1(x_2));
1037+
rl.close();
10421038
`)
10431039
},
10441040
{
@@ -1111,7 +1107,7 @@ const fixtures = [
11111107
}
11121108
}
11131109
funktionPrint(1);
1114-
rl.close()
1110+
rl.close();
11151111
`)
11161112
},
11171113
{
@@ -1182,7 +1178,7 @@ const fixtures = [
11821178
}
11831179
}
11841180
funktionPrint(( 1 === 1 ? "yes" : "no"));
1185-
rl.close()
1181+
rl.close();
11861182
`)
11871183
},
11881184
{
@@ -1253,9 +1249,84 @@ const fixtures = [
12531249
}
12541250
}
12551251
funktionPrint(( 1 !== 2 ? "yes" : "no"));
1256-
rl.close()
1252+
rl.close();
12571253
`)
12581254
},
1255+
{
1256+
name: "input with prompt",
1257+
source: `f(x) = input("Enter your name: ")`,
1258+
expected: dedent(`
1259+
import { createInterface } from "node:readline/promises";
1260+
import { stdin as input, stdout as output } from "node:process";
1261+
const rl = createInterface({ input, output });
1262+
1263+
console.log("Enter your name: ");
1264+
const inputVar__0 = await rl.question("Input: ");
1265+
1266+
function generateRange(start = 1, end = 5, step = 1) {
1267+
if (end < start) step *= -1;
1268+
return {
1269+
start,
1270+
end,
1271+
step
1272+
};
1273+
}
1274+
1275+
function initializeMutableRange(timestepRange = generateRange()) {
1276+
return {
1277+
timestepRange,
1278+
values: [],
1279+
index: -1,
1280+
size: 0
1281+
};
1282+
}
1283+
1284+
function funktionPrint(value) {
1285+
if (Array.isArray(value)) {
1286+
console.log(value.join('\\n'));
1287+
}
1288+
else if (typeof value === "object") {
1289+
console.log(value.values.join('\\n'));
1290+
}
1291+
else {
1292+
console.log(value);
1293+
}
1294+
}
1295+
1296+
function applyFunction(gen, iterations, f) {
1297+
let currentVal = gen.timestepRange.start + gen.timestepRange.step * (gen.index + 1);
1298+
if (gen.size === 0) {
1299+
gen.size++;
1300+
gen.index++;
1301+
const result = f(currentVal);
1302+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
1303+
currentVal += gen.timestepRange.step;
1304+
}
1305+
if (gen.timestepRange.step > 0) {
1306+
while (currentVal <= gen.timestepRange.end && iterations > 0) {
1307+
gen.size++;
1308+
gen.index++;
1309+
const result = f(currentVal);
1310+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
1311+
currentVal += gen.timestepRange.step;
1312+
iterations--;
1313+
}
1314+
} else {
1315+
while (currentVal >= gen.timestepRange.end && iterations > 0) {
1316+
gen.size++;
1317+
gen.index++;
1318+
const result = f(currentVal);
1319+
gen.values.push(Array.isArray(result) ? result.join(' ') : result);
1320+
currentVal += gen.timestepRange.step;
1321+
iterations--;
1322+
}
1323+
}
1324+
}
1325+
function f_1(x_2) { return [inputVar__0]; }
1326+
let x_2 = initializeMutableRange();
1327+
rl.close();
1328+
`)
1329+
}
12591330
];
12601331

12611332
describe("The code generator", () => {

0 commit comments

Comments
 (0)