face-hh/bussin

Objects are immutable?

Closed this issue · 4 comments

I don't know if this is a 'design choice' (like how loops doesn't have break), because this example from the readme doesn't work.

lit x be cap rn
lit obj be { key: nocap, x } rn

obj.key be cap
waffle(obj.key)

it will output

D:\Github\bussin\src\runtime\environment.ts:167
        if (value) pastVal.properties.set(prop, value);
                                      ^
TypeError: Cannot read properties of undefined (reading 'set')
    at Environment.lookupOrMutObject (D:\Github\bussin\src\runtime\environment.ts:167:39)
    at eval_member_expr (D:\Github\bussin\src\runtime\eval\expressions.ts:145:30)
    at eval_assignment (D:\Github\bussin\src\runtime\eval\expressions.ts:83:52)
    at evaluate (D:\Github\bussin\src\runtime\interpreter.ts:21:35)
    at eval_program (D:\Github\bussin\src\runtime\eval\statements.ts:11:33)
    at evaluate (D:\Github\bussin\src\runtime\interpreter.ts:25:32)
    at D:\Github\bussin\src\main.ts:31:28
    at step (D:\Github\bussin\src\main.ts:33:23)
    at Object.next (D:\Github\bussin\src\main.ts:14:53)
    at D:\Github\bussin\src\main.ts:8:71

no matter if it's bs or bsx. i tried current commit, previous commit, and the earliest commit. The issue seems persistent.

objects apparently don't work, the properties aren't assigned and in fact the property value itself is undefined.

Found the issue. The code was assigning the temp object to a value before assigning its value.

Function "lookupOrMutObject()" in environment.ts should be replaced with

` public lookupOrMutObject(expr: MemberExpr, value?: RuntimeVal, property?: Identifier): RuntimeVal {
if (expr.object.kind === 'MemberExpr') return this.lookupOrMutObject(expr.object as MemberExpr, value, expr.property as Identifier);

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

    let pastVal = env.variables.get(varname) as ObjectVal;

    const prop = property
        ? property.symbol
        : (expr.property as Identifier).symbol;
    const currentProp = (expr.property as Identifier).symbol;

    if (value) pastVal.properties.set(prop, value);
    
    if (currentProp) pastVal = (pastVal.properties.get(currentProp) as ObjectVal);

    return pastVal;
}`

To fix the issue.

Github code formatting sucks balls.

Fixed in v1.2.1