Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fe5130f

Browse files
committed
fixed 'return -1' bug #40
1 parent 865d452 commit fe5130f

File tree

5 files changed

+26
-3
lines changed

5 files changed

+26
-3
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.1.15",
3+
"version": "2.1.16",
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class Evaluator {
6060
}
6161
if (node.type === 'import') {
6262
// we can't use it here, because loader has to be promise
63-
throw new Error(`Import is not support with 'eval'. Use method 'evalAsync' instead`);
63+
throw new Error(`Import is not supported with 'eval'. Use method 'evalAsync' instead`);
6464
}
6565
try {
6666
lastResult = this.evalNode(node, blockContext);

‎src/interpreter.spec.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ describe('Interpreter', () => {
133133
});
134134

135135
it('json ignore last comma', () => {
136-
expect(JSON.stringify(e.eval('[{a:1,}, {a:2},]'))).toBe(JSON.stringify([{ a: 1, }, { a: 2 },]));
136+
expect(JSON.stringify(e.eval('[{a:1,}, {a:2},]'))).toBe(JSON.stringify([{ a: 1 }, { a: 2 }]));
137137
});
138138

139139
[{ a: 1 }, { a: 2 }];
@@ -995,5 +995,20 @@ describe('Interpreter', () => {
995995
expect(interpreter.eval(script)).toBe(11);
996996
});
997997

998+
it('return -1', async () => {
999+
const interpreter = Interpreter.create();
1000+
1001+
const script = `return -1`;
1002+
expect(await interpreter.evalAsync(script)).toBe(-1);
1003+
expect(interpreter.eval(script)).toBe(-1);
1004+
});
1005+
1006+
it('return -3.14', async () => {
1007+
const interpreter = Interpreter.create();
1008+
1009+
const script = `return -3.14`;
1010+
expect(await interpreter.evalAsync(script)).toBe(-3.14);
1011+
expect(interpreter.eval(script)).toBe(-3.14);
1012+
});
9981013
//
9991014
});

‎src/tokenizer/tokenizer.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,13 @@ describe('Tokenizer => ', () => {
112112
expect(tokens[4][0]).toBe('d');
113113
});
114114

115+
it('return -1', async () => {
116+
const tokens = new Tokenizer().tokenize('return -1');
117+
expect(tokens.length).toBe(2);
118+
expect(tokens[0][0]).toBe('return');
119+
expect(tokens[1][0]).toBe(-1);
120+
});
121+
115122
/*
116123
it('3 - -2', async () => {
117124
let tokens = new Tokenizer().tokenize('3 - -2')

‎src/tokenizer/tokenizer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ export class Tokenizer {
278278
const prevToken = currentTokens.length !== 0 ? currentTokens[currentTokens.length - 1] : null;
279279
return (
280280
prevToken === null ||
281+
getTokenValue(prevToken) === 'return' ||
281282
(getTokenType(prevToken) === TokenTypes.Operator && getTokenValue(prevToken) !== ')')
282283
);
283284
} else if (symbol === '.' && this.parseNumberOrNull(this.tokenText) !== null) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /