-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hugr-py): builder for function definition/declaration and call
- Loading branch information
Showing
7 changed files
with
306 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
|
||
import hugr._ops as ops | ||
import hugr._val as val | ||
|
||
from ._dfg import _DfBase | ||
from hugr._node_port import Node | ||
from ._hugr import Hugr | ||
from ._tys import TypeRow, TypeParam, PolyFuncType | ||
|
||
|
||
@dataclass | ||
class Function(_DfBase[ops.FuncDefn]): | ||
def __init__( | ||
self, | ||
name: str, | ||
input_types: TypeRow, | ||
type_params: list[TypeParam] | None = None, | ||
) -> None: | ||
root_op = ops.FuncDefn(name, input_types, type_params or []) | ||
super().__init__(root_op) | ||
|
||
|
||
@dataclass | ||
class Module: | ||
hugr: Hugr | ||
|
||
def __init__(self) -> None: | ||
self.hugr = Hugr(ops.Module()) | ||
|
||
def define_function( | ||
self, | ||
name: str, | ||
input_types: TypeRow, | ||
type_params: list[TypeParam] | None = None, | ||
) -> Function: | ||
parent_op = ops.FuncDefn(name, input_types, type_params or []) | ||
return Function.new_nested(parent_op, self.hugr) | ||
|
||
def define_main(self, input_types: TypeRow) -> Function: | ||
return self.define_function("main", input_types) | ||
|
||
def declare_function(self, name: str, signature: PolyFuncType) -> Node: | ||
return self.hugr.add_node(ops.FuncDecl(name, signature), self.hugr.root) | ||
|
||
def add_const(self, value: val.Value) -> Node: | ||
return self.hugr.add_node(ops.Const(value), self.hugr.root) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.