Skip to content

Commit

Permalink
Better formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
cvs0 committed May 23, 2024
1 parent 453a88d commit a22498d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 56 deletions.
8 changes: 4 additions & 4 deletions frontend/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ export interface CallExpr extends Expr {
}

export interface MemberExpr extends Expr {
kind: "MemberExpr";
object: Expr;
property: Expr;
computed: boolean;
kind: "MemberExpr";
object: Expr;
property: Expr;
computed: boolean;
}

export interface Identifier extends Expr {
Expand Down
74 changes: 38 additions & 36 deletions frontend/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,42 +541,44 @@ export default class Parser {

// Parse a member expression, including properties accessed via dot or square bracket notation
private parse_member_expr(): Expr {
// Parse the initial object as a primary expression
let object = this.parse_primary_expr();

// Continue parsing member expressions while encountering dot or open bracket
while (this.at().type === TokenType.Dot || this.at().type === TokenType.OpenBracket) {
const operator = this.eat(); // Consume the dot or open bracket

let property: Expr;
let computed: boolean;

if (operator.type === TokenType.Dot) { // Non-computed values (e.g., dot.expr)
computed = false;
// Parse and get the identifier as the property
property = this.parse_primary_expr();

// Ensure that the property is an identifier
if (property.kind !== "Identifier") {
throw 'Cannot use dot operator without the right-hand side being an identifier.';
}
} else { // Allows object[computedValue] (square bracket notation)
computed = true;
property = this.parse_expr(); // Parse the computed property expression
this.expect(TokenType.CloseBracket, "Error: Incomplete Computed Value - Missing Closing Bracket ']'.");
}

// Create a Member Expression node representing the property access
object = {
kind: "MemberExpr",
object,
property,
computed,
} as MemberExpr;
}

return object; // Return the resulting member expression
}
let object = this.parse_primary_expr();

while (
this.at().type == TokenType.Dot ||
this.at().type == TokenType.OpenBracket
) {
const operator = this.eat();
let property: Expr;
let computed: boolean;

// non-computed values aka obj.expr
if (operator.type == TokenType.Dot) {
computed = false;
// get identifier
property = this.parse_primary_expr();
if (property.kind != "Identifier") {
throw `Cannonot use dot operator without right hand side being a identifier`;
}
} else {
// this allows obj[computedValue]
computed = true;
property = this.parse_expr();
this.expect(
TokenType.CloseBracket,
"Missing closing bracket in computed value."
);
}

object = {
kind: "MemberExpr",
object,
property,
computed,
} as MemberExpr;
}

return object;
}

// Orders of prescidence
//
Expand Down
21 changes: 9 additions & 12 deletions runtime/eval/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,18 @@ export function eval_assignment(
}

export function eval_object_expr(
obj: ObjectLiteral,
env: Environment
obj: ObjectLiteral,
env: Environment
): RuntimeVal {
const object = { type: "object", properties: new Map()} as ObjectVal;
const object = { type: "object", properties: new Map() } as ObjectVal;
for (const { key, value } of obj.properties) {
const runtimeVal =
value == undefined ? env.lookupVar(key) : evaluate(value, env);

for (const { key, value } of obj.properties) {

const runtimeVal = (value == undefined)
? env.lookupVar(key)
: evaluate(value, env);

object.properties.set(key, runtimeVal);
}
object.properties.set(key, runtimeVal);
}

return object;
return object;
}

export function eval_call_expr(expr: CallExpr, env: Environment): RuntimeVal {
Expand Down
16 changes: 12 additions & 4 deletions test.cvs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
fn log (str) {
print(str)
}
let foo = 50 / 2;

log(5)
const obj = {
x: 100,
y: 32,
foo: foo,
complex: {
bar: true,
},
};

let f = object.complex.bar;
foo = obj.foo + 5

0 comments on commit a22498d

Please sign in to comment.