Skip to content

Commit

Permalink
feat(syntax): Support value in method_id attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
byakuren-hijiri committed Jul 20, 2024
1 parent 5961ff2 commit 9065452
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
21 changes: 16 additions & 5 deletions src/func/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ export class FuncFormatter {
node as FuncAstFunctionDefinition,
);
case "asm_function_definition":
return this.formatAsmFunction(
node as FuncAstAsmFunction,
);
return this.formatAsmFunction(node as FuncAstAsmFunction);
case "var_def_stmt":
return this.formatVarDefStmt(node as FuncAstVarDefStmt);
case "return_stmt":
Expand Down Expand Up @@ -193,13 +191,26 @@ export class FuncFormatter {
.join("");
}

private formatFunctionAttribute(attr: FuncAstFunctionAttribute): string {
switch (attr.kind) {
case "method_id":
return attr.value === undefined
? "method_id"
: `method_id(${attr.value})`;
case "impure":
case "inline":
case "inline_ref":
return attr.kind;
}
}

private formatFunctionSignature(
name: FuncAstIdExpr,
attrs: FuncAstFunctionAttribute[],
params: FuncAstFormalFunctionParam[],
returnTy: FuncType,
): string {
const attrsStr = attrs.join(" ");
const attrsStr = attrs.map(this.formatFunctionAttribute).join(" ");
const nameStr = this.dump(name);
const paramsStr = params
.map((param) => `${this.dump(param.ty)} ${this.dump(param.name)}`)
Expand Down Expand Up @@ -456,7 +467,7 @@ export class FuncFormatter {
}

private formatCR(node: FuncAstCR): string {
return '\n'.repeat(node.lines);
return "\n".repeat(node.lines);
}

private formatType(node: FuncType): string {
Expand Down
8 changes: 4 additions & 4 deletions src/func/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,10 @@ export type FuncAstConstant = {
};

export type FuncAstFunctionAttribute =
| "impure"
| "inline"
| "inline_ref"
| "method_id";
| { kind: "impure" }
| { kind: "inline" }
| { kind: "inline_ref" }
| { kind: "method_id"; value: number | undefined };

export type FuncAstFormalFunctionParam = {
kind: "function_param";
Expand Down
18 changes: 18 additions & 0 deletions src/func/syntaxConstructors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,24 @@ export class Type {
}
}

export class FunAttr {
public static impure(): FuncAstFunctionAttribute {
return { kind: "impure" };
}

public static inline(): FuncAstFunctionAttribute {
return { kind: "inline" };
}

public static inline_ref(): FuncAstFunctionAttribute {
return { kind: "inline_ref" };
}

public static method_id(value?: number): FuncAstFunctionAttribute {
return { kind: "method_id", value };
}
}

//
// Expressions
//
Expand Down

0 comments on commit 9065452

Please sign in to comment.