Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preliminary support for unions added. #63

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Language/Rust/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ data ItemKind
= Function [FunctionAttribute] String [(Mutable, Var, Type)] Type Block
| Static Mutable Var Type Expr
| Struct String [(String, Type)]
| Union String [(String, Type)]
| Extern [ExternItem]
| Use String
| Enum String [Enumerator]
Expand All @@ -102,6 +103,10 @@ instance Pretty ItemKind where
text "struct" <+> text name <+> text "{" $+$
nest 4 (vcat [ text "pub" <+> text field <+> text ":" <+> pPrint ty <> text "," | (field, ty) <- fields ]) $+$
text "}"
pPrint (Union name cases) =
text "union" <+> text name <+> text "{" $+$
nest 4 (vcat [ text "pub" <+> text field <+> text ":" <+> pPrint ty <> text "," | (field, ty) <- cases ]) $+$
text "}"
pPrint (Extern defs) = vcat
( text "extern {"
: map (nest 4 . pPrint) defs
Expand Down Expand Up @@ -147,6 +152,7 @@ data Expr
| Var Var
| Path Path
| StructExpr String [(String, Expr)] (Maybe Expr)
| UnionExpr String (String, Expr)
| Call Expr [Expr]
| MethodCall Expr Var [Expr]
| Lambda [Var] Expr
Expand Down Expand Up @@ -223,6 +229,8 @@ instance Pretty Expr where
: punctuate (text ",") ([ nest 4 (text name <> text ":" <+> pPrint val) | (name, val) <- fields ] ++ maybe [] (\b -> [ text ".." <> pPrint b ]) base)
++ [text "}"]
)
UnionExpr str (name, val) -> sep
[ text str <+> text "{", nest 4 (text name <> text ":" <+> pPrint val), text "}" ]
Call f args -> cat
( pPrintPrec l 13 f <> text "("
: punctuate (text ",") (map (nest 4 . pPrint) args)
Expand Down
Loading