Skip to content
This repository was archived by the owner on Oct 13, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions BackEnd/Interpreter/Expressions/Binary_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ export const evaluate_numeric_binary_expression = (
operator: string,
): NumberVal => {
let result: number;
if (operator == "+") {
if (operator === "+") {
result = lhs.value + rhs.value;
} else if (operator == "-") {
} else if (operator === "-") {
result = lhs.value - rhs.value;
} else if (operator == "*") {
} else if (operator === "*") {
result = lhs.value * rhs.value;
} else if (operator == "/") {
} else if (operator === "/") {
// TODO: Division by zero check
result = lhs.value / rhs.value;
} else if (operator == "^") {
} else if (operator === "^") {
result = Math.pow(lhs.value, rhs.value);
} else {
result = lhs.value % rhs.value;
Expand All @@ -46,16 +46,16 @@ export const evaluate_numeric_string_binary_expression = (
operator: string,
): StringVal => {
let result: string;
if (operator == "+") {
if (operator === "+") {
result = lhs.value + rhs.value;
} else if (operator == "-") {
} else if (operator === "-") {
result = "NaN";
} else if (operator == "*") {
} else if (operator === "*") {
result = rhs.value;
while (--lhs.value) {
result += rhs.value;
}
} else if (operator == "/") {
} else if (operator === "/") {
result = "NaN";
} else {
result = "NaN";
Expand All @@ -76,16 +76,16 @@ export const evaluate_string_numeric_binary_expression = (
operator: string,
): StringVal => {
let result: string;
if (operator == "+") {
if (operator === "+") {
result = lhs.value + rhs.value;
} else if (operator == "-") {
} else if (operator === "-") {
result = "NaN";
} else if (operator == "*") {
} else if (operator === "*") {
result = lhs.value;
while (--rhs.value) {
result += lhs.value;
}
} else if (operator == "/") {
} else if (operator === "/") {
result = "NaN";
} else {
result = "NaN";
Expand All @@ -106,13 +106,13 @@ export const evaluate_string_binary_expression = (
operator: string,
): StringVal => {
let result: string;
if (operator == "+") {
if (operator === "+") {
result = lhs.value + rhs.value;
} else if (operator == "-") {
} else if (operator === "-") {
result = "NaN";
} else if (operator == "*") {
} else if (operator === "*") {
result = "NaN";
} else if (operator == "/") {
} else if (operator === "/") {
result = "NaN";
} else {
result = "NaN";
Expand All @@ -132,25 +132,25 @@ export const evaluate_binary_expression = (
const LHS = evaluate(binop.left, env);
const RHS = evaluate(binop.right, env);

if (LHS.type == "number" && RHS.type == "number") {
if (LHS.type === "number" && RHS.type === "number") {
return evaluate_numeric_binary_expression(
LHS as NumberVal,
RHS as NumberVal,
binop.operator,
);
} else if (LHS.type == "number" && RHS.type == "string") {
} else if (LHS.type === "number" && RHS.type === "string") {
return evaluate_numeric_string_binary_expression(
LHS as NumberVal,
RHS as StringVal,
binop.operator,
);
} else if (LHS.type == "string" && RHS.type == "number") {
} else if (LHS.type === "string" && RHS.type === "number") {
return evaluate_string_numeric_binary_expression(
LHS as StringVal,
RHS as NumberVal,
binop.operator,
);
} else if (LHS.type == "string" && RHS.type == "string") {
} else if (LHS.type === "string" && RHS.type === "string") {
return evaluate_string_binary_expression(
LHS as StringVal,
RHS as StringVal,
Expand Down
6 changes: 3 additions & 3 deletions BackEnd/Interpreter/Expressions/Boolean_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const evaluate_boolean_logical_expression = (
operator: string,
): BooleanVal => {
let result: boolean;
if (operator == "&&" || operator == "and") {
if (operator === "&&" || operator === "and") {
result = lhs.value && rhs.value;
} else {
result = lhs.value || rhs.value;
Expand All @@ -36,7 +36,7 @@ export const evaluate_logical_expression = (
const LHS = evaluate(logic.left, env);
const RHS = evaluate(logic.right, env);

if (LHS.type == "boolean" && RHS.type == "boolean") {
if (LHS.type === "boolean" && RHS.type === "boolean") {
return evaluate_boolean_logical_expression(
LHS as BooleanVal,
RHS as BooleanVal,
Expand Down Expand Up @@ -69,7 +69,7 @@ export const evaluate_unary_expr = (
env: Environment,
): RuntimeVal => {
const expression = evaluate(expr.argument, env);
if (expression.type == "boolean") {
if (expression.type === "boolean") {
return evaluate_boolean_unary_expression(
expression as BooleanVal,
);
Expand Down
24 changes: 12 additions & 12 deletions BackEnd/Interpreter/Expressions/Comparison_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export const evaluate_numeric_comparison_expression = (
operator: string,
): BooleanVal => {
let result: boolean;
if (operator == "==") {
if (operator === "==") {
result = lhs.value === rhs.value;
} else if (operator == "!=") {
} else if (operator === "!=") {
result = lhs.value !== rhs.value;
} else if (operator == "<") {
} else if (operator === "<") {
result = lhs.value < rhs.value;
} else if (operator == ">") {
} else if (operator === ">") {
result = lhs.value > rhs.value;
} else if (operator == "<=") {
} else if (operator === "<=") {
result = lhs.value <= rhs.value;
} else {
result = lhs.value >= rhs.value;
Expand All @@ -51,15 +51,15 @@ export const evaluate_string_comparison_expression = (
operator: string,
): BooleanVal => {
let result: boolean;
if (operator == "==") {
if (operator === "==") {
result = lhs.value === rhs.value;
} else if (operator == "!=") {
} else if (operator === "!=") {
result = lhs.value !== rhs.value;
} else if (operator == "<") {
} else if (operator === "<") {
result = lhs.value < rhs.value;
} else if (operator == ">") {
} else if (operator === ">") {
result = lhs.value > rhs.value;
} else if (operator == "<=") {
} else if (operator === "<=") {
result = lhs.value <= rhs.value;
} else {
result = lhs.value >= rhs.value;
Expand All @@ -79,15 +79,15 @@ export const evaluate_comparison_expression = (
const LHS = evaluate(comp.left, env);
const RHS = evaluate(comp.right, env);

if (LHS.type == "number" && RHS.type == "number") {
if (LHS.type === "number" && RHS.type === "number") {
return evaluate_numeric_comparison_expression(
LHS as NumberVal,
RHS as NumberVal,
comp.operator,
);
}

if (LHS.type == "string" && RHS.type == "string") {
if (LHS.type === "string" && RHS.type === "string") {
return evaluate_string_comparison_expression(
LHS as StringVal,
RHS as StringVal,
Expand Down
12 changes: 6 additions & 6 deletions BackEnd/Interpreter/Expressions/Expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const evaluate_call_expression = (
const args = expr.args.map((arg) => evaluate(arg, env));
const fn = evaluate(expr.caller, env);

if (fn.type == "native-fn") {
if (fn.type === "native-fn") {
const result = (fn as NativeFnVal).call(args, env);
return result;
}
Expand All @@ -47,17 +47,17 @@ export const evaluate_assignment_expression = (
node: AssignmentExpression,
env: Environment,
): RuntimeVal => {
if (node.assignee.kind == "Identifier") {
if (node.assignee.kind === "Identifier") {
const varname = (node.assignee as Identifier).symbol;
const value = evaluate(node.val, env);
return env.assignVar(varname, value);
}
if (node.assignee.kind == "MemberExpr") {
if (node.assignee.kind === "MemberExpr") {
const rhs = evaluate(node.val, env) as RuntimeVal;
let rhs_value;
if (rhs.type == "number") {
if (rhs.type === "number") {
rhs_value = (rhs as NumberVal).value;
} else if (rhs.type == "string") {
} else if (rhs.type === "string") {
rhs_value = (rhs as StringVal).value;
}
const assigner = (node.assignee) as MemberExpr;
Expand All @@ -71,7 +71,7 @@ export const evaluate_assignment_expression = (
const object = evaluate(assigner.object, env) as ArrayVal;
if (object.type === "array") {
const array = object.values;
if (rhs.type == "number") {
if (rhs.type === "number") {
array[index_val] = { kind: "NumericLiteral", value: rhs_value } as Expr;
} else {
array[index_val] = { kind: "StringLiteral", value: rhs_value } as Expr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const evaluate_switch_statement = (
if (discriminant.type === "number") {
for (const switchCase of switchStmt.cases) {
const test = evaluate(switchCase.test, env);
if (test.type == "number") {
if (test.type === "number") {
const value = (test as NumberVal).value;
const discriminant_val = (discriminant as NumberVal).value;
if (value === discriminant_val) {
Expand Down Expand Up @@ -59,7 +59,7 @@ export const evaluate_switch_statement = (
} else if (discriminant.type === "string") {
for (const switchCase of switchStmt.cases) {
const test = evaluate(switchCase.test, env);
if (test.type == "string") {
if (test.type === "string") {
const value = (test as StringVal).value;
const discriminant_val = (discriminant as StringVal).value;
if (value === discriminant_val) {
Expand Down
2 changes: 1 addition & 1 deletion BackEnd/Interpreter/Statements/Loop/For_loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const evaluate_for_loop_statement = (
const start = evaluate(stmt.start, env) as NumberVal;
const end = evaluate(stmt.end, env) as NumberVal;
let step: NumberVal;
if (stmt.step == undefined) {
if (stmt.step === undefined) {
step = { value: 1, type: "number" };
} else {
step = evaluate(stmt.step as NumericLiteral, env) as NumberVal;
Expand Down
2 changes: 1 addition & 1 deletion BackEnd/Scope/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class Environment {
if (this.variables.has(varname)) {
return this;
}
if (this.parent == undefined) {
if (this.parent === undefined) {
throw `cannot resolve ${varname} in the scope.`;
}
return this.parent.resolveScope(varname);
Expand Down
29 changes: 15 additions & 14 deletions FrontEnd/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class Parser {
* @returns A boolean indicating if there are more tokens.
*/
private not_eof(): boolean {
return this.tokens[0].type != TokenType.EOF;
return this.tokens[0].type !== TokenType.EOF;
}

/**
Expand Down Expand Up @@ -79,7 +79,7 @@ export default class Parser {
*/
private expect(type: TokenType, err: string) {
const prev = this.tokens.shift() as Token;
if (!prev || prev.type != type) {
if (!prev || prev.type !== type) {
console.error("Parser Error: \n", err, prev, "- Expecting: ", type);
Deno.exit(1);
}
Expand Down Expand Up @@ -497,13 +497,13 @@ export default class Parser {
* @throws {Error} If there are syntax errors or missing tokens in the statement.
*/
private parse_var_declaration(): Stmt {
const isConstant = this.eat().type == TokenType.NewEternal;
const isConstant = this.eat().type === TokenType.NewEternal;
const identifier = this.expect(
TokenType.Identifier,
"Expected identifier name while declaration",
).value;

if (this.at().type == TokenType.Semicolon) {
if (this.at().type === TokenType.Semicolon) {
// Consume semicolon
this.eat();
if (isConstant) {
Expand Down Expand Up @@ -557,7 +557,7 @@ export default class Parser {
*/
private parse_assignment_expr(): Expr {
const left = this.parse_logical_expr();
if (this.at().type == TokenType.Equals) {
if (this.at().type === TokenType.Equals) {
// Consume the equals token we just found
this.eat();

Expand Down Expand Up @@ -588,7 +588,7 @@ export default class Parser {
private parse_logical_expr(): Expr {
let left = this.parse_comparison_expr();

while (this.at().type == TokenType.LogicalOperator) {
while (this.at().type === TokenType.LogicalOperator) {
const operator = this.eat().value;
const right = this.parse_comparison_expr();

Expand All @@ -611,7 +611,7 @@ export default class Parser {
let left = this.parse_additive_expr();

while (
this.at().type == TokenType.ComparisonOperator
this.at().type === TokenType.ComparisonOperator
) {
const operator = this.eat().value; // Consume the comparison operator token
const right = this.parse_additive_expr(); // Parse the right-hand side of the comparison
Expand All @@ -638,7 +638,7 @@ export default class Parser {
let left = this.parse_multiplicative_expr();

// Pase operator
while (this.at().value == "+" || this.at().value == "-") {
while (this.at().value === "+" || this.at().value === "-") {
const operator = this.eat().value;
const right = this.parse_multiplicative_expr();

Expand All @@ -661,7 +661,8 @@ export default class Parser {

// Pase operator
while (
this.at().value == "*" || this.at().value == "/" || this.at().value == "%"
this.at().value === "*" || this.at().value === "/" ||
this.at().value === "%"
) {
const operator = this.eat().value;
const right = this.parse_exponential_expr();
Expand All @@ -685,7 +686,7 @@ export default class Parser {

// Pase operator
while (
this.at().value == "^"
this.at().value === "^"
) {
const operator = this.eat().value;
const right = this.parse_call_member_expr();
Expand All @@ -709,7 +710,7 @@ export default class Parser {
private parse_call_member_expr(): Expr {
const member = this.parse_member_expr();

if (this.at().type == TokenType.OpenParen) {
if (this.at().type === TokenType.OpenParen) {
return this.parse_call_expr(member);
}
return member;
Expand All @@ -729,7 +730,7 @@ export default class Parser {
args: this.parse_args(),
} as CallExpr;

if (this.at().type == TokenType.OpenParen) {
if (this.at().type === TokenType.OpenParen) {
call_expr = this.parse_call_expr(call_expr);
}

Expand All @@ -744,7 +745,7 @@ export default class Parser {
*/
private parse_args(): Expr[] {
this.expect(TokenType.OpenParen, `Expected open parenthesis`);
const args = this.at().type == TokenType.CloseParen
const args = this.at().type === TokenType.CloseParen
? []
: this.parse_arguments_list();

Expand All @@ -761,7 +762,7 @@ export default class Parser {
private parse_arguments_list(): Expr[] {
const args = [this.parse_assignment_expr()];

while (this.at().type == TokenType.Comma && this.eat()) {
while (this.at().type === TokenType.Comma && this.eat()) {
args.push(this.parse_assignment_expr());
}

Expand Down
Loading