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

Commit ed941a3

Browse files
Merge pull request #17 from SahilK-027/main
Added Strict Equality (===) & Inequality (!==) Checks
2 parents a3d4f24 + 308ab22 commit ed941a3

10 files changed

Lines changed: 89 additions & 75 deletions

File tree

BackEnd/Interpreter/Expressions/Binary_expression.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ export const evaluate_numeric_binary_expression = (
1616
operator: string,
1717
): NumberVal => {
1818
let result: number;
19-
if (operator == "+") {
19+
if (operator === "+") {
2020
result = lhs.value + rhs.value;
21-
} else if (operator == "-") {
21+
} else if (operator === "-") {
2222
result = lhs.value - rhs.value;
23-
} else if (operator == "*") {
23+
} else if (operator === "*") {
2424
result = lhs.value * rhs.value;
25-
} else if (operator == "/") {
25+
} else if (operator === "/") {
2626
// TODO: Division by zero check
2727
result = lhs.value / rhs.value;
28-
} else if (operator == "^") {
28+
} else if (operator === "^") {
2929
result = Math.pow(lhs.value, rhs.value);
3030
} else {
3131
result = lhs.value % rhs.value;
@@ -46,16 +46,16 @@ export const evaluate_numeric_string_binary_expression = (
4646
operator: string,
4747
): StringVal => {
4848
let result: string;
49-
if (operator == "+") {
49+
if (operator === "+") {
5050
result = lhs.value + rhs.value;
51-
} else if (operator == "-") {
51+
} else if (operator === "-") {
5252
result = "NaN";
53-
} else if (operator == "*") {
53+
} else if (operator === "*") {
5454
result = rhs.value;
5555
while (--lhs.value) {
5656
result += rhs.value;
5757
}
58-
} else if (operator == "/") {
58+
} else if (operator === "/") {
5959
result = "NaN";
6060
} else {
6161
result = "NaN";
@@ -76,16 +76,16 @@ export const evaluate_string_numeric_binary_expression = (
7676
operator: string,
7777
): StringVal => {
7878
let result: string;
79-
if (operator == "+") {
79+
if (operator === "+") {
8080
result = lhs.value + rhs.value;
81-
} else if (operator == "-") {
81+
} else if (operator === "-") {
8282
result = "NaN";
83-
} else if (operator == "*") {
83+
} else if (operator === "*") {
8484
result = lhs.value;
8585
while (--rhs.value) {
8686
result += lhs.value;
8787
}
88-
} else if (operator == "/") {
88+
} else if (operator === "/") {
8989
result = "NaN";
9090
} else {
9191
result = "NaN";
@@ -106,13 +106,13 @@ export const evaluate_string_binary_expression = (
106106
operator: string,
107107
): StringVal => {
108108
let result: string;
109-
if (operator == "+") {
109+
if (operator === "+") {
110110
result = lhs.value + rhs.value;
111-
} else if (operator == "-") {
111+
} else if (operator === "-") {
112112
result = "NaN";
113-
} else if (operator == "*") {
113+
} else if (operator === "*") {
114114
result = "NaN";
115-
} else if (operator == "/") {
115+
} else if (operator === "/") {
116116
result = "NaN";
117117
} else {
118118
result = "NaN";
@@ -132,25 +132,25 @@ export const evaluate_binary_expression = (
132132
const LHS = evaluate(binop.left, env);
133133
const RHS = evaluate(binop.right, env);
134134

135-
if (LHS.type == "number" && RHS.type == "number") {
135+
if (LHS.type === "number" && RHS.type === "number") {
136136
return evaluate_numeric_binary_expression(
137137
LHS as NumberVal,
138138
RHS as NumberVal,
139139
binop.operator,
140140
);
141-
} else if (LHS.type == "number" && RHS.type == "string") {
141+
} else if (LHS.type === "number" && RHS.type === "string") {
142142
return evaluate_numeric_string_binary_expression(
143143
LHS as NumberVal,
144144
RHS as StringVal,
145145
binop.operator,
146146
);
147-
} else if (LHS.type == "string" && RHS.type == "number") {
147+
} else if (LHS.type === "string" && RHS.type === "number") {
148148
return evaluate_string_numeric_binary_expression(
149149
LHS as StringVal,
150150
RHS as NumberVal,
151151
binop.operator,
152152
);
153-
} else if (LHS.type == "string" && RHS.type == "string") {
153+
} else if (LHS.type === "string" && RHS.type === "string") {
154154
return evaluate_string_binary_expression(
155155
LHS as StringVal,
156156
RHS as StringVal,

BackEnd/Interpreter/Expressions/Boolean_expression.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const evaluate_boolean_logical_expression = (
1616
operator: string,
1717
): BooleanVal => {
1818
let result: boolean;
19-
if (operator == "&&" || operator == "and") {
19+
if (operator === "&&" || operator === "and") {
2020
result = lhs.value && rhs.value;
2121
} else {
2222
result = lhs.value || rhs.value;
@@ -36,7 +36,7 @@ export const evaluate_logical_expression = (
3636
const LHS = evaluate(logic.left, env);
3737
const RHS = evaluate(logic.right, env);
3838

39-
if (LHS.type == "boolean" && RHS.type == "boolean") {
39+
if (LHS.type === "boolean" && RHS.type === "boolean") {
4040
return evaluate_boolean_logical_expression(
4141
LHS as BooleanVal,
4242
RHS as BooleanVal,
@@ -69,7 +69,7 @@ export const evaluate_unary_expr = (
6969
env: Environment,
7070
): RuntimeVal => {
7171
const expression = evaluate(expr.argument, env);
72-
if (expression.type == "boolean") {
72+
if (expression.type === "boolean") {
7373
return evaluate_boolean_unary_expression(
7474
expression as BooleanVal,
7575
);

BackEnd/Interpreter/Expressions/Comparison_expression.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ export const evaluate_numeric_comparison_expression = (
2222
operator: string,
2323
): BooleanVal => {
2424
let result: boolean;
25-
if (operator == "==") {
25+
if (operator === "==") {
2626
result = lhs.value === rhs.value;
27-
} else if (operator == "!=") {
27+
} else if (operator === "!=") {
2828
result = lhs.value !== rhs.value;
29-
} else if (operator == "<") {
29+
} else if (operator === "<") {
3030
result = lhs.value < rhs.value;
31-
} else if (operator == ">") {
31+
} else if (operator === ">") {
3232
result = lhs.value > rhs.value;
33-
} else if (operator == "<=") {
33+
} else if (operator === "<=") {
3434
result = lhs.value <= rhs.value;
3535
} else {
3636
result = lhs.value >= rhs.value;
@@ -51,15 +51,15 @@ export const evaluate_string_comparison_expression = (
5151
operator: string,
5252
): BooleanVal => {
5353
let result: boolean;
54-
if (operator == "==") {
54+
if (operator === "==") {
5555
result = lhs.value === rhs.value;
56-
} else if (operator == "!=") {
56+
} else if (operator === "!=") {
5757
result = lhs.value !== rhs.value;
58-
} else if (operator == "<") {
58+
} else if (operator === "<") {
5959
result = lhs.value < rhs.value;
60-
} else if (operator == ">") {
60+
} else if (operator === ">") {
6161
result = lhs.value > rhs.value;
62-
} else if (operator == "<=") {
62+
} else if (operator === "<=") {
6363
result = lhs.value <= rhs.value;
6464
} else {
6565
result = lhs.value >= rhs.value;
@@ -79,15 +79,15 @@ export const evaluate_comparison_expression = (
7979
const LHS = evaluate(comp.left, env);
8080
const RHS = evaluate(comp.right, env);
8181

82-
if (LHS.type == "number" && RHS.type == "number") {
82+
if (LHS.type === "number" && RHS.type === "number") {
8383
return evaluate_numeric_comparison_expression(
8484
LHS as NumberVal,
8585
RHS as NumberVal,
8686
comp.operator,
8787
);
8888
}
8989

90-
if (LHS.type == "string" && RHS.type == "string") {
90+
if (LHS.type === "string" && RHS.type === "string") {
9191
return evaluate_string_comparison_expression(
9292
LHS as StringVal,
9393
RHS as StringVal,

BackEnd/Interpreter/Expressions/Expressions.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const evaluate_call_expression = (
3030
const args = expr.args.map((arg) => evaluate(arg, env));
3131
const fn = evaluate(expr.caller, env);
3232

33-
if (fn.type == "native-fn") {
33+
if (fn.type === "native-fn") {
3434
const result = (fn as NativeFnVal).call(args, env);
3535
return result;
3636
}
@@ -47,17 +47,17 @@ export const evaluate_assignment_expression = (
4747
node: AssignmentExpression,
4848
env: Environment,
4949
): RuntimeVal => {
50-
if (node.assignee.kind == "Identifier") {
50+
if (node.assignee.kind === "Identifier") {
5151
const varname = (node.assignee as Identifier).symbol;
5252
const value = evaluate(node.val, env);
5353
return env.assignVar(varname, value);
5454
}
55-
if (node.assignee.kind == "MemberExpr") {
55+
if (node.assignee.kind === "MemberExpr") {
5656
const rhs = evaluate(node.val, env) as RuntimeVal;
5757
let rhs_value;
58-
if (rhs.type == "number") {
58+
if (rhs.type === "number") {
5959
rhs_value = (rhs as NumberVal).value;
60-
} else if (rhs.type == "string") {
60+
} else if (rhs.type === "string") {
6161
rhs_value = (rhs as StringVal).value;
6262
}
6363
const assigner = (node.assignee) as MemberExpr;
@@ -71,7 +71,7 @@ export const evaluate_assignment_expression = (
7171
const object = evaluate(assigner.object, env) as ArrayVal;
7272
if (object.type === "array") {
7373
const array = object.values;
74-
if (rhs.type == "number") {
74+
if (rhs.type === "number") {
7575
array[index_val] = { kind: "NumericLiteral", value: rhs_value } as Expr;
7676
} else {
7777
array[index_val] = { kind: "StringLiteral", value: rhs_value } as Expr;

BackEnd/Interpreter/Statements/Conditional_Jump/Switch_case.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const evaluate_switch_statement = (
2424
if (discriminant.type === "number") {
2525
for (const switchCase of switchStmt.cases) {
2626
const test = evaluate(switchCase.test, env);
27-
if (test.type == "number") {
27+
if (test.type === "number") {
2828
const value = (test as NumberVal).value;
2929
const discriminant_val = (discriminant as NumberVal).value;
3030
if (value === discriminant_val) {
@@ -59,7 +59,7 @@ export const evaluate_switch_statement = (
5959
} else if (discriminant.type === "string") {
6060
for (const switchCase of switchStmt.cases) {
6161
const test = evaluate(switchCase.test, env);
62-
if (test.type == "string") {
62+
if (test.type === "string") {
6363
const value = (test as StringVal).value;
6464
const discriminant_val = (discriminant as StringVal).value;
6565
if (value === discriminant_val) {

BackEnd/Interpreter/Statements/Loop/For_loop.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const evaluate_for_loop_statement = (
1717
const start = evaluate(stmt.start, env) as NumberVal;
1818
const end = evaluate(stmt.end, env) as NumberVal;
1919
let step: NumberVal;
20-
if (stmt.step == undefined) {
20+
if (stmt.step === undefined) {
2121
step = { value: 1, type: "number" };
2222
} else {
2323
step = evaluate(stmt.step as NumericLiteral, env) as NumberVal;

BackEnd/Scope/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default class Environment {
8080
if (this.variables.has(varname)) {
8181
return this;
8282
}
83-
if (this.parent == undefined) {
83+
if (this.parent === undefined) {
8484
throw `cannot resolve ${varname} in the scope.`;
8585
}
8686
return this.parent.resolveScope(varname);

FrontEnd/Parser.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default class Parser {
5050
* @returns A boolean indicating if there are more tokens.
5151
*/
5252
private not_eof(): boolean {
53-
return this.tokens[0].type != TokenType.EOF;
53+
return this.tokens[0].type !== TokenType.EOF;
5454
}
5555

5656
/**
@@ -79,7 +79,7 @@ export default class Parser {
7979
*/
8080
private expect(type: TokenType, err: string) {
8181
const prev = this.tokens.shift() as Token;
82-
if (!prev || prev.type != type) {
82+
if (!prev || prev.type !== type) {
8383
console.error("Parser Error: \n", err, prev, "- Expecting: ", type);
8484
Deno.exit(1);
8585
}
@@ -497,13 +497,13 @@ export default class Parser {
497497
* @throws {Error} If there are syntax errors or missing tokens in the statement.
498498
*/
499499
private parse_var_declaration(): Stmt {
500-
const isConstant = this.eat().type == TokenType.NewEternal;
500+
const isConstant = this.eat().type === TokenType.NewEternal;
501501
const identifier = this.expect(
502502
TokenType.Identifier,
503503
"Expected identifier name while declaration",
504504
).value;
505505

506-
if (this.at().type == TokenType.Semicolon) {
506+
if (this.at().type === TokenType.Semicolon) {
507507
// Consume semicolon
508508
this.eat();
509509
if (isConstant) {
@@ -557,7 +557,7 @@ export default class Parser {
557557
*/
558558
private parse_assignment_expr(): Expr {
559559
const left = this.parse_logical_expr();
560-
if (this.at().type == TokenType.Equals) {
560+
if (this.at().type === TokenType.Equals) {
561561
// Consume the equals token we just found
562562
this.eat();
563563

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

591-
while (this.at().type == TokenType.LogicalOperator) {
591+
while (this.at().type === TokenType.LogicalOperator) {
592592
const operator = this.eat().value;
593593
const right = this.parse_comparison_expr();
594594

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

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

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

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

662662
// Pase operator
663663
while (
664-
this.at().value == "*" || this.at().value == "/" || this.at().value == "%"
664+
this.at().value === "*" || this.at().value === "/" ||
665+
this.at().value === "%"
665666
) {
666667
const operator = this.eat().value;
667668
const right = this.parse_exponential_expr();
@@ -685,7 +686,7 @@ export default class Parser {
685686

686687
// Pase operator
687688
while (
688-
this.at().value == "^"
689+
this.at().value === "^"
689690
) {
690691
const operator = this.eat().value;
691692
const right = this.parse_call_member_expr();
@@ -709,7 +710,7 @@ export default class Parser {
709710
private parse_call_member_expr(): Expr {
710711
const member = this.parse_member_expr();
711712

712-
if (this.at().type == TokenType.OpenParen) {
713+
if (this.at().type === TokenType.OpenParen) {
713714
return this.parse_call_expr(member);
714715
}
715716
return member;
@@ -729,7 +730,7 @@ export default class Parser {
729730
args: this.parse_args(),
730731
} as CallExpr;
731732

732-
if (this.at().type == TokenType.OpenParen) {
733+
if (this.at().type === TokenType.OpenParen) {
733734
call_expr = this.parse_call_expr(call_expr);
734735
}
735736

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

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

764-
while (this.at().type == TokenType.Comma && this.eat()) {
765+
while (this.at().type === TokenType.Comma && this.eat()) {
765766
args.push(this.parse_assignment_expr());
766767
}
767768

0 commit comments

Comments
 (0)