update NAC3, define option type

This commit is contained in:
Sebastien Bourdeauducq 2022-03-26 16:00:49 +08:00
parent a4c1f8a079
commit d2add0a683
4 changed files with 50 additions and 10 deletions

View File

@ -1,6 +1,7 @@
from artiq.language.core import nac3
from artiq.language.core import nac3, UnwrapNoneError
from builtins import ZeroDivisionError, ValueError, IndexError, RuntimeError, AssertionError
@nac3
class RTIOUnderflow(Exception):
"""Raised when the CPU or DMA core fails to submit a RTIO event early

View File

@ -6,7 +6,6 @@ from typing import Generic, TypeVar
from functools import wraps
from inspect import getfullargspec, getmodule
from types import SimpleNamespace
from typing import TypeVar
from math import floor, ceil
from artiq.language import import_cache
@ -16,9 +15,10 @@ __all__ = [
"Kernel", "KernelInvariant", "virtual",
"round64", "floor64", "ceil64",
"extern", "kernel", "portable", "nac3", "rpc",
"print_rpc",
"Option", "Some", "none", "UnwrapNoneError",
"parallel", "sequential",
"set_watchdog_factory", "watchdog", "TerminationRequested",
"print_rpc"
]
@ -109,13 +109,51 @@ def rpc(arg=None, flags={}):
return arg
T = TypeVar('T')
@rpc
def print_rpc(a: T):
print(a)
@nac3
class UnwrapNoneError(Exception):
"""Raised when unwrapping a none Option."""
artiq_builtin = True
class Option(Generic[T]):
_nac3_option: T
def __init__(self, v: T):
self._nac3_option = v
def is_none(self):
return self._nac3_option is None
def is_some(self):
return self._nac3_option is not None
def unwrap(self):
if self.is_none():
raise UnwrapNoneError()
return self._nac3_option
def __repr__(self) -> str:
if self.is_none():
return "none"
else:
return "Some({})".format(repr(self._nac3_option))
def __str__(self) -> str:
if self.is_none():
return "none"
else:
return "Some({})".format(str(self._nac3_option))
def Some(v: T) -> Option[T]:
return Option(v)
none = Option(None)
@nac3
class KernelContextManager:
@kernel

View File

@ -23,7 +23,8 @@ class EmbeddingMap:
"CacheError",
"SPIError",
"0:ZeroDivisionError",
"0:IndexError"])
"0:IndexError",
"0:UnwrapNoneError"])
def preallocate_runtime_exception_names(self, names):
for i, name in enumerate(names):

8
flake.lock generated
View File

@ -45,11 +45,11 @@
"nixpkgs": "nixpkgs"
},
"locked": {
"lastModified": 1648132215,
"narHash": "sha256-Sts6AGduDi4jsD6YSYR2bwHW35TByGgHAfVPrenrCxg=",
"lastModified": 1648280781,
"narHash": "sha256-HSelJSl7Q4LNI1cCApsUULidDCnRcXRocT+/e31iHL8=",
"ref": "master",
"rev": "55db05fdbb2ee35a907d32de778887716f71f5fe",
"revCount": 680,
"rev": "1ad4b0227c87736beec62d94161291b7e27de6cc",
"revCount": 685,
"type": "git",
"url": "https://git.m-labs.hk/m-labs/nac3.git"
},