Skip to content

Commit e596e75

Browse files
committed
Feat: Add Support for NaN and Infinity
1 parent 8f6d7b0 commit e596e75

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

Diff for: src/tracer/__tests__/tracer_debug.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ function h(f, x) {
109109
})
110110

111111

112+
// TODO: Check for -Infinity
112113
test('general', () => {
113114
const code = `
114-
3 % 0;
115+
-NaN;
115116
`
116117
const program = parse(code, { ecmaVersion: 10 })!
117118
const steps = getSteps(convert(program), { stepLimit: 200 })

Diff for: src/tracer/generator.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,15 @@ const nodeConverters: { [Key: string]: (node: any) => StepperBaseNode } = {
5151
Program: (node: es.Program) => StepperProgram.create(node),
5252
VariableDeclaration: (node: es.VariableDeclaration) => StepperVariableDeclaration.create(node),
5353
VariableDeclarator: (node: es.VariableDeclarator) => StepperVariableDeclarator.create(node),
54-
Identifier: (node: es.Identifier) => StepperIdentifier.create(node),
54+
Identifier: (node: es.Identifier) => {
55+
if (node.name === 'NaN') {
56+
return new StepperLiteral(NaN, 'NaN');
57+
} else if (node.name === 'Infinity') {
58+
return new StepperLiteral(Infinity, 'Infinity');
59+
} else {
60+
return StepperIdentifier.create(node);
61+
}
62+
},
5563
BlockStatement: (node: es.BlockStatement) => StepperBlockStatement.create(node),
5664
IfStatement: (node: es.IfStatement) => StepperIfStatement.create(node)
5765
}

Diff for: src/tracer/nodes/Expression/BinaryExpression.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,6 @@ export class StepperBinaryExpression implements BinaryExpression, StepperBaseNod
5959
}
6060
return ret
6161
} else if (left_type === 'number' && right_type === 'number') {
62-
if (this.operator === '/' && this.right.value === 0) {
63-
throw new Error('Division by zero');
64-
}
65-
66-
if (this.operator === '%' && this.right.value === 0) {
67-
throw new Error('Modulo by zero');
68-
}
69-
7062
const ret =
7163
['*', '+', '/', '-', '===', '!==', '<', '>', '<=', '>=', '%'].includes(this.operator as string)
7264
if (ret) {
@@ -120,7 +112,7 @@ export class StepperBinaryExpression implements BinaryExpression, StepperBaseNod
120112
? left! >= right!
121113
: left! > right!
122114

123-
let ret = new StepperLiteral(value)
115+
let ret = new StepperLiteral(value, value !== null ? value.toString() : "null")
124116
redex.postRedex = [ret]
125117
return ret
126118
}

Diff for: src/tracer/nodes/Expression/UnaryExpression.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class StepperUnaryExpression implements UnaryExpression, StepperBaseNode
9595
redex.postRedex = [ret]
9696
return ret
9797
} else if (this.operator === '-') {
98-
const ret = new StepperLiteral(-(operand as number))
98+
const ret = new StepperLiteral(-(operand as number), (-(operand as number)).toString())
9999
redex.postRedex = [ret]
100100
return ret
101101
}

0 commit comments

Comments
 (0)