Skip to content

Commit 24b9bad

Browse files
committed
Feat: Add Errors for Undeclared Constants
1 parent 20cb359 commit 24b9bad

File tree

5 files changed

+22
-2
lines changed

5 files changed

+22
-2
lines changed

src/tracer/__tests__/tracer_debug.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ function h(f, x) {
112112
// TODO: Check for -Infinity
113113
test('general', () => {
114114
const code = `
115-
-Infinity;
115+
1 + x;
116116
`
117117
const program = parse(code, { ecmaVersion: 10, locations: true })!
118118
const steps = getSteps(convert(program), { stepLimit: 200 })

src/tracer/nodes/Expression/BinaryExpression.ts

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { redex } from '../..'
44
import { StepperLiteral } from './Literal'
55
import { StepperExpression, StepperPattern } from '..'
66
import { convert } from '../../generator'
7+
import { isBuiltinFunction } from '../../builtins'
78
export class StepperBinaryExpression implements BinaryExpression, StepperBaseNode {
89
type: 'BinaryExpression'
910
operator: BinaryOperator
@@ -46,6 +47,10 @@ export class StepperBinaryExpression implements BinaryExpression, StepperBaseNod
4647
}
4748

4849
isContractible(): boolean {
50+
if (this.freeNames().filter(name => !isBuiltinFunction(name)).length > 0) {
51+
throw new Error(`Line ${this.loc?.start.line || 0}: Name ${this.freeNames()[0]} not declared or assigned.`);
52+
}
53+
4954
if (this.left.type !== 'Literal' || this.right.type !== 'Literal') {
5055
return false;
5156
}

src/tracer/nodes/Expression/ConditionalExpression.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { StepperBaseNode } from '../../interface'
33
import { redex } from '../..'
44
import { StepperExpression, StepperPattern } from '..'
55
import { convert } from '../../generator'
6+
import { isBuiltinFunction } from '../../builtins'
67

78
export class StepperConditionalExpression implements ConditionalExpression, StepperBaseNode {
89
type: 'ConditionalExpression'
@@ -46,6 +47,10 @@ export class StepperConditionalExpression implements ConditionalExpression, Step
4647
}
4748

4849
isContractible(): boolean {
50+
if (this.freeNames().filter(name => !isBuiltinFunction(name)).length > 0) {
51+
throw new Error(`Line ${this.loc?.start.line || 0}: Name ${this.freeNames()[0]} not declared or assigned.`);
52+
}
53+
4954
if (this.test.type !== 'Literal') return false
5055
const test_value = this.test.value
5156
if (typeof test_value !== 'boolean') {

src/tracer/nodes/Expression/LogicalExpression.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { redex } from '../..'
44
import { StepperLiteral } from './Literal'
55
import { StepperExpression, StepperPattern } from '..'
66
import { convert } from '../../generator'
7+
import { isBuiltinFunction } from '../../builtins'
78

89
export class StepperLogicalExpression implements LogicalExpression, StepperBaseNode {
910
type: 'LogicalExpression'
@@ -47,6 +48,10 @@ export class StepperLogicalExpression implements LogicalExpression, StepperBaseN
4748
}
4849

4950
isContractible(): boolean {
51+
if (this.freeNames().filter(name => !isBuiltinFunction(name)).length > 0) {
52+
throw new Error(`Line ${this.loc?.start.line || 0}: Name ${this.freeNames()[0]} not declared or assigned.`);
53+
}
54+
5055
if (this.left.type === 'Literal') {
5156
const leftType = typeof this.left.value;
5257

@@ -57,7 +62,7 @@ export class StepperLogicalExpression implements LogicalExpression, StepperBaseN
5762
redex.preRedex = [this];
5863
return true;
5964
}
60-
65+
6166
return false;
6267
}
6368

src/tracer/nodes/Expression/UnaryExpression.ts

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { redex } from '../..'
44
import { StepperLiteral } from './Literal'
55
import { convert } from '../../generator'
66
import { StepperExpression, StepperPattern } from '..'
7+
import { isBuiltinFunction } from '../../builtins'
78

89
export class StepperUnaryExpression implements UnaryExpression, StepperBaseNode {
910
type: 'UnaryExpression'
@@ -68,6 +69,10 @@ export class StepperUnaryExpression implements UnaryExpression, StepperBaseNode
6869
}
6970

7071
isContractible(): boolean {
72+
if (this.freeNames().filter(name => !isBuiltinFunction(name)).length > 0) {
73+
throw new Error(`Line ${this.loc?.start.line || 0}: Name ${this.freeNames()[0]} not declared or assigned.`);
74+
}
75+
7176
if (this.argument.type !== 'Literal') return false
7277

7378
const valueType = typeof this.argument.value;

0 commit comments

Comments
 (0)