aboutsummaryrefslogtreecommitdiff
path: root/src/Ast.hs
blob: 075b9f85cd1cfeedee88ac6d5a41c2886cd25d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Ast where

import Data.List (intercalate)
import Text.Parsec (SourcePos)

type Ident = String

data Type = Type String | FuncType [Type] (Maybe Type) deriving (Eq, Ord)

instance Show Type where
  show (Type t) = t
  show (FuncType params rtyp) =
    "(" ++ (intercalate ", " (fmap show params)) ++ ") -> " ++ case rtyp of
      Just t -> show t
      Nothing -> "()"

showList :: [Type] -> String
showList xs = intercalate ", " $ fmap show xs

type FuncParam = (Ident, Type, Bool, SourcePos)

data Expr
  = Num Integer SourcePos
  | Bool' Bool SourcePos
  | BinOp Op Expr Expr
  | Variable Ident SourcePos
  | -- v type value private pos
    Var Ident Type Expr Bool SourcePos
  | -- fn [params] return body private anomyous pos
    Func Ident [FuncParam] (Maybe Type) [Expr] Bool Bool SourcePos
  | Call Expr [Expr] SourcePos
  | Return (Maybe Expr) SourcePos
  | Module String SourcePos
  deriving (Eq, Ord, Show)

data Op
  = Plus
  | Minus
  | Mul
  | Div
  deriving (Eq, Ord, Show)

toFuncType :: [FuncParam] -> Maybe Type -> Type
toFuncType params rtyp =
  FuncType (map (\(_, t, _, _) -> t) params) rtyp