porffor 0.0.0-650350 → 0.0.0-828ee15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/codeGen.js +19 -19
- package/package.json +1 -1
- package/runner/repl.js +3 -3
package/compiler/codeGen.js
CHANGED
@@ -327,13 +327,15 @@ const concatStrings = (scope, left, right, global, name, assign) => {
|
|
327
327
|
|
328
328
|
scope.memory = true;
|
329
329
|
|
330
|
-
const
|
330
|
+
const getLocalTmp = name => localTmp(scope, name + binaryExpDepth);
|
331
331
|
|
332
332
|
const rightPointer = localTmp(scope, 'concat_right_pointer', Valtype.i32);
|
333
333
|
const rightLength = localTmp(scope, 'concat_right_length', Valtype.i32);
|
334
334
|
const leftLength = localTmp(scope, 'concat_left_length', Valtype.i32);
|
335
335
|
|
336
336
|
if (assign) {
|
337
|
+
const pointer = arrays.get(name ?? '$undeclared');
|
338
|
+
|
337
339
|
return [
|
338
340
|
// setup right
|
339
341
|
...right,
|
@@ -384,15 +386,12 @@ const concatStrings = (scope, left, right, global, name, assign) => {
|
|
384
386
|
|
385
387
|
const leftPointer = localTmp(scope, 'concat_left_pointer', Valtype.i32);
|
386
388
|
|
387
|
-
|
389
|
+
// alloc/assign array
|
390
|
+
const [ , pointer ] = makeArray(scope, {
|
388
391
|
rawElements: new Array(0)
|
389
392
|
}, global, name, true, 'i16');
|
390
393
|
|
391
394
|
return [
|
392
|
-
// setup new/out array
|
393
|
-
...newOut,
|
394
|
-
[ Opcodes.drop ],
|
395
|
-
|
396
395
|
// setup left
|
397
396
|
...left,
|
398
397
|
Opcodes.i32_to_u,
|
@@ -524,7 +523,7 @@ const truthy = (scope, wasm, type) => {
|
|
524
523
|
];
|
525
524
|
};
|
526
525
|
|
527
|
-
const performOp = (scope, op, left, right, leftType, rightType, _global = false, _name = '$
|
526
|
+
const performOp = (scope, op, left, right, leftType, rightType, _global = false, _name = '$undeclared', assign = false) => {
|
528
527
|
if (op === '||' || op === '&&' || op === '??') {
|
529
528
|
return performLogicOp(scope, op, left, right);
|
530
529
|
}
|
@@ -539,7 +538,8 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
|
|
539
538
|
if (!['==', '===', '!=', '!==', '>', '>=', '<', '<='].includes(op)) return number(NaN);
|
540
539
|
|
541
540
|
// else leave bool ops
|
542
|
-
// todo: convert string to number if string and number
|
541
|
+
// todo: convert string to number if string and number
|
542
|
+
// todo: string (>|>=|<|<=) string
|
543
543
|
// todo: string equality
|
544
544
|
}
|
545
545
|
|
@@ -569,13 +569,17 @@ const performOp = (scope, op, left, right, leftType, rightType, _global = false,
|
|
569
569
|
];
|
570
570
|
};
|
571
571
|
|
572
|
+
let binaryExpDepth = 0;
|
572
573
|
const generateBinaryExp = (scope, decl, _global, _name) => {
|
574
|
+
binaryExpDepth++;
|
575
|
+
|
573
576
|
const out = [
|
574
577
|
...performOp(scope, decl.operator, generate(scope, decl.left), generate(scope, decl.right), getNodeType(scope, decl.left), getNodeType(scope, decl.right), _global, _name)
|
575
578
|
];
|
576
579
|
|
577
580
|
if (valtype !== 'i32' && ['==', '===', '!=', '!==', '>', '>=', '<', '<='].includes(decl.operator)) out.push(Opcodes.i32_from_u);
|
578
581
|
|
582
|
+
binaryExpDepth--;
|
579
583
|
return out;
|
580
584
|
};
|
581
585
|
|
@@ -792,7 +796,7 @@ const generateLiteral = (scope, decl, global, name) => {
|
|
792
796
|
|
793
797
|
return makeArray(scope, {
|
794
798
|
rawElements
|
795
|
-
}, global, name, false, 'i16');
|
799
|
+
}, global, name, false, 'i16')[0];
|
796
800
|
|
797
801
|
default:
|
798
802
|
return todo(`cannot generate literal of type ${typeof decl.value}`);
|
@@ -941,10 +945,9 @@ const generateCall = (scope, decl, _global, _name) => {
|
|
941
945
|
if (codeLog) log('codegen', 'cloning unknown dynamic pointer');
|
942
946
|
|
943
947
|
// register array
|
944
|
-
makeArray(scope, {
|
948
|
+
const [ , pointer ] = makeArray(scope, {
|
945
949
|
rawElements: new Array(0)
|
946
950
|
}, _global, baseName, true, baseType === TYPES.string ? 'i16' : valtype);
|
947
|
-
pointer = arrays.get(baseName);
|
948
951
|
|
949
952
|
const [ local, isGlobal ] = lookupName(scope, baseName);
|
950
953
|
|
@@ -980,10 +983,9 @@ const generateCall = (scope, decl, _global, _name) => {
|
|
980
983
|
set: value => arrayUtil.setLength(pointer, value),
|
981
984
|
setI32: value => arrayUtil.setLengthI32(pointer, value)
|
982
985
|
}, generate(scope, decl.arguments[0] ?? DEFAULT_VALUE), protoLocal, (length, itemType) => {
|
983
|
-
|
986
|
+
return makeArray(scope, {
|
984
987
|
rawElements: new Array(length)
|
985
988
|
}, _global, _name, true, itemType);
|
986
|
-
return [ out, arrays.get(_name ?? '$undeclared') ];
|
987
989
|
}),
|
988
990
|
[ Opcodes.end ]
|
989
991
|
];
|
@@ -1578,12 +1580,12 @@ const makeArray = (scope, decl, global = false, name = '$undeclared', initEmpty
|
|
1578
1580
|
|
1579
1581
|
scope.memory = true;
|
1580
1582
|
|
1581
|
-
return out;
|
1583
|
+
return [ out, pointer ];
|
1582
1584
|
};
|
1583
1585
|
|
1584
1586
|
let arrays = new Map();
|
1585
1587
|
const generateArray = (scope, decl, global = false, name = '$undeclared', initEmpty = false) => {
|
1586
|
-
return makeArray(scope, decl, global, name, initEmpty, valtype);
|
1588
|
+
return makeArray(scope, decl, global, name, initEmpty, valtype)[0];
|
1587
1589
|
};
|
1588
1590
|
|
1589
1591
|
export const generateMember = (scope, decl, _global, _name) => {
|
@@ -1644,10 +1646,9 @@ export const generateMember = (scope, decl, _global, _name) => {
|
|
1644
1646
|
|
1645
1647
|
// string
|
1646
1648
|
|
1647
|
-
const newOut = makeArray(scope, {
|
1649
|
+
const [ newOut, newPointer ] = makeArray(scope, {
|
1648
1650
|
rawElements: new Array(1)
|
1649
1651
|
}, _global, _name, true, 'i16');
|
1650
|
-
const newPointer = arrays.get(_name ?? '$undeclared');
|
1651
1652
|
|
1652
1653
|
return [
|
1653
1654
|
// setup new/out array
|
@@ -1912,10 +1913,9 @@ const internalConstrs = {
|
|
1912
1913
|
|
1913
1914
|
// new Array(n)
|
1914
1915
|
|
1915
|
-
makeArray(scope, {
|
1916
|
+
const [ , pointer ] = makeArray(scope, {
|
1916
1917
|
rawElements: new Array(0)
|
1917
1918
|
}, global, name, true);
|
1918
|
-
const pointer = arrays.get(name ?? '$undeclared');
|
1919
1919
|
|
1920
1920
|
const arg = decl.arguments[0] ?? DEFAULT_VALUE;
|
1921
1921
|
|
package/package.json
CHANGED
package/runner/repl.js
CHANGED
@@ -51,12 +51,12 @@ const memoryToString = mem => {
|
|
51
51
|
let prev = '';
|
52
52
|
const run = async (source, _context, _filename, callback, run = true) => {
|
53
53
|
let toRun = prev + source.trim();
|
54
|
-
prev = toRun + ';\n';
|
54
|
+
// prev = toRun + ';\n';
|
55
55
|
|
56
56
|
const { exports, wasm, pages } = await compile(toRun, []);
|
57
57
|
fs.writeFileSync('out.wasm', Buffer.from(wasm));
|
58
58
|
|
59
|
-
if (exports.$) {
|
59
|
+
if (run && exports.$) {
|
60
60
|
lastMemory = exports.$;
|
61
61
|
lastPages = [...pages.keys()];
|
62
62
|
}
|
@@ -64,7 +64,7 @@ const run = async (source, _context, _filename, callback, run = true) => {
|
|
64
64
|
const ret = run ? exports.main() : undefined;
|
65
65
|
callback(null, ret);
|
66
66
|
|
67
|
-
|
67
|
+
if (source.includes(' = ') || source.includes('let ') || source.includes('var ') || source.includes('const ') || source.includes('function ')) prev = toRun + ';\n';
|
68
68
|
// prev = toRun + ';\n';
|
69
69
|
};
|
70
70
|
|