Skip to content

Commit 3103841

Browse files
committed
fixed all unit tests, apart from 'promise' 5-139
1 parent a56e4bb commit 3103841

File tree

6 files changed

+115
-107
lines changed

6 files changed

+115
-107
lines changed

index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
<h4>JSPython development console</h4>
3131
<div id="editor">
3232

33-
def func1():
34-
# test
35-
15 + 25
36-
func1()
33+
x = 5
34+
if 2 == 2:
35+
return x
36+
37+
x + 10
3738

3839
</div>
3940
<!--

src/common/ast-types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class BinOpNode extends AstNode {
171171

172172
export interface AstBlock {
173173
name: string;
174-
type: 'module' | 'func' | 'if' | 'for' | 'trycatch'
174+
type: 'module' | 'func' | 'if' | 'for' | 'while' | 'trycatch'
175175
funcs: FunctionDefNode[];
176176
body: AstNode[];
177177
}

src/evaluator/evaluator.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@ export class Evaluator {
2727

2828
if (blockContext.returnCalled) {
2929
const res = blockContext.returnObject;
30-
blockContext.returnCalled = false;
31-
blockContext.returnObject = null;
30+
31+
// stop processing return
32+
if(ast.type == 'func' || ast.type == 'module'){
33+
blockContext.returnCalled = false;
34+
blockContext.returnObject = null;
35+
}
3236
return res;
3337
}
3438

@@ -63,8 +67,11 @@ export class Evaluator {
6367
lastResult = this.evalNode(node, blockContext);
6468
if (blockContext.returnCalled) {
6569
const res = blockContext.returnObject;
66-
blockContext.returnCalled = false;
67-
blockContext.returnObject = null;
70+
// stop processing return
71+
if(ast.type == 'func' || ast.type == 'module'){
72+
blockContext.returnCalled = false;
73+
blockContext.returnObject = null;
74+
}
6875
return res;
6976
}
7077

@@ -81,7 +88,8 @@ export class Evaluator {
8188

8289
private jspyFuncInvoker(funcDef: FuncDefNode, context: BlockContext, ...args: unknown[]): unknown {
8390

84-
const ast = funcDef.funcAst;
91+
const ast = Object.assign({}, funcDef.funcAst);
92+
ast.type = 'func';
8593

8694
const blockContext = {
8795
namelessFuncsCount: 0,
@@ -150,9 +158,9 @@ export class Evaluator {
150158
if (node.type === 'if') {
151159
const ifNode = node as IfNode;
152160
if (this.evalNode(ifNode.conditionNode, blockContext)) {
153-
this.evalBlock({ body: ifNode.ifBody } as AstBlock, blockContext);
161+
this.evalBlock({ type: 'if', body: ifNode.ifBody } as AstBlock, blockContext);
154162
} else if (ifNode.elseBody) {
155-
this.evalBlock({ body: ifNode.elseBody } as AstBlock, blockContext);
163+
this.evalBlock({ type: 'if', body: ifNode.elseBody } as AstBlock, blockContext);
156164
}
157165

158166
return;
@@ -185,7 +193,7 @@ export class Evaluator {
185193

186194
for (let item of array) {
187195
blockContext.blockScope.set(forNode.itemVarName, item);
188-
this.evalBlock({ body: forNode.body } as AstBlock, blockContext);
196+
this.evalBlock({ type:'for', body: forNode.body } as AstBlock, blockContext);
189197
if (blockContext.continueCalled) { blockContext.continueCalled = false; }
190198
if (blockContext.breakCalled) { break; }
191199
}
@@ -194,10 +202,10 @@ export class Evaluator {
194202
}
195203

196204
if (node.type === 'while') {
197-
const forNode = node as WhileNode;
205+
const whileNode = node as WhileNode;
198206

199-
while (this.evalNode(forNode.condition, blockContext)) {
200-
this.evalBlock({ body: forNode.body } as AstBlock, blockContext);
207+
while (this.evalNode(whileNode.condition, blockContext)) {
208+
this.evalBlock({ type:'while', body: whileNode.body } as AstBlock, blockContext);
201209

202210
if (blockContext.continueCalled) { blockContext.continueCalled = false; }
203211
if (blockContext.breakCalled) { break; }

0 commit comments

Comments
 (0)