Pretty printing of the limited stuff that's there.

This commit is contained in:
2016-06-09 19:02:14 -07:00
parent e5bb88aa4e
commit 89a7df58e5
3 changed files with 62 additions and 14 deletions

View File

@@ -1,8 +1,9 @@
module Bang.Syntax.AST
where
import Data.Text.Lazy(Text)
import Data.Text.Lazy(Text, unpack)
import Bang.Syntax.Location
import Text.PrettyPrint.Annotated
data NameEnvironment = ModuleEnv | TypeEnv | VarEnv
deriving (Eq, Ord, Show)
@@ -10,26 +11,67 @@ data NameEnvironment = ModuleEnv | TypeEnv | VarEnv
data Name = Name Location NameEnvironment Word Text
deriving (Show)
ppName :: Name -> Doc a
ppName (Name _ _ _ t) = text' t
data Module = Module Name [Declaration]
deriving (Show)
data Declaration = TypeDeclaration !Name !Type
| ValueDeclaration !Name !Expression
| PrimTypeDecl !PrimitiveType
ppModule :: Module -> Doc a
ppModule (Module name decls) =
vcat ([text "module" <> space <> ppName name, text ""] ++
map ppDeclaration decls)
data Declaration = TypeDeclaration Name Type
| ValueDeclaration Name Expression
| PrimTypeDeclaration Name Text
deriving (Show)
data PrimitiveType = PrimType Name Text
deriving (Show)
ppDeclaration :: Declaration -> Doc a
ppDeclaration d =
case d of
TypeDeclaration n t ->
ppName n <> space <> text "::" <> space <> ppType t
ValueDeclaration n e ->
ppName n <> space <> text "=" <> space <> ppExpression e
PrimTypeDeclaration n t ->
text "primitive" <> space <> text "type" <> space <>
ppName n <> space <> text "=" <> space <> text' t
data Expression = ConstantExp Location ConstantVal
data Expression = ConstantExp Location ConstantValue
| ReferenceExp Location Name
deriving (Show)
data ConstantVal = ConstantInt Word Text
| ConstantChar Text
| ConstantString Text
| ConstantFloat Text
ppExpression :: Expression -> Doc a
ppExpression e =
case e of
ConstantExp _ v -> ppConstantValue v
ReferenceExp _ n -> ppName n
data ConstantValue = ConstantInt Word Text
| ConstantChar Text
| ConstantString Text
| ConstantFloat Text
deriving (Show)
ppConstantValue :: ConstantValue -> Doc a
ppConstantValue cv =
case cv of
ConstantInt 2 t -> text "0b" <> text' t
ConstantInt 8 t -> text "0o" <> text' t
ConstantInt 10 t -> text' t
ConstantInt 16 t -> text "0x" <> text' t
ConstantChar c -> text' c
ConstantString s -> text' s
ConstantFloat f -> text' f
data Type = TypeRef Location Name
deriving (Show)
ppType :: Type -> Doc a
ppType t =
case t of
TypeRef _ n -> ppName n
text' :: Text -> Doc a
text' = text . unpack

View File

@@ -143,7 +143,7 @@ declaration :: { Maybe Declaration }
do let Located src (TypeIdent rawName) = $3
Located _ (StringTok rawText) = $5
name <- registerName False src TypeEnv rawName
return (Just (PrimTypeDecl (PrimType name rawText))) }
return (Just (PrimTypeDeclaration name rawText)) }
Type :: { Type }
: TypeIdent {%