Skip to content

Commit

Permalink
refactor: use switch (true) {} pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Dec 11, 2023
1 parent ee38719 commit a4ed504
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions __tests__/features/assembler/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ end
describe('when parsing move instruction', () => {
it('should throw OperandTypeError if second operand is of wrong type', () => {
expect(() => {
parse('mov al, "bl"')
parse('mov al, bll')
}).toThrowErrorMatchingInlineSnapshot(
`"Expected number, address or register address, got '\\"bl\\"'."`,
`"Expected number, address or register address, got 'bll'."`,
)

expect(() => {
Expand Down
14 changes: 8 additions & 6 deletions src/features/assembler/core/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,14 @@ const parseSingleOperand =
}
break
case TokenType.Unknown:
if (isExpectedType((t = OperandType.Number))) {
if (NUMBER_REGEXP.test(token.value)) {
return createOperand(t, validateNumber(token))
}
} else if (isExpectedType((t = OperandType.Label))) {
return createOperand(t, validateLabel(token))
switch (true) {
case isExpectedType((t = OperandType.Number)):
if (NUMBER_REGEXP.test(token.value)) {
return createOperand(t, validateNumber(token))
}
break
case isExpectedType((t = OperandType.Label)):
return createOperand(t, validateLabel(token))
}
break
}
Expand Down
11 changes: 7 additions & 4 deletions src/features/controller/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,13 @@ export class Controller {
}

private stepFrom = (lastStepResult: StepResult | null): void => {
if (applySelector(selectEditorInput) !== applySelector(selectAssembledSource)) {
store.dispatch(setEditorMessage(sourceChangedMessage))
} else if (applySelector(selectEditorMessage) === sourceChangedMessage) {
store.dispatch(clearEditorMessage())
switch (true) {
case applySelector(selectEditorInput) !== applySelector(selectAssembledSource):
store.dispatch(setEditorMessage(sourceChangedMessage))
break
case applySelector(selectEditorMessage) === sourceChangedMessage:
store.dispatch(clearEditorMessage())
break
}
const { fault, halted } = applySelector(selectCpuStatus)
if (fault !== null || halted) {
Expand Down
11 changes: 7 additions & 4 deletions src/features/cpu/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,13 @@ export const step = (lastStepResult: StepResult, inputSignals: InputSignals): St
flags |= StatusRegisterFlag.Overflow
}
const result = __result > 0xff ? __result % 0x100 : unsign8(__result)
if (result === 0) {
flags |= StatusRegisterFlag.Zero
} else if (result >= 0x80) {
flags |= StatusRegisterFlag.Sign
switch (true) {
case result === 0:
flags |= StatusRegisterFlag.Zero
break
case result >= 0x80:
flags |= StatusRegisterFlag.Sign
break
}
const interruptFlag = __cpuRegisters.sr & StatusRegisterFlag.Interrupt
setSr(flags | interruptFlag)
Expand Down

0 comments on commit a4ed504

Please sign in to comment.