-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #450. Depends on #449. * Adds a `guppy.constant` function to declare constants in modules * Adds a `pi` constant to the angle module for convenient writing of angles
- Loading branch information
Showing
4 changed files
with
148 additions
and
24 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,62 @@ | ||
import ast | ||
from dataclasses import dataclass, field | ||
|
||
from hugr import Node, Wire | ||
from hugr import val as hv | ||
from hugr.build.dfg import DefinitionBuilder, OpVar | ||
|
||
from guppylang.ast_util import AstNode | ||
from guppylang.checker.core import Globals | ||
from guppylang.compiler.core import CompiledGlobals, DFContainer | ||
from guppylang.definition.common import CompilableDef, ParsableDef | ||
from guppylang.definition.value import CompiledValueDef, ValueDef | ||
from guppylang.tys.parsing import type_from_ast | ||
|
||
|
||
@dataclass(frozen=True) | ||
class RawConstDef(ParsableDef): | ||
"""A raw constant definition as provided by the user.""" | ||
|
||
type_ast: ast.expr | ||
value: hv.Value | ||
|
||
description: str = field(default="constant", init=False) | ||
|
||
def parse(self, globals: Globals) -> "ConstDef": | ||
"""Parses and checks the user-provided signature of the function.""" | ||
return ConstDef( | ||
self.id, | ||
self.name, | ||
self.defined_at, | ||
type_from_ast(self.type_ast, globals, None), | ||
self.type_ast, | ||
self.value, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ConstDef(RawConstDef, ValueDef, CompilableDef): | ||
"""A constant with a checked type.""" | ||
|
||
def compile_outer(self, graph: DefinitionBuilder[OpVar]) -> "CompiledConstDef": | ||
const_node = graph.add_const(self.value) | ||
return CompiledConstDef( | ||
self.id, | ||
self.name, | ||
self.defined_at, | ||
self.ty, | ||
self.type_ast, | ||
self.value, | ||
const_node, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class CompiledConstDef(ConstDef, CompiledValueDef): | ||
"""A constant that has been compiled to a Hugr node.""" | ||
|
||
const_node: Node | ||
|
||
def load(self, dfg: DFContainer, globals: CompiledGlobals, node: AstNode) -> Wire: | ||
"""Loads the extern value into a local Hugr dataflow graph.""" | ||
return dfg.builder.load(self.const_node) |
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