diff options
Diffstat (limited to 'ast/CC')
-rw-r--r-- | ast/CC/Source.hs | 48 | ||||
-rw-r--r-- | ast/CC/Typed.hs | 35 |
2 files changed, 83 insertions, 0 deletions
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 |