Skip to content

Commit 02b1cf1

Browse files
committed
fixed comparisson arithmetics AST
1 parent 92496f5 commit 02b1cf1

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

index.html

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@
2828
<body>
2929
<div class="container">
3030
<h4>JSPython development console</h4>
31-
<div id="editor">
32-
33-
x?.p1?.p2?.p3 or "N/A"
34-
35-
</div>
31+
<div id="editor">2 == 1/2+1/2 and 1/2+1/2 == 1</div>
3632
<!--
3733
def f(n):
3834
n * n

src/interpreter.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,9 @@ describe('Interpreter', () => {
332332
it('arithmetic + comparison', async () => {
333333
expect(await e.evaluate('1+2*3 == 5 or 1 > 3')).toBe(false)
334334
expect(await e.evaluate('1+2*3 == 5 or 1 < 3')).toBe(true)
335-
});
336335

336+
expect(await e.evaluate('2 == 1/2 + 1/2 and 1/2 + 1/2 == 1')).toBe(false)
337+
expect(await e.evaluate('(2 == 1/2 + 1/2) and (1/2 + 1/2 == 1)')).toBe(false)
338+
expect(await e.evaluate('(2 == (1/2 + 1/2)) and ((1/2 + 1/2) == 1)')).toBe(false)
339+
});
337340
});

src/parser/parser.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
GetSingleVarNode, FunctionCallNode, getTokenType, getTokenValue, isTokenTypeLiteral, getStartLine,
44
getStartColumn, getEndColumn, getEndLine, findOperators, splitTokens, DotObjectAccessNode, BracketObjectAccessNode,
55
findTokenValueIndex, FunctionDefNode, CreateObjectNode, ObjectPropertyInfo, CreateArrayNode, ArrowFuncDefNode,
6-
ExpressionOperators, IfNode, ForNode, WhileNode, ImportNode, NameAlias, ContinueNode, BreakNode, ReturnNode, CommentNode, getTokenLoc, OperationTypes, LogicalNodeItem, LogicalOperators, LogicalOpNode, ComparisonOperators
6+
ExpressionOperators, IfNode, ForNode, WhileNode, ImportNode, NameAlias, ContinueNode, BreakNode, ReturnNode, CommentNode,
7+
getTokenLoc, OperationTypes, LogicalNodeItem, LogicalOperators, LogicalOpNode, ComparisonOperators
78
} from '../common';
89
import { JspyParserError } from '../common/utils';
910

@@ -96,7 +97,6 @@ export class Parser {
9697
const comparisonOpIndexs: number[] = [];
9798
const assignTokenIndexes: number[] = [];
9899

99-
100100
if (getTokenType(firstToken) === TokenTypes.Comment) {
101101
ast.body.push(new CommentNode(getTokenValue(firstToken) as string, getTokenLoc(firstToken)));
102102
} else if (getTokenValue(firstToken) === 'def'
@@ -220,9 +220,7 @@ export class Parser {
220220
const source = this.createExpressionNode(assignTokens[1]);
221221
ast.body.push(new AssignNode(target, source, getTokenLoc(assignTokens[0][0])));
222222
} else if (findIndexes(instruction.tokens, OperationTypes.Logical, logicOpIndexes)) {
223-
ast.body.push(this.groupLogicalOperations(logicOpIndexes, instruction));
224-
} else if (findIndexes(instruction.tokens, OperationTypes.Comparison, comparisonOpIndexs)) {
225-
ast.body.push(this.groupComparisonOperations(comparisonOpIndexs, instruction));
223+
ast.body.push(this.groupLogicalOperations(logicOpIndexes, instruction.tokens));
226224
} else {
227225
ast.body.push(this.createExpressionNode(instruction.tokens))
228226
}
@@ -240,29 +238,29 @@ export class Parser {
240238
return a.slice(begin, end);
241239
}
242240

243-
private groupComparisonOperations(indexes: number[], instruction: InstructionLine): AstNode {
241+
private groupComparisonOperations(indexes: number[], tokens: Token[]): AstNode {
244242
let start = 0;
245243

246244
let leftNode: AstNode | null = null;
247245
for (let i = 0; i < indexes.length; i++) {
248-
const opToken = getTokenValue(instruction.tokens[indexes[i]]) as ComparisonOperators;
249-
leftNode = (leftNode) ? leftNode : this.createExpressionNode(this.sliceWithBrackets(instruction.tokens, start, indexes[i]))
246+
const opToken = getTokenValue(tokens[indexes[i]]) as ComparisonOperators;
247+
leftNode = (leftNode) ? leftNode : this.createExpressionNode(this.sliceWithBrackets(tokens, start, indexes[i]))
250248

251-
const endInd = (i + 1 < indexes.length) ? indexes[i + 1] : instruction.tokens.length;
252-
const rightNode = this.createExpressionNode(this.sliceWithBrackets(instruction.tokens, indexes[i] + 1, endInd))
249+
const endInd = (i + 1 < indexes.length) ? indexes[i + 1] : tokens.length;
250+
const rightNode = this.createExpressionNode(this.sliceWithBrackets(tokens, indexes[i] + 1, endInd))
253251

254-
leftNode = new BinOpNode(leftNode, opToken, rightNode, getTokenLoc(instruction.tokens[0]));
252+
leftNode = new BinOpNode(leftNode, opToken, rightNode, getTokenLoc(tokens[0]));
255253
}
256254

257255
return leftNode as AstNode;
258256
}
259257

260-
private groupLogicalOperations(logicOp: number[], instruction: InstructionLine) {
258+
private groupLogicalOperations(logicOp: number[], tokens: Token[]) {
261259
let start = 0;
262260
const logicItems: LogicalNodeItem[] = [];
263261
for (let i = 0; i < logicOp.length; i++) {
264-
const opToken = instruction.tokens[logicOp[i]];
265-
const logicalSlice = this.sliceWithBrackets(instruction.tokens, start, logicOp[i]);
262+
const opToken = tokens[logicOp[i]];
263+
const logicalSlice = this.sliceWithBrackets(tokens, start, logicOp[i]);
266264
logicItems.push({
267265
node: this.createExpressionNode(logicalSlice),
268266
op: getTokenValue(opToken) as LogicalOperators
@@ -272,10 +270,10 @@ export class Parser {
272270
}
273271

274272
logicItems.push({
275-
node: this.createExpressionNode(this.sliceWithBrackets(instruction.tokens, start, instruction.tokens.length))
273+
node: this.createExpressionNode(this.sliceWithBrackets(tokens, start, tokens.length))
276274
} as LogicalNodeItem);
277275

278-
const lop = new LogicalOpNode(logicItems, getTokenLoc(instruction.tokens[0]));
276+
const lop = new LogicalOpNode(logicItems, getTokenLoc(tokens[0]));
279277
return lop;
280278
}
281279

@@ -368,6 +366,12 @@ export class Parser {
368366
return new ArrowFuncDefNode(funcAst, params, getTokenLoc(tokens[0]));
369367
}
370368

369+
// comparison operations
370+
const comparissonIndexes = findOperators(tokens, OperationTypes.Comparison);
371+
if (comparissonIndexes.length) {
372+
return this.groupComparisonOperations(comparissonIndexes, tokens);
373+
}
374+
371375
// create arithmetic expression
372376
const ops = findOperators(tokens);
373377
if (ops.length) {

0 commit comments

Comments
 (0)