summaryrefslogtreecommitdiffstats
path: root/llparse/src/implementation/c/node/consume.ts
blob: 658a00e14797f01e353ce1fbdcbf5593a9b807ad (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
import * as frontend from 'llparse-frontend';

import { Compilation } from '../compilation';
import { Node } from './base';

export class Consume extends Node<frontend.node.Consume> {
  public doBuild(out: string[]): void {
    const ctx = this.compilation;

    const index = ctx.stateField(this.ref.field);
    const ty = ctx.getFieldType(this.ref.field);

    let fieldTy: string;
    if (ty === 'i64') {
      fieldTy = 'uint64_t';
    } else if (ty === 'i32') {
      fieldTy = 'uint32_t';
    } else if (ty === 'i16') {
      fieldTy = 'uint16_t';
    } else if (ty === 'i8') {
      fieldTy = 'uint8_t';
    } else {
      throw new Error(
        `Unsupported type ${ty} of field ${this.ref.field} for consume node`);
    }

    out.push('size_t avail;');
    out.push(`${fieldTy} need;`);

    out.push('');
    out.push(`avail = ${ctx.endPosArg()} - ${ctx.posArg()};`);
    out.push(`need = ${index};`);

    // Note: `avail` or `need` are going to coerced to the largest
    // datatype needed to hold either of the values.
    out.push('if (avail >= need) {');
    out.push(`  p += need;`);
    out.push(`  ${index} = 0;`);
    const tmp: string[] = [];
    this.tailTo(tmp, this.ref.otherwise!);
    ctx.indent(out, tmp, '  ');
    out.push('}');
    out.push('');

    out.push(`${index} -= avail;`);
    this.pause(out);
  }
}