Skip to content

Commit

Permalink
VM constructors matches the order in the declared type.
Browse files Browse the repository at this point in the history
Fixes #79
  • Loading branch information
yav committed Jun 22, 2022
1 parent 1dbbe7e commit dd392a5
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion daedalus-core/src/Daedalus/Core/Basics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ data TName = TName
deriving (Generic, NFData)

-- | What "flavor of type" we have
data TFlav = TFlavStruct
data TFlav = TFlavStruct [Label] -- ^ A struct with fields in this order
| TFlavEnum [Label] -- ^ A sum type with no data
| TFlavUnion [Label] -- ^ A sum type with data
deriving (Generic, NFData)
Expand Down
2 changes: 2 additions & 0 deletions daedalus-core/src/Daedalus/Core/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ data Expr =
Var Name
| PureLet Name Expr Expr
| Struct UserType [ (Label, Expr) ]
-- The order of these need NOT match the declaration

| ECase (Case Expr)

| Ap0 Op0
Expand Down
8 changes: 7 additions & 1 deletion daedalus-vm/src/Daedalus/VM/Compile/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ compileE expr k =
b

Src.Struct t fs ->
let labs = case Src.tnameFlav (Src.utName t) of
Src.TFlavStruct ls -> ls
_ -> panic "compileE" ["Not TStructFlav"]
in
compileEs (map snd fs) \vs ->
do s <- stmt ty (\x -> CallPrim x (StructCon t) vs)
do let mp = Map.fromList (zip (map fst fs) vs)
args = [ mp Map.! l | l <- labs ]
s <- stmt ty (\x -> CallPrim x (StructCon t) args)
continue k s

Src.ECase (Src.Case x as) -> compileCase (Src.typeOf expr) x as k
Expand Down
2 changes: 1 addition & 1 deletion src/Daedalus/DDL2Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1671,7 +1671,7 @@ newTNameRec rec =
doOne r d =
let bd = isJust (TC.tctyBD d)
flavor = case TC.tctyDef d of
TC.TCTyStruct {} -> TFlavStruct
TC.TCTyStruct _ fs -> TFlavStruct (map fst fs)
TC.TCTyUnion cs
| all (\(_, (t, _)) -> t == TC.tUnit) cs -> TFlavEnum (map fst cs)
| otherwise -> TFlavUnion (map fst cs)
Expand Down
2 changes: 1 addition & 1 deletion talos/src/Talos/Analysis.hs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ caseIsTotal (Case e alts)
hasDefault = any ((==) PAny . fst) alts

nLabels ut = case tnameFlav (utName ut) of
TFlavStruct -> panic "Unexpected struct" []
TFlavStruct {} -> panic "Unexpected struct" []
TFlavUnion ls -> length ls
TFlavEnum ls -> length ls

Expand Down
9 changes: 9 additions & 0 deletions tests/T077.ddl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def P =
block
a = 'x'
b = Many UInt8

def Main : P =
block
b = "bb"
a = 'a'
28 changes: 28 additions & 0 deletions tests/T077.ddl.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module T077

--- Imports:

--- Type defs:
type T077.P = { a: uint 8
; b: [uint 8]
}

--- Rules:

T077.P : Grammar T077.P =
do (a : uint 8) <- pure 'x'
(b : [uint 8]) <- Many[] Match UInt8
pure {a = a,
b = b}

T077.Main : Grammar T077.P =
do (b : [uint 8]) <- pure "bb"
(a : uint 8) <- pure 'a'
pure {b = b,
a = a}

T077.P_ : Grammar {} =
@Many[] @Match UInt8

T077.Main_ : Grammar {} =
pure {}
3 changes: 3 additions & 0 deletions tests/T077.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dump
--vm
T077.ddl
16 changes: 16 additions & 0 deletions tests/T077.test.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module DaedalusMain where


type P =
struct
a : Word 8
b : [Word 8]

.function Main
.root
.entry L_0_Main9
L_0_Main9(ra0o : Input): /* normal block */
r0 = "bb"()
r1 = newStruct @P(97 @(Word 8), r0)
popDebug
return(r1, ra0o)

0 comments on commit dd392a5

Please sign in to comment.