• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mellonis / turing-machine-js / 10975774899

21 Sep 2024 09:24PM UTC coverage: 93.296% (-0.1%) from 93.407%
10975774899

Pull #77

github

web-flow
Merge 89aa13904 into 6ad236418
Pull Request #77: #15 Write code in TS

166 of 186 branches covered (89.25%)

Branch coverage included in aggregate %.

212 of 223 new or added lines in 12 files covered. (95.07%)

335 of 351 relevant lines covered (95.44%)

330.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/packages/machine/src/classes/State.ts
1
import Command from './Command';
2
import Reference from './Reference';
3
import TapeBlock from './TapeBlock';
4
import TapeCommand from './TapeCommand';
5
import {id} from '../utilities/functions';
6

7
export const ifOtherSymbol = Symbol('other symbol');
13✔
8

9
export default class State {
10
  readonly #id: number = id(this);
134✔
11

12
  readonly #name: string;
13

14
  #overrodeHaltState: State | null = null;
134✔
15

16
  #symbolToDataMap = new Map<symbol, { command: Command, nextState: State | Reference }>();
134✔
17

18
  constructor(stateDefinition: Record<string | symbol, {
24✔
19
    command?: Command | ConstructorParameters<typeof TapeCommand>[0] | ConstructorParameters<typeof TapeCommand>[0][],
20
    nextState?: State | Reference,
21
  }> | null = null, name?: string) {
22
    if (stateDefinition) {
134✔
23
      const keys = Object.getOwnPropertyNames(stateDefinition);
83✔
24

25
      if (keys.length) {
83!
26
        throw new Error(`invalid state definition while constructing state #${this.#id}`);
×
27
      }
28

29
      const symbols = Object.getOwnPropertySymbols(stateDefinition);
83✔
30

31
      if (symbols.length === 0) {
83✔
32
        throw new Error(`invalid state definition while constructing state #${this.#id}`);
2✔
33
      }
34

35
      symbols.forEach((symbol) => {
81✔
36
        const {nextState} = stateDefinition[symbol];
167✔
37
        const nextStateLocal = nextState ?? this;
167✔
38

39
        if (!(nextStateLocal instanceof State) && !(nextStateLocal instanceof Reference)) {
167!
NEW
40
          throw new Error('invalid nextState');
×
41
        }
42

43
        let {command} = stateDefinition[symbol];
167✔
44

45
        if (command == null) {
167✔
46
          command = new Command([
58✔
47
            new TapeCommand({}),
48
          ]);
49
        }
50

51
        if (!(command instanceof Command) && !Array.isArray(command)) {
167✔
52
          command = [command];
96✔
53
        }
54

55
        let commandLocal = command;
167✔
56

57
        if (Array.isArray(command)) {
167✔
58
          try {
109✔
59
            commandLocal = new Command(command);
109✔
60
          } catch (error) {
NEW
61
            void error;
×
62
          }
63
        }
64

65
        if (!(commandLocal instanceof Command)) {
167!
66
          throw new Error('invalid command');
×
67
        }
68

69
        this.#symbolToDataMap.set(symbol, {
167✔
70
          command: commandLocal,
71
          nextState: nextStateLocal,
72
        });
73
      });
74
    }
75

76
    this.#name = name ?? `id:${this.#id}`;
132✔
77
  }
78

79
  get id() {
80
    return this.#id;
2✔
81
  }
82

83
  get name() {
84
    return this.#name;
34✔
85
  }
86

87
  get isHalt() {
88
    return this.#id === 0;
3,207✔
89
  }
90

91
  get overrodeHaltState() {
92
    return this.#overrodeHaltState;
670✔
93
  }
94

95
  get ref() {
96
    return this;
962✔
97
  }
98

99
  getSymbol(tapeBlock: TapeBlock) {
100
    const symbol = [...this.#symbolToDataMap.keys()].find((currentSymbol) => tapeBlock.isMatched({
1,791✔
101
      symbol: currentSymbol,
102
    }));
103

104
    if (symbol) {
1,048!
105
      return symbol;
1,048✔
106
    }
107

108
    return ifOtherSymbol;
×
109
  }
110

111
  getCommand(symbol: symbol) {
112
    if (this.#symbolToDataMap.has(symbol)) {
1,050✔
113
      return this.#symbolToDataMap.get(symbol)!.command;
1,048✔
114
    }
115

116
    throw new Error(`No command for symbol at state named ${this.#name}`);
2✔
117
  }
118

119
  getNextState(symbol: symbol) {
120
    if (this.#symbolToDataMap.has(symbol)) {
1,050✔
121
      return this.#symbolToDataMap.get(symbol)!.nextState;
1,048✔
122
    }
123

124
    throw new Error(`No nextState for symbol at state named ${this.#id}`);
2✔
125
  }
126

127
  withOverrodeHaltState(overrodeHaltState: State) {
128
    const state = new State(null, `${this.name}>${overrodeHaltState.name}`);
14✔
129

130
    state.#symbolToDataMap = this.#symbolToDataMap;
14✔
131
    state.#overrodeHaltState = overrodeHaltState;
14✔
132

133
    return state;
14✔
134
  }
135
}
136

137
export const haltState = new State(null);
13✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc