Skip to content
This repository was archived by the owner on Oct 13, 2024. It is now read-only.

Commit 974f60c

Browse files
authored
Merge pull request #26 from shashankbhosagi/main
⚡ Improved Backend Error Message
2 parents 056ca68 + dd15e5e commit 974f60c

File tree

9 files changed

+33
-37
lines changed

9 files changed

+33
-37
lines changed

BackEnd/Interpreter/Expressions/Unary_expression.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import { evaluate } from "../interpreter.ts";
88
* @param rhs The number value to negate.
99
* @returns The negated number value.
1010
*/
11-
export const evaluate_minus = (
12-
rhs: NumberVal,
13-
): NumberVal => {
11+
export const evaluate_minus = (rhs: NumberVal): NumberVal => {
1412
const res = -1 * rhs.value;
1513
return { type: "number", value: res } as NumberVal;
1614
};
@@ -30,5 +28,5 @@ export const evaluate_minus_expression = (
3028
if (expression.type === "number") {
3129
return evaluate_minus(expression as NumberVal);
3230
}
33-
throw `The RHS to <MINUS> operator should be of type number`;
31+
throw `RunTimeError: The RHS to <MINUS> operator should be of type number`;
3432
};

BackEnd/Interpreter/Statements/Conditional_Jump/If_Else.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ export const evaluate_if_statement = (
197197
case "boolean":
198198
return evaluate_boolean_if_statement(condition as BooleanVal, stmt, env);
199199
default:
200-
throw new Error(
201-
"Condition in if statement must be a boolean or integer value",
202-
);
200+
throw `RunTimeError: Condition in if statement must be a boolean or integer value`;
203201
}
204202
};
205203

BackEnd/Interpreter/Statements/Conditional_Jump/Switch_case.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const evaluate_switch_statement = (
6161
}
6262
// Evaluate the default case if no matching case is found
6363
} else {
64-
throw "Expected Number in switch case as in switch(expression) expression is of type number";
64+
throw `RunTimeError: Expected Number in multiverse madness, as in multiverse(expression) the expression is of type number`;
6565
}
6666
}
6767
for (const consequent of switchStmt.default) {
@@ -126,7 +126,7 @@ export const evaluate_switch_statement = (
126126
}
127127
// Evaluate the default case if no matching case is found
128128
} else {
129-
throw "Expected string in switch case as in switch(expression) expression is of type string";
129+
throw `Expected String in multiverse madness, as in multiverse(expression) the expression is of type String`;
130130
}
131131
}
132132
for (const consequent of switchStmt.default) {

BackEnd/Interpreter/Statements/Declaration_statements.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@ export const evaluate_array_declaration = (
4444
const values_provided = declaration.values.length;
4545

4646
if (size_expr.type !== "number") {
47-
throw `error from array Shas`;
47+
throw `RunTimeError: The operand for subscript operator is expected to be of type number but recieved ${size_expr.type} at '${declaration.name}'`;
4848
}
4949
const arr_size = (evaluate(declaration.size, env) as NumberVal).value;
5050
if (arr_size > 10000000) {
51-
throw `Memory limit exceeded`;
51+
throw `RunTimeError: Segmentation Fault expected array size to be less than 1e7 at '${declaration.name}'`;
5252
} else if (arr_size <= 0) {
53-
throw `Invalid Array size`;
53+
throw `RunTimeError: Invalid Array size ${arr_size} at '${declaration.name}'`;
5454
}
5555
if (values_provided > arr_size) {
56-
throw `Size of array exceeded`;
56+
throw `RunTimeError: Excess elements in array initializer at '${declaration.name}'. Provided array size ${arr_size} while number of elements in ${values_provided}`;
5757
}
5858
let count: number = values_provided;
5959
while (count < arr_size) {

BackEnd/Interpreter/Statements/Loop/For_loop.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ export const evaluate_for_loop_statement = (
3434
step = evaluate(stmt.step as NumericLiteral, env) as NumberVal;
3535
}
3636
if (step.value <= 0) {
37-
throw `TLE(Exception): The step value in wakandaFor must be a positive non-zero value.`;
37+
throw `RunTimeError: The step value in wakandaFor must be a positive non-zero value`;
3838
}
3939
// Ensure the loop control variables are numeric
4040
if (
41-
start.type !== "number" || end.type !== "number" || step.type !== "number"
41+
start.type !== "number" ||
42+
end.type !== "number" ||
43+
step.type !== "number"
4244
) {
43-
throw new Error("Invalid loop control variables");
45+
throw `RunTimeError: Invalid loop control variables`;
4446
}
4547
if (start.value <= end.value) {
4648
// Iterate over the range using the start, end, and step values

BackEnd/Interpreter/Statements/Loop/While_statement.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ export const evaluate_while_statement = (
188188
env,
189189
);
190190
default:
191-
throw new Error(
192-
"Condition in while statement must be a boolean or numeric value",
193-
);
191+
throw `RunTimeError: Condition in while statement must be a boolean or numeric value`;
194192
}
195193
};

BackEnd/Interpreter/interpreter.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export const evaluate_call_expression = (
140140

141141
if (expr.args.length !== funcValue.params.length) {
142142
let caller = expr.caller as Identifier;
143-
throw `Function "${caller.symbol}" expects ${funcValue.params.length} arguments, but ${expr.args.length} were provided.`;
143+
throw `RunTimeError: Function "${caller.symbol}" expects ${funcValue.params.length} arguments, but ${expr.args.length} were provided.`;
144144
}
145145

146146
// Create a new environment for the function call, inheriting the closure from the function definition
@@ -179,7 +179,7 @@ export const evaluate_call_expression = (
179179
}
180180
return returnValue;
181181
} else {
182-
throw `Cannot call non-function ${JSON.stringify(fn)}`;
182+
throw `RunTimeError: Cannot call non-function ${JSON.stringify(fn)}`;
183183
}
184184
};
185185

@@ -213,7 +213,7 @@ export function evaluate(astNode: Stmt, env: Environment): RuntimeVal {
213213
switch (astNode.kind) {
214214
case "NumericLiteral":
215215
return {
216-
value: ((astNode as NumericLiteral).value),
216+
value: (astNode as NumericLiteral).value,
217217
type: "number",
218218
} as NumberVal;
219219

@@ -300,6 +300,6 @@ export function evaluate(astNode: Stmt, env: Environment): RuntimeVal {
300300
default:
301301
// If the AST node has an unknown or unsupported kind, we log an error and exit the program.
302302
console.log(astNode);
303-
throw `This node has not yet been setup in interpretation...`;
303+
throw `RunTimeError: This node has not yet been setup in interpretation...`;
304304
}
305305
}

BackEnd/Scope/environment.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ export default class Environment {
2020
this.MAX_ALLOWED_ITERATIONS = 102702;
2121
}
2222

23-
public checkInfiniteLoop(
24-
iterationCnt: number,
25-
): Boolean {
23+
public checkInfiniteLoop(iterationCnt: number): Boolean {
2624
if (iterationCnt > this.MAX_ALLOWED_ITERATIONS) {
27-
throw `TIME LIMIT EXCEEDED...`;
25+
throw `RunTimeError: TIME LIMIT EXCEEDED...`;
2826
}
2927
return false;
3028
}
@@ -42,7 +40,7 @@ export default class Environment {
4240
isConst: boolean,
4341
): RuntimeVal {
4442
if (this.variables.has(varname)) {
45-
throw `cannot declare variable ${varname}. As it is already defined in the scope.`;
43+
throw `RunTimeError: Cannot declare variable ${varname}. As it is already defined in the scope.`;
4644
}
4745
this.variables.set(varname, value);
4846
if (isConst) {
@@ -61,7 +59,7 @@ export default class Environment {
6159
// Find the scope in which variable exists
6260
const env: Environment = this.resolveScope(varname);
6361
if (env.constants.has(varname)) {
64-
throw `cannot reassign to const variable ${varname}`;
62+
throw `RunTimeError: Cannot reassign to const variable ${varname}`;
6563
}
6664

6765
env.variables.set(varname, value);
@@ -91,7 +89,7 @@ export default class Environment {
9189
return this;
9290
}
9391
if (this.parent === undefined) {
94-
throw `cannot resolve ${varname} in the scope.`;
92+
throw `RunTimeError: Cannot resolve ${varname} in the scope.`;
9593
}
9694
return this.parent.resolveScope(varname);
9795
}

BackEnd/Scope/globalScope.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ArrayVal,
23
BooleanVal,
34
MAKE_BOOL,
45
MAKE_FUNCTION,
@@ -39,7 +40,7 @@ export function setupGlobalScope() {
3940
MAKE_NATIVE_FN((args, _scope): RuntimeVal => {
4041
if (args.length !== 2) {
4142
const error_msg: any =
42-
`No matching function for call to 'assertEqual'. Note: candidate function not viable. Function assertEqual requires 2 arguments, but ${args.length} was provided.`;
43+
`RunTimeError: No matching function for call to 'assertEqual'. Note: candidate function not viable. Function assertEqual requires 2 arguments, but ${args.length} was provided.`;
4344
throw error_msg;
4445
}
4546
const actual_type = args[0].type;
@@ -66,7 +67,7 @@ export function setupGlobalScope() {
6667
}
6768

6869
default:
69-
throw `Error: Null value exception`;
70+
throw `RunTimeError: Null value exception`;
7071
}
7172
}
7273
}),
@@ -77,11 +78,12 @@ export function setupGlobalScope() {
7778
MAKE_NATIVE_FN((args, _scope): RuntimeVal => {
7879
for (let i = 0; i < args.length; i++) {
7980
const type = args[i].type;
81+
8082
switch (type) {
8183
case "number": {
8284
const num_to_print = (args[i] as NumberVal).value;
8385
console.log(num_to_print);
84-
return MAKE_NUll();
86+
break;
8587
}
8688
case "boolean": {
8789
const bool_to_print = (args[i] as BooleanVal).value;
@@ -92,19 +94,19 @@ export function setupGlobalScope() {
9294
ans = "false";
9395
}
9496
console.log(ans);
95-
return MAKE_NUll();
97+
break;
9698
}
9799
case "string": {
98100
const string_to_print = (args[i] as StringVal).value;
99101
console.log(string_to_print);
100-
return MAKE_NUll();
102+
break;
101103
}
102104
case "array": {
103-
throw `Invalid Array Print Operation. Use Iterative Method Instead.`;
105+
throw `RunTimeError: Invalid Array Print Operation. Use Iterative Method Instead.`;
104106
}
105107
case "null": {
106108
console.log(null);
107-
return MAKE_NUll();
109+
break;
108110
}
109111
}
110112
}

0 commit comments

Comments
 (0)