param ids, indices

This commit is contained in:
sfja 2025-01-02 04:40:09 +01:00
parent 26acdc10ca
commit ac36353609
2 changed files with 39 additions and 17 deletions

View File

@ -111,6 +111,8 @@ export type Field = {
}; };
export type Param = { export type Param = {
id: number;
index?: number;
ident: string; ident: string;
etype?: EType; etype?: EType;
pos: Pos; pos: Pos;
@ -156,6 +158,7 @@ export type ETypeKind =
export type GenericParam = { export type GenericParam = {
id: number; id: number;
index: number;
ident: string; ident: string;
pos: Pos; pos: Pos;
vtype?: VType; vtype?: VType;
@ -171,21 +174,34 @@ export class AstCreator {
private nextNodeId = 0; private nextNodeId = 0;
public stmt(kind: StmtKind, pos: Pos, details?: StmtDetails): Stmt { public stmt(kind: StmtKind, pos: Pos, details?: StmtDetails): Stmt {
const id = this.nextNodeId; const id = this.genId();
this.nextNodeId += 1;
return { kind, pos, details, id }; return { kind, pos, details, id };
} }
public expr(kind: ExprKind, pos: Pos): Expr { public expr(kind: ExprKind, pos: Pos): Expr {
const id = this.nextNodeId; const id = this.genId();
this.nextNodeId += 1;
return { kind, pos, id }; return { kind, pos, id };
} }
public etype(kind: ETypeKind, pos: Pos): EType { public etype(kind: ETypeKind, pos: Pos): EType {
const id = this.genId();
return { kind, pos, id };
}
public param(val: Omit<Param, "id">): Param {
const id = this.genId();
return { ...val, id };
}
public genericParam(val: Omit<GenericParam, "id">): GenericParam {
const id = this.genId();
return { ...val, id };
}
private genId(): number {
const id = this.nextNodeId; const id = this.nextNodeId;
this.nextNodeId += 1; this.nextNodeId += 1;
return { kind, pos, id }; return id;
} }
} }

View File

@ -330,16 +330,15 @@ export class Parser {
return this.parseDelimitedList(this.parseETypeParam, ">", ","); return this.parseDelimitedList(this.parseETypeParam, ">", ",");
} }
private veryTemporaryETypeParamIdCounter = 0; private parseETypeParam(index: number): Res<GenericParam> {
private parseETypeParam(): Res<GenericParam> {
const pos = this.pos(); const pos = this.pos();
if (this.test("ident")) { if (this.test("ident")) {
const ident = this.current().identValue!; const ident = this.current().identValue!;
this.step(); this.step();
const id = this.veryTemporaryETypeParamIdCounter; return {
this.veryTemporaryETypeParamIdCounter += 1; ok: true,
return { ok: true, value: { id, ident, pos } }; value: this.astCreator.genericParam({ index, ident, pos }),
};
} }
this.report("expected generic parameter"); this.report("expected generic parameter");
return { ok: false }; return { ok: false };
@ -350,7 +349,7 @@ export class Parser {
} }
private parseDelimitedList<T>( private parseDelimitedList<T>(
parseElem: (this: Parser) => Res<T>, parseElem: (this: Parser, index: number) => Res<T>,
endToken: string, endToken: string,
delimiter: string, delimiter: string,
): T[] { ): T[] {
@ -359,8 +358,9 @@ export class Parser {
this.step(); this.step();
return []; return [];
} }
let i = 0;
const elems: T[] = []; const elems: T[] = [];
const elemRes = parseElem.call(this); const elemRes = parseElem.call(this, i);
if (!elemRes.ok) { if (!elemRes.ok) {
return []; return [];
} }
@ -370,7 +370,7 @@ export class Parser {
if (this.test(endToken)) { if (this.test(endToken)) {
break; break;
} }
const elemRes = parseElem.call(this); const elemRes = parseElem.call(this, i);
if (!elemRes.ok) { if (!elemRes.ok) {
return []; return [];
} }
@ -384,7 +384,7 @@ export class Parser {
return elems; return elems;
} }
private parseParam(): Res<Param> { private parseParam(index?: number): Res<Param> {
const pos = this.pos(); const pos = this.pos();
if (this.test("ident")) { if (this.test("ident")) {
const ident = this.current().identValue!; const ident = this.current().identValue!;
@ -392,9 +392,15 @@ export class Parser {
if (this.test(":")) { if (this.test(":")) {
this.step(); this.step();
const etype = this.parseEType(); 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"); this.report("expected param");
return { ok: false }; return { ok: false };