Skip to content

Commit 5c56527

Browse files
committed
raising error variable is not defined
1 parent ae8993b commit 5c56527

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

src/evaluator/evaluator.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,13 @@ export class Evaluator {
193193
}
194194

195195
if (node.type === "getSingleVar") {
196+
const name = (node as GetSingleVarNode).name;
197+
196198
const value = blockContext.blockScope.get((node as GetSingleVarNode).name);
197-
return value === undefined ? null : value;
199+
if(value === undefined){
200+
throw new Error(`Variable ${name} is not defined.`);
201+
}
202+
return value;
198203
}
199204

200205
if (node.type === "binOp") {

src/evaluator/evaluatorAsync.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,12 @@ export class EvaluatorAsync {
200200
}
201201

202202
if (node.type === "getSingleVar") {
203-
const value = blockContext.blockScope.get((node as GetSingleVarNode).name);
204-
return value === undefined? null : value;
203+
const name = (node as GetSingleVarNode).name;
204+
const value = blockContext.blockScope.get(name);
205+
if(value === undefined){
206+
throw new Error(`Variable ${name} is not defined.`);
207+
}
208+
return value;
205209
}
206210

207211
if (node.type === "binOp") {

src/interpreter.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ describe('Interpreter', () => {
290290
});
291291

292292
it('chaining funcCall with null coelsing', () => {
293-
expect(e.eval("p?.f()?.sdsd")).toBe(null);
294-
expect(e.eval("p?.f()?.sdsd or 5")).toBe(5);
293+
expect(e.eval("p={f: ()=>null}\np?.f()?.sdsd")).toBe(null);
294+
expect(e.eval("p={f: ()=>null}\np?.f()?.sdsd or 5")).toBe(5);
295295
});
296296

297297
it('comparison operations', () => {
@@ -323,10 +323,10 @@ describe('Interpreter', () => {
323323
});
324324

325325
it('conditionals', async () => {
326-
expect(await e.evaluate('x == null')).toBe(true)
327-
expect(await e.evaluate('x?.p1?.p == null')).toBe(true)
328-
expect(await e.evaluate('x != null and x.length >0')).toBe(false)
329-
expect(await e.evaluate('x?.p1?.p != null and x.length >0')).toBe(false)
326+
expect(await e.evaluate('x = null\nx == null')).toBe(true)
327+
expect(await e.evaluate('x = null\nx?.p1?.p == null')).toBe(true)
328+
expect(await e.evaluate('x = null\nx != null and x.length >0')).toBe(false)
329+
expect(await e.evaluate('x = null\nx?.p1?.p != null and x.length >0')).toBe(false)
330330
});
331331

332332
it('arithmetic + comparison', async () => {

src/interpreter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ export class Interpreter {
2626
return new Interpreter();
2727
}
2828

29+
jsPythonInfo(){
30+
return this.initialScope.jsPython();
31+
}
32+
2933
tokenize(script: string): Token[] {
3034
const tokenizer = new Tokenizer();
3135
return tokenizer.tokenize(script);

0 commit comments

Comments
 (0)