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

Commit

Permalink
Merge pull request #26 from shashankbhosagi/main
Browse files Browse the repository at this point in the history
⚡ Improved Backend Error Message
  • Loading branch information
SahilK-027 authored Aug 4, 2023
2 parents 056ca68 + dd15e5e commit 974f60c
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 37 deletions.
6 changes: 2 additions & 4 deletions BackEnd/Interpreter/Expressions/Unary_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import { evaluate } from "../interpreter.ts";
* @param rhs The number value to negate.
* @returns The negated number value.
*/
export const evaluate_minus = (
rhs: NumberVal,
): NumberVal => {
export const evaluate_minus = (rhs: NumberVal): NumberVal => {
const res = -1 * rhs.value;
return { type: "number", value: res } as NumberVal;
};
Expand All @@ -30,5 +28,5 @@ export const evaluate_minus_expression = (
if (expression.type === "number") {
return evaluate_minus(expression as NumberVal);
}
throw `The RHS to <MINUS> operator should be of type number`;
throw `RunTimeError: The RHS to <MINUS> operator should be of type number`;
};
4 changes: 1 addition & 3 deletions BackEnd/Interpreter/Statements/Conditional_Jump/If_Else.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,7 @@ export const evaluate_if_statement = (
case "boolean":
return evaluate_boolean_if_statement(condition as BooleanVal, stmt, env);
default:
throw new Error(
"Condition in if statement must be a boolean or integer value",
);
throw `RunTimeError: Condition in if statement must be a boolean or integer value`;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const evaluate_switch_statement = (
}
// Evaluate the default case if no matching case is found
} else {
throw "Expected Number in switch case as in switch(expression) expression is of type number";
throw `RunTimeError: Expected Number in multiverse madness, as in multiverse(expression) the expression is of type number`;
}
}
for (const consequent of switchStmt.default) {
Expand Down Expand Up @@ -126,7 +126,7 @@ export const evaluate_switch_statement = (
}
// Evaluate the default case if no matching case is found
} else {
throw "Expected string in switch case as in switch(expression) expression is of type string";
throw `Expected String in multiverse madness, as in multiverse(expression) the expression is of type String`;
}
}
for (const consequent of switchStmt.default) {
Expand Down
8 changes: 4 additions & 4 deletions BackEnd/Interpreter/Statements/Declaration_statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ export const evaluate_array_declaration = (
const values_provided = declaration.values.length;

if (size_expr.type !== "number") {
throw `error from array Shas`;
throw `RunTimeError: The operand for subscript operator is expected to be of type number but recieved ${size_expr.type} at '${declaration.name}'`;
}
const arr_size = (evaluate(declaration.size, env) as NumberVal).value;
if (arr_size > 10000000) {
throw `Memory limit exceeded`;
throw `RunTimeError: Segmentation Fault expected array size to be less than 1e7 at '${declaration.name}'`;
} else if (arr_size <= 0) {
throw `Invalid Array size`;
throw `RunTimeError: Invalid Array size ${arr_size} at '${declaration.name}'`;
}
if (values_provided > arr_size) {
throw `Size of array exceeded`;
throw `RunTimeError: Excess elements in array initializer at '${declaration.name}'. Provided array size ${arr_size} while number of elements in ${values_provided}`;
}
let count: number = values_provided;
while (count < arr_size) {
Expand Down
8 changes: 5 additions & 3 deletions BackEnd/Interpreter/Statements/Loop/For_loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ export const evaluate_for_loop_statement = (
step = evaluate(stmt.step as NumericLiteral, env) as NumberVal;
}
if (step.value <= 0) {
throw `TLE(Exception): The step value in wakandaFor must be a positive non-zero value.`;
throw `RunTimeError: The step value in wakandaFor must be a positive non-zero value`;
}
// Ensure the loop control variables are numeric
if (
start.type !== "number" || end.type !== "number" || step.type !== "number"
start.type !== "number" ||
end.type !== "number" ||
step.type !== "number"
) {
throw new Error("Invalid loop control variables");
throw `RunTimeError: Invalid loop control variables`;
}
if (start.value <= end.value) {
// Iterate over the range using the start, end, and step values
Expand Down
4 changes: 1 addition & 3 deletions BackEnd/Interpreter/Statements/Loop/While_statement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,6 @@ export const evaluate_while_statement = (
env,
);
default:
throw new Error(
"Condition in while statement must be a boolean or numeric value",
);
throw `RunTimeError: Condition in while statement must be a boolean or numeric value`;
}
};
8 changes: 4 additions & 4 deletions BackEnd/Interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export const evaluate_call_expression = (

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

// Create a new environment for the function call, inheriting the closure from the function definition
Expand Down Expand Up @@ -179,7 +179,7 @@ export const evaluate_call_expression = (
}
return returnValue;
} else {
throw `Cannot call non-function ${JSON.stringify(fn)}`;
throw `RunTimeError: Cannot call non-function ${JSON.stringify(fn)}`;
}
};

Expand Down Expand Up @@ -213,7 +213,7 @@ export function evaluate(astNode: Stmt, env: Environment): RuntimeVal {
switch (astNode.kind) {
case "NumericLiteral":
return {
value: ((astNode as NumericLiteral).value),
value: (astNode as NumericLiteral).value,
type: "number",
} as NumberVal;

Expand Down Expand Up @@ -300,6 +300,6 @@ export function evaluate(astNode: Stmt, env: Environment): RuntimeVal {
default:
// If the AST node has an unknown or unsupported kind, we log an error and exit the program.
console.log(astNode);
throw `This node has not yet been setup in interpretation...`;
throw `RunTimeError: This node has not yet been setup in interpretation...`;
}
}
12 changes: 5 additions & 7 deletions BackEnd/Scope/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ export default class Environment {
this.MAX_ALLOWED_ITERATIONS = 102702;
}

public checkInfiniteLoop(
iterationCnt: number,
): Boolean {
public checkInfiniteLoop(iterationCnt: number): Boolean {
if (iterationCnt > this.MAX_ALLOWED_ITERATIONS) {
throw `TIME LIMIT EXCEEDED...`;
throw `RunTimeError: TIME LIMIT EXCEEDED...`;
}
return false;
}
Expand All @@ -42,7 +40,7 @@ export default class Environment {
isConst: boolean,
): RuntimeVal {
if (this.variables.has(varname)) {
throw `cannot declare variable ${varname}. As it is already defined in the scope.`;
throw `RunTimeError: Cannot declare variable ${varname}. As it is already defined in the scope.`;
}
this.variables.set(varname, value);
if (isConst) {
Expand All @@ -61,7 +59,7 @@ export default class Environment {
// Find the scope in which variable exists
const env: Environment = this.resolveScope(varname);
if (env.constants.has(varname)) {
throw `cannot reassign to const variable ${varname}`;
throw `RunTimeError: Cannot reassign to const variable ${varname}`;
}

env.variables.set(varname, value);
Expand Down Expand Up @@ -91,7 +89,7 @@ export default class Environment {
return this;
}
if (this.parent === undefined) {
throw `cannot resolve ${varname} in the scope.`;
throw `RunTimeError: Cannot resolve ${varname} in the scope.`;
}
return this.parent.resolveScope(varname);
}
Expand Down
16 changes: 9 additions & 7 deletions BackEnd/Scope/globalScope.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ArrayVal,
BooleanVal,
MAKE_BOOL,
MAKE_FUNCTION,
Expand Down Expand Up @@ -39,7 +40,7 @@ export function setupGlobalScope() {
MAKE_NATIVE_FN((args, _scope): RuntimeVal => {
if (args.length !== 2) {
const error_msg: any =
`No matching function for call to 'assertEqual'. Note: candidate function not viable. Function assertEqual requires 2 arguments, but ${args.length} was provided.`;
`RunTimeError: No matching function for call to 'assertEqual'. Note: candidate function not viable. Function assertEqual requires 2 arguments, but ${args.length} was provided.`;
throw error_msg;
}
const actual_type = args[0].type;
Expand All @@ -66,7 +67,7 @@ export function setupGlobalScope() {
}

default:
throw `Error: Null value exception`;
throw `RunTimeError: Null value exception`;
}
}
}),
Expand All @@ -77,11 +78,12 @@ export function setupGlobalScope() {
MAKE_NATIVE_FN((args, _scope): RuntimeVal => {
for (let i = 0; i < args.length; i++) {
const type = args[i].type;

switch (type) {
case "number": {
const num_to_print = (args[i] as NumberVal).value;
console.log(num_to_print);
return MAKE_NUll();
break;
}
case "boolean": {
const bool_to_print = (args[i] as BooleanVal).value;
Expand All @@ -92,19 +94,19 @@ export function setupGlobalScope() {
ans = "false";
}
console.log(ans);
return MAKE_NUll();
break;
}
case "string": {
const string_to_print = (args[i] as StringVal).value;
console.log(string_to_print);
return MAKE_NUll();
break;
}
case "array": {
throw `Invalid Array Print Operation. Use Iterative Method Instead.`;
throw `RunTimeError: Invalid Array Print Operation. Use Iterative Method Instead.`;
}
case "null": {
console.log(null);
return MAKE_NUll();
break;
}
}
}
Expand Down

0 comments on commit 974f60c

Please sign in to comment.