/* global describe, it, xit */
/* jslint node: true, esnext: true */

"use strict";

const chai = require('chai');
const assert = chai.assert;
const expect = chai.expect;
const should = chai.should();

const createGrammar = require('../lib/grammar').createGrammar;

describe("calculator",
  function () {
    function Value(value) {
      return Object.create(null, {
        value: {
          value: value
        }
      });
    }

    let myGrammar = createGrammar({
      terminals: {
        'number': {}
      },
      prefix: {
        '(': {
          nud(grammar) {
            const e = grammar.expression(0);
            grammar.advance(')');
            return e;
          }
        }
      },
      infix: {
        ')': {},
        '+': {
          precedence: 50,
          combine: (left, right) => Value(left.value + right.value)
        },
        '-': {
          precedence: 50,
          ccombine: (left, right) => Value(left.value - right.value)
        },
        '*': {
          precedence: 60,
          combine: (left, right) => Value(left.value * right.value)
        },
        '/': {
          precedence: 60,
          combine: (left, right) => Value(left.value / right.value)
        }
      }
    });

    xit("evaluates", () => assert.equal(myGrammar.parse("1 + 41 * 3 - 2").value, 122));
    it("evaluates with prefix op", () => assert.equal(myGrammar.parse("(1 + 41)").value, 42));
    it("evaluates with prefix op 2", () => assert.equal(myGrammar.parse("(1 + 41) * 2").value, 84));
    it("evaluates with prefix op 3", () => assert.equal(myGrammar.parse("(1 + 1) * (2 + 7)").value, 18));
    it("evaluates with prefix op 4", () => assert.equal(myGrammar.parse("(1 + (1 + 4 * 3)) * (2 + 1)").value, 42));
  });

/* global describe, it, xit */
/* jslint node: true, esnext: true */

'use strict';

const chai = require('chai');
const assert = chai.assert;
const expect = chai.expect;
const should = chai.should();

const createGrammar = require('../lib/grammar').createGrammar;

describe('calculator',
  function () {
    function Value(value) {
      return Object.create(null, {
        value: {
          value: value
        }
      });
    }

    let myGrammar = createGrammar({
      terminals: {
        number: {}
      },
      prefix: {
        '(': {
          nud(grammar) {
            const e = grammar.expression(0);
            grammar.advance(')');
            return e;
          }
        }
      },
      infix: {
        ')': {},
        '+': {
          precedence: 50,
          combine: (left, right) => Value(left.value + right.value)
        },
        '-': {
          precedence: 50,
          ccombine: (left, right) => Value(left.value - right.value)
        },
        '*': {
          precedence: 60,
          combine: (left, right) => Value(left.value * right.value)
        },
        '/': {
          precedence: 60,
          combine: (left, right) => Value(left.value / right.value)
        }
      }
    });

    xit('evaluates', () => assert.equal(myGrammar.parse('1 + 41 * 3 - 2').value, 122));
    it('evaluates with prefix op', () => assert.equal(myGrammar.parse('(1 + 41)').value, 42));
    it('evaluates with prefix op 2', () => assert.equal(myGrammar.parse('(1 + 41) * 2').value, 84));
    it('evaluates with prefix op 3', () => assert.equal(myGrammar.parse('(1 + 1) * (2 + 7)').value, 18));
    it('evaluates with prefix op 4', () => assert.equal(myGrammar.parse('(1 + (1 + 4 * 3)) * (2 + 1)').value, 42));
  });

use ' for string literals