Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't compare lists #7475

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/javascript/interpreter/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export type RuntimeErrorType =
| 'IndexOutOfBoundsInGet'
| 'IndexOutOfBoundsInChange'
| 'MaxTotalExecutionTimeReached'
| 'ListsCannotBeCompared'
| 'IncomparableTypes'

export type StaticErrorType =
| DisabledLanguageFeatureErrorType
Expand Down
20 changes: 20 additions & 0 deletions app/javascript/interpreter/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,10 @@ export class Executor {

public visitBinaryExpression(expression: BinaryExpression): EvaluationResult {
const leftResult = this.evaluate(expression.left)
this.verifyLiteral(leftResult.resultingValue, expression.left)

const rightResult = this.evaluate(expression.right)
this.verifyLiteral(rightResult.resultingValue, expression.right)

const result: EvaluationResult = {
type: 'BinaryExpression',
Expand Down Expand Up @@ -1119,6 +1122,23 @@ export class Executor {
}
}

private verifyLiteral(value: any, expr: Expression): void {
if (isNumber(value)) return
if (isString(value)) return
if (isBoolean(value)) return

this.guardUncalledFunction(value, expr)

if (isArray(value)) {
this.error('ListsCannotBeCompared', expr.location, {
value: formatLiteral(value),
})
}
this.error('IncomparableTypes', expr.location, {
value: formatLiteral(value),
})
}

private verifyNumber(value: any, expr: Expression): void {
if (isNumber(value)) return
this.guardUncalledFunction(value, expr)
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/interpreter/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"PointlessStatement": "This line of code doesn't do anything.",
"PotentialMissingParenthesesForFunctionCall": "Jiki didn't know what to do with this line of code. Did you mean to call a function and forget the parentheses? (`()`)",
"MissingEachAfterFor": "Did you forget to write `each` after the `for` keyword?",
"ForeachNotIterable": "Jiki couldn't work out how to iterate over `{{value}}`. He expected it to be a string or a list."
"ForeachNotIterable": "Jiki couldn't work out how to iterate over `{{value}}`. He expected it to be a string or a list.",
"ListsCannotBeCompared": "You can't compare lists directly to each other in JikiScript. Different languages handle this very differently, and Jiki's decided to opt out of the confusion. If you need to compare lists, we'll provide you with a specific function in the instructions."
},
"semantic": {
"CannotAssignToConstant": "Cannot re-assign value of constant.",
Expand Down
3 changes: 2 additions & 1 deletion app/javascript/interpreter/locales/system/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@
"IndexIsZero": "IndexIsZero",
"IndexOutOfBoundsInGet": "IndexOutOfBoundsInGet: index: {{index}}, length: {{length}}, dataType: {{dataType}}",
"IndexOutOfBoundsInChange": "IndexOutOfBoundsInChange: index: {{index}}, length: {{length}}, dataType: {{dataType}}",
"MaxTotalExecutionTimeReached": "MaxTotalExecutionTimeReached"
"MaxTotalExecutionTimeReached": "MaxTotalExecutionTimeReached",
"ListsCannotBeCompared": "ListsCannotBeCompared"
},
"disabledLanguageFeature": {
"ExcludeListViolation": "ExcludeListViolation: tokenType: {{tokenType}}",
Expand Down
7 changes: 7 additions & 0 deletions test/javascript/interpreter/runtimeErrors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,4 +504,11 @@ test('InvalidChangeElementTarget', () => {
expect(frames[1].error!.message).toBe('InvalidChangeElementTarget')
})

test('ListsCannotBeCompared', () => {
const code = `log [] == []`
const { frames } = interpret(code)
expectFrameToBeError(frames[0], `log [] == []`, 'ListsCannotBeCompared')
expect(frames[0].error!.message).toBe('ListsCannotBeCompared')
})

// TOOD: Strings are immutable
2 changes: 1 addition & 1 deletion test/javascript/validate_bootcamp_content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function testIo(project, exerciseSlug, config, task, testData, exampleScript) {
}

if (value != testData.expected) {
console.log(error, value, frames)
// console.log(error, value, frames)
}
expect(value).toEqual(testData.expected)
})
Expand Down
Loading