Skip to content

Commit

Permalink
fixed member expressions not working in assignment expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
aName2050 committed Apr 8, 2024
1 parent cc413bd commit bee39d5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
54 changes: 47 additions & 7 deletions src/GigaScript/runtime/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,60 @@ export default class Environment {
return prop;
}

// FIXME:
// obj.complex.more = { test: "hello" }
// results in { test: "hello" } being added to the base objects properties map instead of the "complex" property's properties map
public modifyObject(expr: MemberExpr, newValue: GSAny): GSAny {
if (expr.object.kind == 'MemberExpr') {
let obj = expr.object;
let obj = this.getObject(expr.object as MemberExpr);

if (obj.type == 'object') {
(obj as GSObject).properties.set(
expr.property.symbol,
newValue
);
}

return obj;
}

const object = this.resolve(
(expr.object as Identifier).symbol
).variables.get((expr.object as Identifier).symbol) as GSObject;
const objectIdentifer = (expr.object as Identifier).symbol;
const env = this.resolve(objectIdentifer);

const object = env.variables.get(objectIdentifer) as GSObject;

object.properties.set(expr.property.symbol, newValue);

return object;
}

private getObject(expr: MemberExpr): GSAny {
if (expr.object.kind == 'MemberExpr') {
const value = this.lookupObjectValue(expr.object as MemberExpr);

if (value == undefined) {
throw `Property "${
expr.property.symbol
}" does't exist on object "${
(expr.object as Identifier).symbol
}"`;
}

if (value.type == 'object')
return (value as GSObject).properties.get(
expr.property.symbol
)!;
}

const varName = (expr.object as Identifier).symbol;
const env = this.resolve(varName);

let object = env.variables.get(varName) as GSObject;

const prop = object.properties.get(expr.property.symbol);

if (!prop)
throw `Property ${expr.property.symbol} does not exist on object "${
(expr.object as Identifier).symbol
}"`;

return prop;
}
}
2 changes: 2 additions & 0 deletions tests/main.g
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@ obj.string = "changed!?"

obj.complex.foo = 'foo'

obj.complex.more = { different: "this is different" }

print('modified', obj)

0 comments on commit bee39d5

Please sign in to comment.