Skip to content

Commit

Permalink
Don't compare lists (#7475)
Browse files Browse the repository at this point in the history
  • Loading branch information
iHiD authored Feb 9, 2025
1 parent 8e310ea commit e6eeec2
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
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

0 comments on commit e6eeec2

Please sign in to comment.