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 module Bang.Syntax.AST
where where
import Data.Text.Lazy(Text) import Data.Text.Lazy(Text, unpack)
import Bang.Syntax.Location import Bang.Syntax.Location
import Text.PrettyPrint.Annotated
data NameEnvironment = ModuleEnv | TypeEnv | VarEnv data NameEnvironment = ModuleEnv | TypeEnv | VarEnv
deriving (Eq, Ord, Show) deriving (Eq, Ord, Show)
@@ -10,26 +11,67 @@ data NameEnvironment = ModuleEnv | TypeEnv | VarEnv
data Name = Name Location NameEnvironment Word Text data Name = Name Location NameEnvironment Word Text
deriving (Show) deriving (Show)
ppName :: Name -> Doc a
ppName (Name _ _ _ t) = text' t
data Module = Module Name [Declaration] data Module = Module Name [Declaration]
deriving (Show) deriving (Show)
data Declaration = TypeDeclaration !Name !Type ppModule :: Module -> Doc a
| ValueDeclaration !Name !Expression ppModule (Module name decls) =
| PrimTypeDecl !PrimitiveType vcat ([text "module" <> space <> ppName name, text ""] ++
map ppDeclaration decls)
data Declaration = TypeDeclaration Name Type
| ValueDeclaration Name Expression
| PrimTypeDeclaration Name Text
deriving (Show) deriving (Show)
data PrimitiveType = PrimType Name Text ppDeclaration :: Declaration -> Doc a
deriving (Show) 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 | ReferenceExp Location Name
deriving (Show) deriving (Show)
data ConstantVal = ConstantInt Word 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 | ConstantChar Text
| ConstantString Text | ConstantString Text
| ConstantFloat Text | ConstantFloat Text
deriving (Show) 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 data Type = TypeRef Location Name
deriving (Show) 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 do let Located src (TypeIdent rawName) = $3
Located _ (StringTok rawText) = $5 Located _ (StringTok rawText) = $5
name <- registerName False src TypeEnv rawName name <- registerName False src TypeEnv rawName
return (Just (PrimTypeDecl (PrimType name rawText))) } return (Just (PrimTypeDeclaration name rawText)) }
Type :: { Type } Type :: { Type }
: TypeIdent {% : TypeIdent {%

View File

@@ -1,4 +1,5 @@
import Bang.CommandLine import Bang.CommandLine
import Bang.Syntax.AST(ppModule)
import Bang.Syntax.Lexer import Bang.Syntax.Lexer
import Bang.Syntax.Location import Bang.Syntax.Location
import Bang.Syntax.Parser import Bang.Syntax.Parser
@@ -8,6 +9,7 @@ import qualified Data.Text.Lazy.IO as T
import Data.Version(showVersion) import Data.Version(showVersion)
import Paths_bang(version) import Paths_bang(version)
import System.IO.Error(isDoesNotExistError) import System.IO.Error(isDoesNotExistError)
import Text.PrettyPrint.Annotated(render)
main :: IO () main :: IO ()
main = getCommand >>= \ cmd -> main = getCommand >>= \ cmd ->
@@ -34,5 +36,9 @@ runParser _cmd opts =
case mtxt of case mtxt of
Left _ -> fail ("Unable to open file: " ++ path) Left _ -> fail ("Unable to open file: " ++ path)
Right txt -> Right txt ->
do let mod = parseModule (File path) txt do let res = parseModule (File path) txt
putStrLn (show mod) case res of
Left err ->
putStrLn (show err)
Right mod ->
putStrLn (render (ppModule mod))