Skip to content

Commit

Permalink
correccion de error en repl cuando se redefinia una variable (#319)
Browse files Browse the repository at this point in the history
* correccion de error en repl

* agregue una verificacion mas

* agregue una verificacion mas

* Adding more tests and expectations

---------

Co-authored-by: Thom <[email protected]>
Co-authored-by: Fernando Dodino <[email protected]>
  • Loading branch information
3 people authored Dec 13, 2024
1 parent a733ac9 commit 75e485e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/interpreter/runtimeModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,13 +488,19 @@ export class Evaluation {
}

protected *execVariable(node: Variable): Execution<void> {
const variableFullName = targetName(node, node.name)

if(this.currentFrame.locals.get(variableFullName)){
throw new Error('Can\'t redefine a variable')
}

const value = yield* this.exec(node.value)

assertNotVoid(value, `Cannot assign to variable '${node.name}': ${getExpressionFor(node.value)} produces no value, cannot assign it to a variable`)

yield node

this.currentFrame.set(targetName(node, node.name), value)
this.currentFrame.set(variableFullName, value)
}

protected *execAssignment(node: Assignment): Execution<void> {
Expand Down
76 changes: 76 additions & 0 deletions test/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,82 @@ describe('Wollok Interpreter', () => {
)
})

it('Can\'t redefine a const with a var', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
const variableName = 1
`,
}])
interpreter = new Interpreter(Evaluation.build(replEnvironment, WRENatives))
const { error } = interprete(interpreter, 'var variableName = 2')
assertBasicError(error)
expect(getStackTraceSanitized(error)).to.deep.equal(
[
'wollok.lang.EvaluationError: Error: Can\'t redefine a variable',

]
)
const { result } = interprete(interpreter, 'variableName')
expect(+result).to.equal(1)
})

it('Can\'t redefine a const with a const', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
const variableName = 1
`,
}])
interpreter = new Interpreter(Evaluation.build(replEnvironment, WRENatives))
const { error } = interprete(interpreter, 'const variableName = 2')
assertBasicError(error)
expect(getStackTraceSanitized(error)).to.deep.equal(
[
'wollok.lang.EvaluationError: Error: Can\'t redefine a variable',

]
)
const { result } = interprete(interpreter, 'variableName')
expect(+result).to.equal(1)
})

it('Can\'t redefine a var with a const', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
var variableName = 1
`,
}])
interpreter = new Interpreter(Evaluation.build(replEnvironment, WRENatives))
const { error } = interprete(interpreter, 'const variableName = 2')
assertBasicError(error)
expect(getStackTraceSanitized(error)).to.deep.equal(
[
'wollok.lang.EvaluationError: Error: Can\'t redefine a variable',

]
)
const { result } = interprete(interpreter, 'variableName')
expect(+result).to.equal(1)
})

it('Can\'t redefine a var with a var', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
var variableName = 1
`,
}])
interpreter = new Interpreter(Evaluation.build(replEnvironment, WRENatives))
const { error } = interprete(interpreter, 'var variableName = 2')
assertBasicError(error)
expect(getStackTraceSanitized(error)).to.deep.equal(
[
'wollok.lang.EvaluationError: Error: Can\'t redefine a variable',

]
)
const { result } = interprete(interpreter, 'variableName')
expect(+result).to.equal(1)
})

it('should wrap void validation errors for assignment to void value', () => {
const replEnvironment = buildEnvironment([{
name: REPL, content: `
Expand Down

0 comments on commit 75e485e

Please sign in to comment.