Compare commits

..

8 Commits

Author SHA1 Message Date
ychenfo 7ede18429f nac3core/artiq: raise dedicated UnwrapNoneError 2022-03-26 14:09:17 +08:00
ychenfo ddc7687ac6 nac3standalone: add tests for option 2022-03-26 13:23:57 +08:00
ychenfo 762a7ccc14 nac3core/artiq: use none instead of None 2022-03-26 13:23:57 +08:00
ychenfo 71d92a7a18 nac3artiq/core: host option object support 2022-03-26 13:23:57 +08:00
ychenfo 1aa698ccc7 nac3core: option type codegen support 2022-03-26 13:23:53 +08:00
ychenfo be6b6c9440 nac3core: remove previous handling of None
not to confuse with option none, and the None token is parsed as a special ast::constant instead of an ast::name so the handling is invalid
2022-03-26 13:23:27 +08:00
ychenfo 38f856e956 nac3core: fix broken tests 2022-03-26 13:23:27 +08:00
ychenfo 62d13e297c nac3core: option type front end 2022-03-26 13:23:23 +08:00
5 changed files with 15 additions and 7 deletions

View File

@ -16,7 +16,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):

View File

@ -11,7 +11,7 @@ from embedding_map import EmbeddingMap
__all__ = [
"Kernel", "KernelInvariant", "virtual",
"Option", "Some", "none",
"Option", "Some", "none", "UnwrapNoneError",
"round64", "floor64", "ceil64",
"extern", "kernel", "portable", "nac3",
"rpc", "ms", "us", "ns",
@ -47,7 +47,7 @@ class Option(Generic[T]):
def unwrap(self):
if self.is_none():
raise ValueError("unwrap on none")
raise UnwrapNoneError()
return self._nac3_option
def __repr__(self) -> str:
@ -274,5 +274,10 @@ class KernelContextManager:
def __exit__(self):
pass
@nac3
class UnwrapNoneError(Exception):
"""raised when unwrapping a none value"""
artiq_builtin = True
parallel = KernelContextManager()
sequential = KernelContextManager()

View File

@ -496,7 +496,8 @@ impl Nac3 {
"KeyError",
"NotImplementedError",
"OverflowError",
"IOError"
"IOError",
"UnwrapNoneError",
];
add_exceptions(&mut composer, &mut builtins_def, &mut builtins_ty, &exception_names);

View File

@ -1319,8 +1319,8 @@ pub fn gen_expr<'ctx, 'a, G: CodeGenerator>(
ctx.make_assert(
generator,
not_null,
"0:ValueError",
"unwrap on none",
"0:UnwrapNoneError",
"",
[None, None, None],
expr.location,
);

View File

@ -105,7 +105,8 @@ pub fn get_builtins(primitives: &mut (PrimitiveStore, Unifier)) -> BuiltinInfo {
("__param2__".into(), int64, true),
];
// for Option, is_some and is_none share the same type
// for Option, is_some and is_none share the same type: () -> bool,
// and they are methods under the same class `Option`
let (is_some_ty, unwrap_ty, (option_ty_var, option_ty_var_id)) =
if let TypeEnum::TObj { fields, params, .. } =
primitives.1.get_ty(primitives.0.option).as_ref()