From ac363536099b3d6e867dcf76c5c507976d776e3a Mon Sep 17 00:00:00 2001 From: sfja Date: Thu, 2 Jan 2025 04:40:09 +0100 Subject: [PATCH] param ids, indices --- compiler/ast.ts | 26 +++++++++++++++++++++----- compiler/parser.ts | 30 ++++++++++++++++++------------ 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/compiler/ast.ts b/compiler/ast.ts index c9d81f0..1a3901d 100644 --- a/compiler/ast.ts +++ b/compiler/ast.ts @@ -111,6 +111,8 @@ export type Field = { }; export type Param = { + id: number; + index?: number; ident: string; etype?: EType; pos: Pos; @@ -156,6 +158,7 @@ export type ETypeKind = export type GenericParam = { id: number; + index: number; ident: string; pos: Pos; vtype?: VType; @@ -171,21 +174,34 @@ export class AstCreator { private nextNodeId = 0; public stmt(kind: StmtKind, pos: Pos, details?: StmtDetails): Stmt { - const id = this.nextNodeId; - this.nextNodeId += 1; + const id = this.genId(); return { kind, pos, details, id }; } public expr(kind: ExprKind, pos: Pos): Expr { - const id = this.nextNodeId; - this.nextNodeId += 1; + const id = this.genId(); return { kind, pos, id }; } public etype(kind: ETypeKind, pos: Pos): EType { + const id = this.genId(); + return { kind, pos, id }; + } + + public param(val: Omit): Param { + const id = this.genId(); + return { ...val, id }; + } + + public genericParam(val: Omit): GenericParam { + const id = this.genId(); + return { ...val, id }; + } + + private genId(): number { const id = this.nextNodeId; this.nextNodeId += 1; - return { kind, pos, id }; + return id; } } diff --git a/compiler/parser.ts b/compiler/parser.ts index 03f6fbd..98b5c66 100644 --- a/compiler/parser.ts +++ b/compiler/parser.ts @@ -330,16 +330,15 @@ export class Parser { return this.parseDelimitedList(this.parseETypeParam, ">", ","); } - private veryTemporaryETypeParamIdCounter = 0; - - private parseETypeParam(): Res { + private parseETypeParam(index: number): Res { const pos = this.pos(); if (this.test("ident")) { const ident = this.current().identValue!; this.step(); - const id = this.veryTemporaryETypeParamIdCounter; - this.veryTemporaryETypeParamIdCounter += 1; - return { ok: true, value: { id, ident, pos } }; + return { + ok: true, + value: this.astCreator.genericParam({ index, ident, pos }), + }; } this.report("expected generic parameter"); return { ok: false }; @@ -350,7 +349,7 @@ export class Parser { } private parseDelimitedList( - parseElem: (this: Parser) => Res, + parseElem: (this: Parser, index: number) => Res, endToken: string, delimiter: string, ): T[] { @@ -359,8 +358,9 @@ export class Parser { this.step(); return []; } + let i = 0; const elems: T[] = []; - const elemRes = parseElem.call(this); + const elemRes = parseElem.call(this, i); if (!elemRes.ok) { return []; } @@ -370,7 +370,7 @@ export class Parser { if (this.test(endToken)) { break; } - const elemRes = parseElem.call(this); + const elemRes = parseElem.call(this, i); if (!elemRes.ok) { return []; } @@ -384,7 +384,7 @@ export class Parser { return elems; } - private parseParam(): Res { + private parseParam(index?: number): Res { const pos = this.pos(); if (this.test("ident")) { const ident = this.current().identValue!; @@ -392,9 +392,15 @@ export class Parser { if (this.test(":")) { this.step(); const etype = this.parseEType(); - return { ok: true, value: { ident, etype, pos } }; + return { + ok: true, + value: this.astCreator.param({ index, ident, etype, pos }), + }; } - return { ok: true, value: { ident, pos } }; + return { + ok: true, + value: this.astCreator.param({ index, ident, pos }), + }; } this.report("expected param"); return { ok: false };