Passed
Push — main ( 8104af...1d7420 )
by Dylan
04:05
created

src/Parser.ts   A

Complexity

Total Complexity 6
Complexity/F 3

Size

Lines of Code 57
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
eloc 26
dl 0
loc 57
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0
mnd 4
bc 4
fnc 2
bpm 2
cpm 3
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A Parser.evaluate 0 24 5
A Parser.toString 0 6 1
1
import { Token, TokenType } from "./Token"
2
3
interface Identifiers<T> {
4
  [Key: string]: T;
5
}
6
7
/**
8
 * @class Parser
9
 * @name Parser
10
 */
11 2
export class Parser {
12
  tokens: Generator<Token>
13 7
  identifiers: Identifiers<string> = {}
14
15
  /**
16
   * Initialize a parser from a token generator.
17
   */
18
  constructor(tokens: Generator<Token>) {
19 7
    this.tokens = tokens
20
  }
21
22
  /**
23
   * Evaluate the result.
24
   */
25
  evaluate(): bigint {
26 6
    let n = 0n
27 6
    for(const x of this.tokens) {
28 8
      if (x.type === TokenType.identifier) {
29 3
        if (!(x.s in this.identifiers)) {
30 2
          throw `"${x.s}" is undefined`
31
        }
32 1
        n += BigInt(this.identifiers[x.s])
33
      }
34
      // else if (x.type === TokenType.separator) {
35
      // }
36
      // else if (x.type === TokenType.operator) {
37
      //   if (x.s === '=') {
38
      //   }
39
      // }
40 5
      else if (x.type === TokenType.literal) {
41 3
        n += BigInt(x.s)
42
      }
43
    }
44 4
    return n
45
  }
46
47
  /**
48
   * The text representation.
49
   */
50
  toString(): string {
51 2
    return this.evaluate().toString()
52
  }
53
54
}
55
56
export default Parser
57