about summary refs log tree commit diff stats
path: root/js/baba-yaga/tests/parser-with-header.test.js
blob: f9de453f8b48ab2799c9e58b0b55ae013e276dae (plain) (blame)
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
import assert from 'assert';
import { createLexer } from '../src/core/lexer.js';
import { createParser } from '../src/core/parser.js';

function parse(code) {
  const lexer = createLexer(code);
  const tokens = lexer.allTokens();
  const parser = createParser(tokens);
  return parser.parse();
}

describe('parser: with header', () => {
  it('parses basic with header', () => {
    const ast = parse('f : x -> with (a : x + 1; b : a * 2;) -> a + b;');
    const fn = ast.body[0];
    assert.strictEqual(fn.type, 'FunctionDeclaration');
    assert.strictEqual(fn.body.type, 'WithHeader');
    assert.strictEqual(fn.body.entries.length, 2);
    assert.strictEqual(fn.body.entries[0].type, 'WithAssign');
  });

  it('parses typed locals in header', () => {
    const ast = parse('g : (x: Int) -> Int -> with (a Int; a : x;) -> a;');
    const fn = ast.body[0];
    assert.strictEqual(fn.body.entries[0].type, 'WithTypeDecl');
    assert.strictEqual(fn.body.entries[1].type, 'WithAssign');
  });

  it('parses with rec variant', () => {
    const ast = parse('h : -> with rec (f : x -> x; g : y -> y;) -> 0;');
    const fn = ast.body[0];
    assert.strictEqual(fn.body.recursive, true);
  });
});