Skip to content

Commit c4f2fd0

Browse files
author
Wangdahai
committed
update error handling logic on stdlib
1 parent 6c3617f commit c4f2fd0

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/stdlib.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Value } from "./cse-machine/stash";
55
import { gamma, lgamma, erf } from 'mathjs';
66
import { addPrint } from "./cse-machine/interpreter";
77
import { handleRuntimeError } from "./cse-machine/utils";
8-
import { MissingRequiredPositionalError, TooManyPositionalArgumentsError, ValueError, TypeError } from "./errors/errors";
8+
import { MissingRequiredPositionalError, TooManyPositionalArgumentsError, ValueError, TypeError, ZeroDivisionError } from "./errors/errors";
99
import { ControlItem } from "./cse-machine/control";
1010
import { Context } from "./cse-machine/context";
1111
import * as es from 'estree';
@@ -144,7 +144,7 @@ export function _int_from_string(args: Value[], source: string, command: Control
144144

145145
// base should be in between 2 and 36
146146
if (base < 2 || base > 36) {
147-
throw new Error(`_int_from_string: base must be in [2..36], got ${base}`);
147+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "_int_from_string"));
148148
}
149149

150150
let str = strVal.value as string;
@@ -163,7 +163,7 @@ export function _int_from_string(args: Value[], source: string, command: Control
163163
// The remaining portion must consist of valid characters for the specified base.
164164
const parsedNumber = parseInt(str, base);
165165
if (isNaN(parsedNumber)) {
166-
throw new Error(`_int_from_string: cannot parse "${strVal.value}" with base ${base}`);
166+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "_int_from_string"));
167167
}
168168

169169
const result: bigint = sign * BigInt(parsedNumber);
@@ -241,10 +241,10 @@ export function isinstance(args: Value[], source: string, command: ControlItem,
241241
expectedType = 'NoneType';
242242
break;
243243
default:
244-
throw new Error(`isinstance: unknown type '${classinfo.value}'`);
244+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "isinstance"));
245+
return;
245246
}
246247
} else {
247-
// TODO: If the value is not in string format, additional handling can be added as needed.
248248
handleRuntimeError(context, new TypeError(source, command as es.Node, context, args[0].type, "string"));
249249
return;
250250
}
@@ -274,7 +274,7 @@ export function math_acos(args: Value[], source: string, command: ControlItem, c
274274
}
275275

276276
if (num < -1 || num > 1) {
277-
throw new Error(`math_acos: argument must be in the interval [-1, 1], but got ${num}`);
277+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_acos"));
278278
}
279279

280280
const result = Math.acos(num);
@@ -302,7 +302,7 @@ export function math_acosh(args: Value[], source: string, command: ControlItem,
302302
}
303303

304304
if (num < 1) {
305-
throw new Error(`math_acosh: argument must be greater than or equal to 1, but got ${num}`);
305+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_acosh"));
306306
}
307307

308308
const result = Math.acosh(num);
@@ -329,7 +329,7 @@ export function math_asin(args: Value[], source: string, command: ControlItem, c
329329
}
330330

331331
if (num < -1 || num > 1) {
332-
throw new Error(`math_asin: argument must be in the interval [-1, 1], but got ${num}`);
332+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_asin"));
333333
}
334334

335335
const result = Math.asin(num);
@@ -434,7 +434,7 @@ export function math_atanh(args: Value[], source: string, command: ControlItem,
434434
}
435435

436436
if (num <= -1 || num >= 1) {
437-
throw new Error(`math_atanh: argument must be in the interval (-1, 1), but got ${num}`);
437+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_atanh"));
438438
}
439439

440440
const result = Math.atanh(num);
@@ -593,7 +593,7 @@ export function math_comb(args: Value[], source: string, command: ControlItem, c
593593
const kVal = BigInt(k.value);
594594

595595
if (nVal < 0 || kVal < 0) {
596-
throw new Error(`comb: n and k must be non-negative, got n=${nVal}, k=${kVal}`);
596+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_comb"));
597597
}
598598

599599
if (kVal > nVal) {
@@ -626,7 +626,7 @@ export function math_factorial(args: Value[], source: string, command: ControlIt
626626
const nVal = BigInt(n.value);
627627

628628
if (nVal < 0) {
629-
throw new Error(`factorial: argument must be non-negative, but got ${nVal}`);
629+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_factorial"));
630630
}
631631

632632
// 0! = 1
@@ -781,7 +781,7 @@ export function math_perm(args: Value[], source: string, command: ControlItem, c
781781
}
782782

783783
if (n < 0 || k < 0) {
784-
throw new Error(`perm: n and k must be non-negative, got n=${n}, k=${k}`);
784+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_perm"));
785785
}
786786

787787
if (k > n) {
@@ -952,7 +952,7 @@ export function math_fmod(args: Value[], source: string, command: ControlItem, c
952952

953953
// Divisor cannot be zero
954954
if (yVal === 0) {
955-
throw new Error("fmod: divisor (y) must not be zero");
955+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_fmod"));
956956
}
957957

958958
// JavaScript's % operator behaves similarly to C's fmod
@@ -1009,7 +1009,8 @@ export function math_remainder(args: Value[], source: string, command: ControlIt
10091009
}
10101010

10111011
if (yValue === 0) {
1012-
throw new Error(`remainder: divisor y must not be zero`);
1012+
1013+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_remainder"));
10131014
}
10141015

10151016
const quotient = xValue / yValue;
@@ -1316,7 +1317,7 @@ export function math_log(args: Value[], source: string, command: ControlItem, co
13161317
}
13171318

13181319
if (num <= 0) {
1319-
throw new Error(`math_log: argument must be positive, but got ${num}`);
1320+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_log"));
13201321
}
13211322

13221323
if (args.length === 1) {
@@ -1334,7 +1335,7 @@ export function math_log(args: Value[], source: string, command: ControlItem, co
13341335
baseNum = Number(baseArg.value);
13351336
}
13361337
if (baseNum <= 0) {
1337-
throw new Error(`math_log: base must be positive, but got ${baseNum}`);
1338+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_log"));
13381339
}
13391340

13401341
const result = Math.log(num) / Math.log(baseNum);
@@ -1359,7 +1360,7 @@ export function math_log10(args: Value[], source: string, command: ControlItem,
13591360
num = Number(x.value);
13601361
}
13611362
if (num <= 0) {
1362-
throw new Error(`math_log10: argument must be positive, but got ${num}`);
1363+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_log10"));
13631364
}
13641365

13651366
const result = Math.log10(num);
@@ -1384,7 +1385,7 @@ export function math_log1p(args: Value[], source: string, command: ControlItem,
13841385
num = Number(x.value);
13851386
}
13861387
if (1 + num <= 0) {
1387-
throw new Error(`math_log1p: 1 + argument must be positive, but got 1 + ${num} = ${1 + num}`);
1388+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_log1p"));
13881389
}
13891390

13901391
const result = Math.log1p(num);
@@ -1409,7 +1410,7 @@ export function math_log2(args: Value[], source: string, command: ControlItem, c
14091410
num = Number(x.value);
14101411
}
14111412
if (num <= 0) {
1412-
throw new Error(`math_log2: argument must be positive, but got ${num}`);
1413+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_log2"));
14131414
}
14141415

14151416
const result = Math.log2(num);
@@ -1435,7 +1436,7 @@ export function math_pow(args: Value[], source: string, command: ControlItem, co
14351436
let baseNum: number;
14361437
if (base.type === 'number') {
14371438
baseNum = base.value;
1438-
} else { // 'bigint'
1439+
} else {
14391440
baseNum = Number(base.value);
14401441
}
14411442

@@ -1585,7 +1586,7 @@ export function math_sqrt(args: Value[], source: string, command: ControlItem, c
15851586
}
15861587

15871588
if (num < 0) {
1588-
throw new Error(`math_sqrt: argument must be non-negative, but got ${num}`);
1589+
handleRuntimeError(context, new ValueError(source, command as es.Node, context, "math_sqrt"));
15891590
}
15901591

15911592
const result = Math.sqrt(num);
@@ -1863,7 +1864,7 @@ export function str(args: Value[], source: string, command: ControlItem, context
18631864
}
18641865

18651866
export function input(args: Value[], source: string, command: ControlItem, context: Context): Value {
1866-
// TODO: call conductor to receive user input
1867+
// TODO: : call conductor to receive user input
18671868
}
18681869

18691870
export function print(args: Value[], source: string, command: ControlItem, context: Context) {

0 commit comments

Comments
 (0)