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 #25 from shashankbhosagi/main
Browse files Browse the repository at this point in the history
⚡Impoved Error messages , Dynamic Array & Top Secret
  • Loading branch information
SahilK-027 authored Aug 4, 2023
2 parents b9c33dd + 9047098 commit 056ca68
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 212 deletions.
4 changes: 3 additions & 1 deletion BackEnd/Interpreter/Expressions/Expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const evaluate_assignment_expression = (
rhs_value = (rhs as BooleanVal).value;
}

const assigner = (node.assignee) as MemberExpr;
const assigner = node.assignee as MemberExpr;
const assigner_name = (assigner.object as Identifier).symbol;
const index = evaluate(assigner.property, env) as NumberVal;
const index_val = index.value;
Expand Down Expand Up @@ -93,8 +93,10 @@ export const evaluate_member_expression = (
env: Environment,
): RuntimeVal => {
const object = evaluate(member.object, env);
// console.log(object);

if (object.type === "array") {
// console.log(object.size);
const index = evaluate(member.property, env) as NumberVal;
if (index.type !== "number") {
throw new Error("Array index must be a number");
Expand Down
31 changes: 29 additions & 2 deletions BackEnd/Interpreter/Statements/Declaration_statements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import {
VariableDeclaration,
} from "../../../FrontEnd/AST.ts";
import Environment from "../../Scope/environment.ts";
import { ArrayVal, MAKE_NUll, RuntimeVal } from "../../values.ts";
import {
ArrayVal,
MAKE_NUll,
MAKE_NUM,
NumberVal,
RuntimeVal,
} from "../../values.ts";
import { evaluate } from "../interpreter.ts";

/**
Expand Down Expand Up @@ -34,11 +40,32 @@ export const evaluate_array_declaration = (
declaration: ArrayDeclaration,
env: Environment,
): RuntimeVal => {
const size_expr = evaluate(declaration.size, env);
const values_provided = declaration.values.length;

if (size_expr.type !== "number") {
throw `error from array Shas`;
}
const arr_size = (evaluate(declaration.size, env) as NumberVal).value;
if (arr_size > 10000000) {
throw `Memory limit exceeded`;
} else if (arr_size <= 0) {
throw `Invalid Array size`;
}
if (values_provided > arr_size) {
throw `Size of array exceeded`;
}
let count: number = values_provided;
while (count < arr_size) {
declaration.values.push({ kind: "NumericLiteral", value: 0 });
count++;
}

const arr = {
type: "array",
name: declaration.name,
values: declaration.values,
size: declaration.size,
size: arr_size,
} as ArrayVal;

return env.declareVar(declaration.name, arr, false);
Expand Down
2 changes: 1 addition & 1 deletion FrontEnd/AST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export interface BreakStatement extends Stmt {
export interface ArrayDeclaration extends Stmt {
kind: "ArrayDeclaration";
name: string;
size: number;
size: Expr;
values: Expr[];
}

Expand Down
Loading

0 comments on commit 056ca68

Please sign in to comment.