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 ++++++++++++++++++++++++++++++++++++++++++++++++ ast/CC/Typed.hs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 ast/CC/Source.hs create mode 100644 ast/CC/Typed.hs (limited to 'ast') 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 diff --git a/ast/CC/Typed.hs b/ast/CC/Typed.hs new file mode 100644 index 0000000..b11067f --- /dev/null +++ b/ast/CC/Typed.hs @@ -0,0 +1,35 @@ +module CC.Typed( + module CC.Typed, + module CC.Types +) where + +import CC.Types + + +data ProgramT = ProgramT [DeclT] + deriving (Show, Read) + +data DeclT = DefT DefT -- import? + deriving (Show, Read) + +data DefT = FunctionT TypeT Name [Name] ExprT + deriving (Show, Read) + +data TypeT = TFunT TypeT TypeT + | TIntT + | TyVar Int + deriving (Show, Read) + +data ExprT = CallT TypeT ExprT ExprT + | IntT Int + | VarT Occ + deriving (Show, Read) + +-- | Occurrence of a variable +data Occ = Occ Name TypeT + deriving (Show, Read) + +exprType :: ExprT -> TypeT +exprType (CallT typ _ _) = typ +exprType (IntT _) = TIntT +exprType (VarT (Occ _ typ)) = typ -- cgit v1.2.3-70-g09d2