Skip to content

Commit 6a27e1b

Browse files
committed
added trace information to 'for' operation
1 parent f552158 commit 6a27e1b

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jspython-interpreter",
3-
"version": "2.0.12",
3+
"version": "2.0.14",
44
"description": "JSPython is a javascript implementation of Python language that runs within web browser or NodeJS environment",
55
"keywords": [
66
"python",

src/evaluator/evaluator.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,21 @@ export class Evaluator {
6767

6868
const blockContext = cloneContext(context);
6969

70-
// set parameters into new scope, based incomming arguments
71-
for (let i = 0; i < funcDef.params?.length || 0; i++) {
72-
const argValue = args?.length > i ? args[i] : null;
73-
blockContext.blockScope.set(funcDef.params[i], argValue);
70+
71+
for (let i = 0; i < args?.length || 0; i++) {
72+
if (i >= funcDef.params.length) {
73+
break;
74+
// throw new Error('Too many parameters provided');
75+
}
76+
blockContext.blockScope.set(funcDef.params[i], args[i]);
7477
}
7578

79+
// // set parameters into new scope, based incomming arguments
80+
// for (let i = 0; i < funcDef.params?.length || 0; i++) {
81+
// const argValue = args?.length > i ? args[i] : null;
82+
// blockContext.blockScope.set(funcDef.params[i], argValue);
83+
// }
84+
7685
return this.evalBlock(ast, blockContext);
7786
}
7887

@@ -200,12 +209,22 @@ export class Evaluator {
200209

201210
const array = this.evalNode(forNode.sourceArray, blockContext) as unknown[] | string;
202211

203-
for (let item of array) {
204-
blockContext.blockScope.set(forNode.itemVarName, item);
205-
this.evalBlock({ name: blockContext.moduleName, type: 'for', body: forNode.body } as AstBlock, blockContext);
206-
if (blockContext.continueCalled) { blockContext.continueCalled = false; }
207-
if (blockContext.breakCalled) { break; }
212+
try {
213+
for (let i = 0; i < array.length; i++) {
214+
const item = array[i];
215+
console.log('**DEBUG:', array.length, i);
216+
217+
blockContext.blockScope.set(forNode.itemVarName, item);
218+
this.evalBlock({ name: blockContext.moduleName, type: 'for', body: forNode.body } as AstBlock, blockContext);
219+
if (blockContext.continueCalled) { blockContext.continueCalled = false; }
220+
if (blockContext.breakCalled) { break; }
221+
}
222+
223+
console.log('**FOR finished.');
224+
} catch (err) {
225+
console.log('**FOR FAILED:', err?.message || err);
208226
}
227+
209228
if (blockContext.breakCalled) { blockContext.breakCalled = false; }
210229
return;
211230
}

src/evaluator/evaluatorAsync.ts

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,21 @@ export class EvaluatorAsync {
7676

7777
const blockContext = cloneContext(context);
7878

79-
// set parameters into new scope, based incomming arguments
80-
for (let i = 0; i < funcDef.params?.length || 0; i++) {
81-
const argValue = args?.length > i ? args[i] : null;
82-
blockContext.blockScope.set(funcDef.params[i], argValue);
79+
for (let i = 0; i < args?.length || 0; i++) {
80+
if (i >= funcDef.params.length) {
81+
break;
82+
// throw new Error('Too many parameters provided');
83+
}
84+
blockContext.blockScope.set(funcDef.params[i], args[i]);
8385
}
8486

87+
88+
// // set parameters into new scope, based incomming arguments
89+
// for (let i = 0; i < funcDef.params?.length || 0; i++) {
90+
// const argValue = args?.length > i ? args[i] : null;
91+
// blockContext.blockScope.set(funcDef.params[i], argValue);
92+
// }
93+
8594
return await this.evalBlockAsync(ast, blockContext);
8695
}
8796

@@ -208,13 +217,24 @@ export class EvaluatorAsync {
208217
const forNode = node as ForNode;
209218

210219
const array = await this.evalNodeAsync(forNode.sourceArray, blockContext) as unknown[] | string;
220+
try {
211221

212-
for (let item of array) {
213-
blockContext.blockScope.set(forNode.itemVarName, item);
214-
await this.evalBlockAsync({ name: blockContext.moduleName, type: 'for', body: forNode.body } as AstBlock, blockContext);
215-
if (blockContext.continueCalled) { blockContext.continueCalled = false; }
216-
if (blockContext.breakCalled) { break; }
222+
for (let i = 0; i < array.length; i++) {
223+
const item = array[i];
224+
console.log('**DEBUG:', array.length, i);
225+
226+
blockContext.blockScope.set(forNode.itemVarName, item);
227+
await this.evalBlockAsync({ name: blockContext.moduleName, type: 'for', body: forNode.body } as AstBlock, blockContext);
228+
if (blockContext.continueCalled) { blockContext.continueCalled = false; }
229+
if (blockContext.breakCalled) { break; }
230+
}
231+
232+
console.log('**FOR finished.');
233+
} catch (err) {
234+
console.log('**FOR FAILED:', err?.message || err);
217235
}
236+
237+
218238
if (blockContext.breakCalled) { blockContext.breakCalled = false; }
219239
return;
220240
}

0 commit comments

Comments
 (0)