Skip to content

Commit

Permalink
Disable or in rock-paper-scissors (#7353)
Browse files Browse the repository at this point in the history
* Disable or in rock-paper-scissors

* Remove stray console.log
  • Loading branch information
iHiD authored Jan 21, 2025
1 parent 72885fc commit ed1b1e8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app/javascript/interpreter/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export type FrameContext = {
export type Toggle = 'ON' | 'OFF'

export type LanguageFeatures = {
IncludeList?: TokenType[]
ExcludeList?: TokenType[]
includeList?: TokenType[]
excludeList?: TokenType[]
shadowing?: Toggle
truthiness?: Toggle
repeatDelay?: number
Expand Down
18 changes: 10 additions & 8 deletions app/javascript/interpreter/languages/javascript/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export class Scanner {
}

private addToken(type: TokenType, literal: any = null): void {
this.verifyEnabled(type)
this.verifyEnabled(type, this.lexeme())

this.tokens.push({
type,
Expand Down Expand Up @@ -438,25 +438,27 @@ export class Scanner {
this.lineOffset = 0
}

private verifyEnabled(tokenType: TokenType): void {
private verifyEnabled(tokenType: TokenType, lexeme: string): void {
if (!this.languageFeatures) return

if (
this.languageFeatures.ExcludeList &&
this.languageFeatures.ExcludeList.includes(tokenType)
this.languageFeatures.excludeList &&
this.languageFeatures.excludeList.includes(tokenType)
)
this.disabledLanguageFeatureError('ExcludeListViolation', {
ExcludeList: this.languageFeatures.ExcludeList,
excludeList: this.languageFeatures.excludeList,
tokenType,
lexeme,
})

if (
this.languageFeatures.IncludeList &&
!this.languageFeatures.IncludeList.includes(tokenType)
this.languageFeatures.includeList &&
!this.languageFeatures.includeList.includes(tokenType)
)
this.disabledLanguageFeatureError('IncludeListViolation', {
IncludeList: this.languageFeatures.IncludeList,
includeList: this.languageFeatures.includeList,
tokenType,
lexeme,
})
}

Expand Down
18 changes: 10 additions & 8 deletions app/javascript/interpreter/languages/jikiscript/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export class Scanner {
}

private addToken(type: TokenType, literal: any = null): void {
this.verifyEnabled(type)
this.verifyEnabled(type, this.lexeme())

this.tokens.push({
type,
Expand Down Expand Up @@ -462,25 +462,27 @@ export class Scanner {
this.lineOffset = 0
}

private verifyEnabled(tokenType: TokenType): void {
private verifyEnabled(tokenType: TokenType, lexeme: string): void {
if (!this.languageFeatures) return

if (
this.languageFeatures.ExcludeList &&
this.languageFeatures.ExcludeList.includes(tokenType)
this.languageFeatures.excludeList &&
this.languageFeatures.excludeList.includes(tokenType)
)
this.disabledLanguageFeatureError('ExcludeListViolation', {
ExcludeList: this.languageFeatures.ExcludeList,
excludeList: this.languageFeatures.excludeList,
tokenType,
lexeme,
})

if (
this.languageFeatures.IncludeList &&
!this.languageFeatures.IncludeList.includes(tokenType)
this.languageFeatures.includeList &&
!this.languageFeatures.includeList.includes(tokenType)
)
this.disabledLanguageFeatureError('IncludeListViolation', {
IncludeList: this.languageFeatures.IncludeList,
includeList: this.languageFeatures.includeList,
tokenType,
lexeme,
})
}

Expand Down
4 changes: 2 additions & 2 deletions app/javascript/interpreter/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@
"VariableNotDeclared": "Did you forget to use the `set` keyword to create a variable called `{{name}}` before this line of code?"
},
"disabledLanguageFeature": {
"ExcludeListViolation": "Usage of '{{tokenType}}' is not allowed`.",
"IncludeListViolation": "Usage of '{{tokenType}}' is not allowed`."
"ExcludeListViolation": "Jiki doesn't know how to use `{{lexeme}}` in this exercise.",
"IncludeListViolation": "Jiki doesn't know how to use `{{lexeme}}` in this exercise."
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"level": 3,
"idx": 3,
"tests_type": "state",
"interpreter_options": {
"exclude_list": ["OR"]
},
"tasks": [
{
"name": "Player 1 chooses paper",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,15 +458,15 @@ describe('error', () => {
})

test('Exclude listed', () => {
expect(() => scan('const x = 1', { ExcludeList: ['CONST'] })).toThrow(
"Usage of 'CONST' is not allowed"
expect(() => scan('const x = 1', { excludeList: ['CONST'] })).toThrow(
"Jiki doesn't know how to use `const` in this exercise."
)
})

test('Include listed', () => {
expect(() =>
scan('const x = 1', { IncludeList: ['IDENTIFIER', 'NUMBER'] })
).toThrow("Usage of 'CONST' is not allowed")
scan('const x = 1', { includeList: ['IDENTIFIER', 'NUMBER'] })
).toThrow("Jiki doesn't know how to use `const` in this exercise.")
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,17 +451,17 @@ describe('error', () => {
})

test('Exclude listed', () => {
expect(() => scan('set x to 1', { ExcludeList: ['SET'] })).toThrow(
"Usage of 'SET' is not allowed"
expect(() => scan('set x to 1', { excludeList: ['SET'] })).toThrow(
"Jiki doesn't know how to use `set` in this exercise."
)
})

test('Include listed', () => {
expect(() =>
scan('set x to 1', {
IncludeList: ['IDENTIFIER', 'NUMBER'],
includeList: ['IDENTIFIER', 'NUMBER'],
})
).toThrow("Usage of 'SET' is not allowed")
).toThrow("Jiki doesn't know how to use `set` in this exercise.")
})
})
})
Expand Down

0 comments on commit ed1b1e8

Please sign in to comment.