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 = {
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, "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;
this.nextNodeId += 1;
return { kind, pos, id };
return id;
}
}

View File

@ -330,16 +330,15 @@ export class Parser {
return this.parseDelimitedList(this.parseETypeParam, ">", ",");
}
private veryTemporaryETypeParamIdCounter = 0;
private parseETypeParam(): Res<GenericParam> {
private parseETypeParam(index: number): Res<GenericParam> {
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<T>(
parseElem: (this: Parser) => Res<T>,
parseElem: (this: Parser, index: number) => Res<T>,
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<Param> {
private parseParam(index?: number): Res<Param> {
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 };