Skip to content

Commit c965432

Browse files
committed
WIP
1 parent 4094120 commit c965432

File tree

2 files changed

+70
-17
lines changed

2 files changed

+70
-17
lines changed

src/Codec/CBOR/Cuddle/CDDL/Resolve.hs

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ import Codec.CBOR.Cuddle.CDDL (
6767
XXType2,
6868
cddlTopLevel,
6969
)
70-
import Codec.CBOR.Cuddle.CDDL.Postlude (PTerm (..))
7170
import Codec.CBOR.Cuddle.IndexMappable (IndexMappable (..))
7271
import Control.Monad.Except (ExceptT (..), runExceptT)
7372
import Control.Monad.Reader (Reader, ReaderT (..), runReader)
@@ -224,7 +223,7 @@ buildRefCTree rules = PartialCTreeRoot $ bimap mapIndex toCTreeRule rules
224223
-- We don't validate numerical items yet
225224
NE.singleton T2Any
226225
toCTreeT2 T2Any = NE.singleton T2Any
227-
toCTreeT2 (XXType2 x) = undefined
226+
toCTreeT2 x@(XXType2 _) = NE.singleton $ mapIndex x
228227

229228
toCTreeDataItem :: Word64 -> Type2 OrReferenced
230229
toCTreeDataItem 20 =
@@ -404,18 +403,20 @@ resolveCTree ::
404403
BindingEnv OrReferenced OrReferenced ->
405404
Type1 OrReferenced ->
406405
Either NameResolutionFailure (TypeOrGroup DistReferenced)
407-
resolveCTree e = CTree.traverseCTree (resolveRef e) (resolveCTree e)
406+
resolveCTree e = undefined -- CTree.traverseCTree (resolveRef e) (resolveCTree e)
408407

409408
buildResolvedCTree ::
410409
PartialCTreeRoot OrReferenced ->
411410
Either NameResolutionFailure (PartialCTreeRoot DistReferenced)
412-
buildResolvedCTree (PartialCTreeRoot ct) = PartialCTreeRoot <$> traverse go ct
411+
buildResolvedCTree (PartialCTreeRoot ct) = undefined -- PartialCTreeRoot <$> traverse go ct
413412
where
414413
go pn =
415-
let args = parameters pn
416-
localBinds = Map.fromList $ zip args (CTreeE . flip Ref [] <$> args)
414+
let argNames = parameters pn
415+
argTerms = (\x -> Type1 x Nothing mempty) . XXType2 . (\n -> Ref False n []) <$> argNames
416+
localBinds =
417+
Map.fromList $ zip argNames argTerms
417418
env = BindingEnv @OrReferenced @OrReferenced ct localBinds
418-
in traverse (resolveCTree env) pn
419+
in undefined pn -- traverse (resolveCTree env) pn
419420

420421
--------------------------------------------------------------------------------
421422
-- 4. Monomorphisation
@@ -458,10 +459,10 @@ newtype MonoM a = MonoM
458459
deriving
459460
( HasSource
460461
"local"
461-
(Map.Map (Name MonoReferenced) (TypeOrGroup MonoReferenced))
462+
(Map.Map (Name MonoReferenced) (Type1 MonoReferenced))
462463
, HasReader
463464
"local"
464-
(Map.Map (Name MonoReferenced) (TypeOrGroup MonoReferenced))
465+
(Map.Map (Name MonoReferenced) (Type1 MonoReferenced))
465466
)
466467
via Field
467468
"local"
@@ -504,7 +505,7 @@ throwNR :: NameResolutionFailure -> MonoM a
504505
throwNR = throw @"nameResolution"
505506

506507
-- | Synthesize a monomorphic rule definition, returning the name
507-
synthMono :: Name DistReferenced -> [TypeOrGroup DistReferenced] -> MonoM (Name phase)
508+
synthMono :: Name DistReferenced -> [TypeOrGroup DistReferenced] -> MonoM (Name MonoReferenced)
508509
synthMono n@(Name origName _) args =
509510
let fresh =
510511
-- % is not a valid CBOR name, so this should avoid conflict
@@ -517,32 +518,33 @@ synthMono n@(Name origName _) args =
517518
Just (ProvidedParameters params' r) ->
518519
if length params' == length args
519520
then do
520-
rargs <- traverse resolveGenericCTree args
521+
rargs <- undefined -- traverse resolveGenericCTree args
521522
let localBinds = Map.fromList $ zip params' rargs
522523
Reader.local @"local" (Map.union localBinds) $ do
523524
foo <- resolveGenericCTree r
524525
modify @"synth" $ Map.insert fresh foo
525-
else throwNR $ MismatchingArgs n params'
526-
Nothing -> throwNR $ UnboundReference n
526+
else throwNR $ MismatchingArgs (mapIndex n) params'
527+
Nothing -> throwNR $ UnboundReference (mapIndex n)
527528
pure fresh
528529

529530
resolveGenericRef ::
530531
XXType2 DistReferenced ->
531532
MonoM (TypeOrGroup MonoReferenced)
532-
resolveGenericRef (RuleRef n []) = pure . CTreeE $ MRuleRef n
533+
resolveGenericRef (RuleRef n []) = pure . undefined $ MRuleRef n
533534
resolveGenericRef (RuleRef n args) = do
534535
fresh <- synthMono n args
535-
pure . CTreeE $ MRuleRef fresh
536+
pure . TOGType . Type0 . NE.singleton $ Type1 (XXType2 $ MRuleRef fresh) Nothing mempty
536537
resolveGenericRef (GenericRef n) = do
537538
localBinds <- ask @"local"
538539
case Map.lookup n localBinds of
539540
Just node -> pure node
540541
Nothing -> throwNR $ UnboundReference n
542+
resolveGenericRef (DistPostlude _) = undefined
541543

542544
resolveGenericCTree ::
543545
TypeOrGroup DistReferenced ->
544546
MonoM (TypeOrGroup MonoReferenced)
545-
resolveGenericCTree = CTree.traverseCTree resolveGenericRef resolveGenericCTree
547+
resolveGenericCTree = undefined -- CTree.traverseCTree resolveGenericRef resolveGenericCTree
546548

547549
data CTreeRoot i = CTreeRoot
548550

@@ -579,3 +581,54 @@ fullResolveCDDL cddl = do
579581
let refCTree = buildRefCTree (asMap cddl)
580582
rCTree <- buildResolvedCTree refCTree
581583
buildMonoCTree rCTree
584+
585+
-- |
586+
--
587+
-- CDDL predefines a number of names. This subsection summarizes these
588+
-- names, but please see Appendix D for the exact definitions.
589+
--
590+
-- The following keywords for primitive datatypes are defined:
591+
--
592+
-- "bool" Boolean value (major type 7, additional information 20
593+
-- or 21).
594+
--
595+
-- "uint" An unsigned integer (major type 0).
596+
--
597+
-- "nint" A negative integer (major type 1).
598+
--
599+
-- "int" An unsigned integer or a negative integer.
600+
--
601+
-- "float16" A number representable as a half-precision float [IEEE754]
602+
-- (major type 7, additional information 25).
603+
--
604+
-- "float32" A number representable as a single-precision float
605+
-- [IEEE754] (major type 7, additional information 26).
606+
--
607+
--
608+
-- "float64" A number representable as a double-precision float
609+
-- [IEEE754] (major type 7, additional information 27).
610+
--
611+
-- "float" One of float16, float32, or float64.
612+
--
613+
-- "bstr" or "bytes" A byte string (major type 2).
614+
--
615+
-- "tstr" or "text" Text string (major type 3).
616+
--
617+
-- (Note that there are no predefined names for arrays or maps; these
618+
-- are defined with the syntax given below.)
619+
data PTerm
620+
= PTBool
621+
| PTUInt
622+
| PTNInt
623+
| PTInt
624+
| PTHalf
625+
| PTFloat
626+
| PTDouble
627+
| PTBytes
628+
| PTText
629+
| PTAny
630+
| PTNil
631+
| PTUndefined
632+
deriving (Eq, Generic, Ord, Show)
633+
634+
instance Hashable PTerm

src/Codec/CBOR/Cuddle/Pretty.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Data.List.NonEmpty qualified as NE
2929
import Data.String (IsString, fromString)
3030
import Data.Text qualified as T
3131
import Data.TreeDiff (ToExpr)
32-
import Data.Void (Void, absurd)
32+
import Data.Void (Void)
3333
import GHC.Generics (Generic)
3434
import Optics.Core ((^.))
3535
import Prettyprinter

0 commit comments

Comments
 (0)