Skip to content

Commit

Permalink
Ponder compilation a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanzab committed Sep 9, 2022
1 parent 457f25f commit e129a83
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions experiments/idris/src/Playground/OpenType/InductiveRecursive.idr
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,93 @@ namespace FormatOf
simple_glyph = FormatOf.do
flag <- flag
pure (flag.repeat + 1)


-- Thinking about compilation

namespace Rust

data RType : Type where
Var : String -> RType
U8 : RType
U16 : RType
U32 : RType
U64 : RType
I8 : RType
I16 : RType
I32 : RType
I64 : RType
Never : RType
Tuple : List RType -> RType
Vec : RType -> RType

data Item : Type where
Struct : List (String, RType) -> Item
Enum : List (String, RType) -> Item
DecodeFn : () -> Item
EncodeFn : () -> Item

record Module where
constructor MkModule
items : List (String, Item)


namespace Compile

-- TODO: Cache compilations of definitions
-- eg. of structs, enums, endocers and decoders


compileFormat : Format -> (Rust.Module -> Maybe Rust.Module)
compileFormat f =
-- compile rep
-- compile decode
-- compile encode
?todo_compileFormat


compileRep : (f : Format) -> Maybe Rust.RType
compileRep End = Just (Rust.Tuple [])
compileRep Fail = Just (Rust.Never)
compileRep (Ignore _ _) = Just (Rust.Tuple [])
compileRep (Repeat _ f) =
Just (Rust.Vec !(compileRep f)) -- TODO: Compile to a contract? Or maybe a
-- fixed size array if the length is known
-- or just throw away the info
compileRep (Pure x) =
?todo_compileSingRep -- TODO: interpret an Idris type as a Rust type??
-- perhaps we need to restrict this?
compileRep (Bind f1 f2) =
Just (Tuple
[ !(compileRep f1)
, !(compileRep (f2 ?todo_compileBind_x)) -- TODO: how to bind the output?
-- enum based on the values of `x : Rep f1`?
-- depends on how `x` is used inside `f2`
])
compileRep (Custom f) =
-- TODO: f.RustRep
Nothing


compileDecode : Format -> (Rust.Module -> Maybe Rust.Module)
compileDecode End = ?todo_compileDecodeEnd
compileDecode Fail = ?todo_compileDecodeFail
compileDecode (Pure x) = ?todo_compileDecodePure
compileDecode (Ignore f _) = ?todo_compileDecodeIgnore
compileDecode (Repeat len f) = ?todo_compileDecodeRepeat
compileDecode (Bind f1 f2) = ?todo_compileDecodeBind
compileDecode (Custom f) =
-- TODO: f.rustDecode
?todo_compileDecodeCustom


compileEncode : Format -> (Rust.Module -> Maybe Rust.Module)
compileEncode End = ?todo_compileEncodeEnd
compileEncode Fail = ?todo_compileEncodeFail
compileEncode (Pure x) = ?todo_compileEncodePure
compileEncode (Ignore f def) = ?todo_compileEncodeIgnore
compileEncode (Repeat len f) = ?todo_compileEncodeRepeat
compileEncode (Bind f1 f2) = ?todo_compileEncodeBind
compileEncode (Custom f) =
-- TODO: f.rustEncode
?todo_compileEncodeCustom

0 comments on commit e129a83

Please sign in to comment.