m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/parser.js
blob: acae9ad1b2da426034f99746ba543f862264fc1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { lex } = require('./lexer.js')

let symbols = {}

let throwSyntaxError = () => {
  throw new Error('Syntax error: unexpected token')
}

let lexemeToToken = lexeme => {
  let token = Object.create(symbols[lexeme.type])
  token.value = lexeme.value
  return token
}

const dieBindingPower = 30

let newSymbol = (type, nud, lbp, led) => {
  const symbol = symbols[type] || {}
  symbols[type] = {
    type,
    nud: nud || symbol.nud || throwSyntaxError,
    lbp: symbol.lbp || lbp,
    led: led || symbol.led || throwSyntaxError
  }
}

const newInfix = (symbol, lbp, options) => {
  const type = options.type || symbol
  const rbp = options.rbp || lbp
  newSymbol(symbol, null, lbp, (left, parser) => {
    return {
      type: type,
      left: left,
      right: parser.expression(rbp)
    }
  })
}

const newDieOperation = (symbol) => {
  newInfix(symbol, dieBindingPower, { rbp: dieBindingPower - 1 })
}

newSymbol('constant', function() {
  return { type: 'constant', value: this.value }
})

newSymbol('(', function(parser) {
  const value = parser.expression(1)
  parser.match(')')
  return value
})

newSymbol(')')

newDieOperation('d')
newSymbol('d', (parser) => {
  return {
    type: 'd',
    left: { type: 'constant', value: 1 },
    right: parser.expression(dieBindingPower - 1)
  }
})
newDieOperation('E')
newDieOperation('e')
newDieOperation('K')
newDieOperation('k')
newDieOperation('A')
newDieOperation('a')
newDieOperation('T')
newDieOperation('t')

newInfix('bigPlus', 20, { type: 'add' })
newInfix('bigMinus', 20, { type: 'subtract' })
newInfix('bigTimes', 23, { type: 'multiply' })
newInfix('bigDivide', 23, { type: 'divide' })
newInfix('plus', 25, { type: 'bonusAdd' })
newInfix('minus', 25, { type: 'bonusSubtract' })
newInfix('times', 26, { type: 'bonusMultiply' })
newInfix('divide', 26, { type: 'bonusDivide' })
newSymbol('minus', (parser) => {
  return {
    type: 'negative',
    value: parser.expression(40)
  }
})

newInfix(' x ', 24, { type: 'repeat' })

newSymbol('end', null, -1)

const newParser = (tokens) => {
  return {
    tokens,
    currentToken: 0,
    token: function() { return this.tokens[this.currentToken] },
    advanceToken: function() { this.currentToken++ },
    match: function(token) {
      if (this.token().type === token) {
        this.advanceToken()
      } else {
        throw throwSyntaxError()
      }
    },
    expression: function(rbp) {
      let symbol = this.token()
      this.advanceToken()
      let left = symbol.nud(this)

      while (rbp < this.token().lbp) {
        symbol = this.token()
        this.advanceToken()
        left = symbol.led(left, this)
      }

      return left
    }
  }
}

const parse = expressionString => {
  const tokens = lex(expressionString).map(lexemeToToken)
  tokens.push(symbols.end)

  const parser = newParser(tokens)
  const expression = parser.expression(0)
  parser.match('end')

  return expression
}

exports.parse = parse