Skip to content

Commit c6520fc

Browse files
committed
Fix issue #4 #4
1 parent 7e63f08 commit c6520fc

File tree

3 files changed

+104
-33
lines changed

3 files changed

+104
-33
lines changed

src/transpiler/ScopeManager.class.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ export class ScopeManager {
6161
this.rootParams.add(name);
6262
}
6363
}
64+
removeContextBoundVar(name): void {
65+
// Remove a variable from the context-bound variables set
66+
if (this.contextBoundVars.has(name)) {
67+
this.contextBoundVars.delete(name);
68+
69+
// If it's also a root parameter, remove it from there too
70+
if (this.rootParams.has(name)) {
71+
this.rootParams.delete(name);
72+
}
73+
}
74+
}
6475
addArrayPatternElement(name: string): void {
6576
this.arrayPatternElements.add(name);
6677
}

src/transpiler/index.ts

Lines changed: 92 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -889,43 +889,101 @@ function transformReturnStatement(node: any, scopeManager: ScopeManager): void {
889889
return prop;
890890
});
891891
} else if (node.argument.type === 'Identifier') {
892+
transformIdentifier(node.argument, scopeManager);
893+
if (node.argument.type === 'Identifier') {
894+
addArrayAccess(node.argument, scopeManager);
895+
}
892896
// Handle identifier return values
893-
const [scopedName, kind] = scopeManager.getVariable(node.argument.name);
894-
node.argument = {
895-
type: 'MemberExpression',
896-
object: {
897-
type: 'MemberExpression',
898-
object: {
899-
type: 'Identifier',
900-
name: CONTEXT_NAME,
901-
},
902-
property: {
903-
type: 'Identifier',
904-
name: kind,
905-
},
906-
computed: false,
907-
},
908-
property: {
909-
type: 'Identifier',
910-
name: scopedName,
911-
},
912-
computed: false,
913-
};
914-
915-
// Add [0] array access
916-
node.argument = {
917-
type: 'MemberExpression',
918-
object: node.argument,
919-
property: {
920-
type: 'Literal',
921-
value: 0,
922-
},
923-
computed: true,
924-
};
897+
// const [scopedName, kind] = scopeManager.getVariable(node.argument.name);
898+
// node.argument = {
899+
// type: 'MemberExpression',
900+
// object: {
901+
// type: 'MemberExpression',
902+
// object: {
903+
// type: 'Identifier',
904+
// name: CONTEXT_NAME,
905+
// },
906+
// property: {
907+
// type: 'Identifier',
908+
// name: kind,
909+
// },
910+
// computed: false,
911+
// },
912+
// property: {
913+
// type: 'Identifier',
914+
// name: scopedName,
915+
// },
916+
// computed: false,
917+
// };
918+
919+
// // Add [0] array access
920+
// node.argument = {
921+
// type: 'MemberExpression',
922+
// object: node.argument,
923+
// property: {
924+
// type: 'Literal',
925+
// value: 0,
926+
// },
927+
// computed: true,
928+
// };
925929
}
926930

927931
if (curScope === 'fn') {
928932
//for nested functions : wrap the return argument in a CallExpression with math._precision(<statement>)
933+
// Process different types of return arguments
934+
if (
935+
node.argument.type === 'Identifier' &&
936+
scopeManager.isContextBound(node.argument.name) &&
937+
!scopeManager.isRootParam(node.argument.name)
938+
) {
939+
// For context-bound identifiers, add [0] array access if not already an array access
940+
node.argument = {
941+
type: 'MemberExpression',
942+
object: node.argument,
943+
property: {
944+
type: 'Literal',
945+
value: 0,
946+
},
947+
computed: true,
948+
};
949+
} else if (node.argument.type === 'MemberExpression') {
950+
// For member expressions, check if the object is context-bound
951+
if (
952+
node.argument.object.type === 'Identifier' &&
953+
scopeManager.isContextBound(node.argument.object.name) &&
954+
!scopeManager.isRootParam(node.argument.object.name)
955+
) {
956+
// Transform array indices first if not already transformed
957+
if (!node.argument._indexTransformed) {
958+
transformArrayIndex(node.argument, scopeManager);
959+
node.argument._indexTransformed = true;
960+
}
961+
}
962+
} else if (
963+
node.argument.type === 'BinaryExpression' ||
964+
node.argument.type === 'LogicalExpression' ||
965+
node.argument.type === 'ConditionalExpression' ||
966+
node.argument.type === 'CallExpression'
967+
) {
968+
// For complex expressions, walk the AST and transform all identifiers and expressions
969+
walk.recursive(node.argument, scopeManager, {
970+
Identifier(node: any, state: ScopeManager) {
971+
transformIdentifier(node, state);
972+
// Add array access if needed
973+
if (node.type === 'Identifier' && !node._arrayAccessed) {
974+
addArrayAccess(node, state);
975+
node._arrayAccessed = true;
976+
}
977+
},
978+
MemberExpression(node: any) {
979+
transformMemberExpression(node, '', scopeManager);
980+
},
981+
CallExpression(node: any, state: ScopeManager) {
982+
transformCallExpression(node, state);
983+
},
984+
});
985+
}
986+
929987
node.argument = {
930988
type: 'CallExpression',
931989
callee: {
@@ -1479,9 +1537,11 @@ function transformCallExpression(node: any, scopeManager: ScopeManager, namespac
14791537

14801538
function transformFunctionDeclaration(node: any, scopeManager: ScopeManager): void {
14811539
// Register function parameters as context-bound (but not as root params)
1540+
const boundParamNames = [];
14821541
node.params.forEach((param: any) => {
14831542
if (param.type === 'Identifier') {
14841543
scopeManager.addContextBoundVar(param.name, false);
1544+
boundParamNames.push(param.name);
14851545
}
14861546
});
14871547

tests/namespaces/array.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('Array', () => {
8787
// console.log('STDEV', part_stdev);
8888
// console.log('STDEV_BIASED', part_stdev_biased);
8989

90-
expect(part_sum).toEqual(expected_sum);
90+
expect(part_sum).toEqual(expected_sum); //FIXME : the sum is failing, need to check if the function implementation is wrong or the data set
9191
expect(part_first).toEqual(expected_first);
9292
expect(part_stdev).toEqual(expected_stdev);
9393
expect(part_stdev_biased).toEqual(expected_stdev_biased);

0 commit comments

Comments
 (0)