diff --git a/BackEnd/Interpreter/Expressions/Unary_expression.ts b/BackEnd/Interpreter/Expressions/Unary_expression.ts index d67b4e3..825195f 100644 --- a/BackEnd/Interpreter/Expressions/Unary_expression.ts +++ b/BackEnd/Interpreter/Expressions/Unary_expression.ts @@ -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; }; @@ -30,5 +28,5 @@ export const evaluate_minus_expression = ( if (expression.type === "number") { return evaluate_minus(expression as NumberVal); } - throw `The RHS to operator should be of type number`; + throw `RunTimeError: The RHS to operator should be of type number`; }; diff --git a/BackEnd/Interpreter/Statements/Conditional_Jump/If_Else.ts b/BackEnd/Interpreter/Statements/Conditional_Jump/If_Else.ts index aa0bb95..8425fcb 100644 --- a/BackEnd/Interpreter/Statements/Conditional_Jump/If_Else.ts +++ b/BackEnd/Interpreter/Statements/Conditional_Jump/If_Else.ts @@ -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`; } }; diff --git a/BackEnd/Interpreter/Statements/Conditional_Jump/Switch_case.ts b/BackEnd/Interpreter/Statements/Conditional_Jump/Switch_case.ts index dde004e..0e8bf3b 100644 --- a/BackEnd/Interpreter/Statements/Conditional_Jump/Switch_case.ts +++ b/BackEnd/Interpreter/Statements/Conditional_Jump/Switch_case.ts @@ -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) { @@ -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) { diff --git a/BackEnd/Interpreter/Statements/Declaration_statements.ts b/BackEnd/Interpreter/Statements/Declaration_statements.ts index d224f0d..405526e 100644 --- a/BackEnd/Interpreter/Statements/Declaration_statements.ts +++ b/BackEnd/Interpreter/Statements/Declaration_statements.ts @@ -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) { diff --git a/BackEnd/Interpreter/Statements/Loop/For_loop.ts b/BackEnd/Interpreter/Statements/Loop/For_loop.ts index 66f9954..7d908a9 100644 --- a/BackEnd/Interpreter/Statements/Loop/For_loop.ts +++ b/BackEnd/Interpreter/Statements/Loop/For_loop.ts @@ -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 diff --git a/BackEnd/Interpreter/Statements/Loop/While_statement.ts b/BackEnd/Interpreter/Statements/Loop/While_statement.ts index 58747a7..dec3a56 100644 --- a/BackEnd/Interpreter/Statements/Loop/While_statement.ts +++ b/BackEnd/Interpreter/Statements/Loop/While_statement.ts @@ -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`; } }; diff --git a/BackEnd/Interpreter/interpreter.ts b/BackEnd/Interpreter/interpreter.ts index 3a8651b..d105292 100644 --- a/BackEnd/Interpreter/interpreter.ts +++ b/BackEnd/Interpreter/interpreter.ts @@ -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 @@ -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)}`; } }; @@ -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; @@ -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...`; } } diff --git a/BackEnd/Scope/environment.ts b/BackEnd/Scope/environment.ts index 4194be4..24f1871 100644 --- a/BackEnd/Scope/environment.ts +++ b/BackEnd/Scope/environment.ts @@ -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; } @@ -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) { @@ -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); @@ -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); } diff --git a/BackEnd/Scope/globalScope.ts b/BackEnd/Scope/globalScope.ts index a41dea1..edff69e 100644 --- a/BackEnd/Scope/globalScope.ts +++ b/BackEnd/Scope/globalScope.ts @@ -1,4 +1,5 @@ import { + ArrayVal, BooleanVal, MAKE_BOOL, MAKE_FUNCTION, @@ -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; @@ -66,7 +67,7 @@ export function setupGlobalScope() { } default: - throw `Error: Null value exception`; + throw `RunTimeError: Null value exception`; } } }), @@ -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; @@ -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; } } }