From bc52411ae2ed26cab1d5086ae6df68f23ebbd052 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Wed, 10 Jun 2020 19:59:03 +0200 Subject: Initial state I found the code in --- ast/CC/Source.hs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 ast/CC/Source.hs (limited to 'ast/CC/Source.hs') diff --git a/ast/CC/Source.hs b/ast/CC/Source.hs new file mode 100644 index 0000000..80d8f01 --- /dev/null +++ b/ast/CC/Source.hs @@ -0,0 +1,48 @@ +module CC.Source(module CC.Source, module CC.Types) where + +import CC.Pretty +import CC.Types + + +data Program = Program [Decl] + deriving (Show, Read) + +data Decl = Def Def -- import? + deriving (Show, Read) + +data Def = Function (Maybe Type) + (Name, SourceRange) + [(Name, SourceRange)] + Expr + deriving (Show, Read) + +data Type = TFun Type Type + | TInt + deriving (Show, Read) + +data Expr = Call SourceRange Expr Expr + | Int SourceRange Int + | Var SourceRange Name + deriving (Show, Read) + +instance Pretty Type where + pretty = unparse + +class Unparse a where + -- Parentheses are required if precedence of unparsed element is + -- greater than the argument. + unparsePrec :: Int -> a -> String + + unparse :: a -> String + unparse = unparsePrec 0 + +instance Unparse Type where + unparsePrec _ TInt = "Int" + unparsePrec p (TFun a b) = + let s = unparsePrec 3 a ++ " -> " ++ unparsePrec 2 b + in if p > 2 then "(" ++ s ++ ")" else s + +instance HasRange Expr where + range (Call sr _ _) = sr + range (Int sr _) = sr + range (Var sr _) = sr -- cgit v1.2.3-54-g00ecf