From d177285a761de33b86fcbde72fd718137bfa6f86 Mon Sep 17 00:00:00 2001 From: Xander Date: Thu, 17 Aug 2023 22:08:21 +0200 Subject: Add valid/unvalid type, replacement of optional --- kernel/memory/paging.dats | 6 ++++++ kernel/memory/paging.sats | 2 ++ lib/DATS/valid.dats | 15 +++++++++++++++ lib/SATS/valid.sats | 18 ++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 lib/DATS/valid.dats create mode 100644 lib/SATS/valid.sats diff --git a/kernel/memory/paging.dats b/kernel/memory/paging.dats index 9b55624..84a8bf4 100644 --- a/kernel/memory/paging.dats +++ b/kernel/memory/paging.dats @@ -6,6 +6,7 @@ staload UN = "prelude/SATS/unsafe.sats" staload "./paging.sats" staload "./frame.sats" +staload "lib/SATS/valid.sats" assume entry_type = uint64 @@ -28,3 +29,8 @@ implement flag_contains(entry,flag) = | NO_EXECUTE() => 63 ))) != zero +(* implement test() = let *) +(* in *) +(* (5,true) *) +(* end *) + diff --git a/kernel/memory/paging.sats b/kernel/memory/paging.sats index 784f567..477e942 100644 --- a/kernel/memory/paging.sats +++ b/kernel/memory/paging.sats @@ -1,6 +1,7 @@ #define ENTRY_COUNT 512 staload "./frame.sats" +staload "lib/SATS/valid.sats" abstype paddr_type(l:addr) = ptr(l) typedef paddr(l:addr) = paddr_type(l) @@ -31,3 +32,4 @@ fn is_unused(entry_t) : bool fn set_unused(&entry_t) : void fn flag_contains(entry_t,entry_flag) : bool +fn test() : [b : bool] valid(int,b) diff --git a/lib/DATS/valid.dats b/lib/DATS/valid.dats new file mode 100644 index 0000000..f4ea0aa --- /dev/null +++ b/lib/DATS/valid.dats @@ -0,0 +1,15 @@ +#define ATS_DYNLOADFLAG 0 +staload "lib/SATS/valid.sats" + +assume valid(a:t@ype,b:bool) = (a,bool b) + +implement{a} create_valid(value : a) = (value,true) + +// Create unvalid value by casting null pointer to type. +implement{a} create_unvalid() = let + staload UN = "prelude/SATS/unsafe.sats" +in + ($UN.ptr0_get(the_null_ptr),false) //UNSAFE: +end + +implement{a} unwrap_valid(v) = v.0 diff --git a/lib/SATS/valid.sats b/lib/SATS/valid.sats new file mode 100644 index 0000000..e3841e0 --- /dev/null +++ b/lib/SATS/valid.sats @@ -0,0 +1,18 @@ + +// implement a type that can be unvalid or valid. +// It's a replacement of Optional, but can be allocated on stack, so no heap allocaton needed. +// When unvalid, it should be not possible to get unvalid value, thats why abstract type is used. +// bool b: true means valid, false unvalid +abst@ype valid(a:t@ype,b:bool) = (a,bool b) + +typedef Valid(a:t@ype) = [b : bool] valid(a,b) + +// Wraps valid value. +fn {a:t@ype} create_valid(value : a) : valid(a,true) + +// Creates unvalid type. +fn {a:t@ype} create_unvalid() : valid(a,false) + +// Only function that allows getting value. Only accepts valid values. +fn {a:t@ype} unwrap_valid(valid(a,true)) : a + -- cgit v1.2.3