packages feed

spade-0.1.0.0: src/Compiler/AST/Program.hs

module Compiler.AST.Program
  ( module Compiler.AST.Expression
  , module Compiler.AST.Program
  , module Compiler.AST.FunctionDef
  )
where

import Control.Applicative
import Data.Text as T

import Common
import Compiler.AST.Common
import Compiler.AST.Expression
import Compiler.AST.FunctionDef
import Compiler.AST.FunctionStatement
import Compiler.AST.Parser.Common
import Compiler.Lexer.Comments
import Test.Common

type StringType = Text

data ProgramStatement
  = FunctionDefStatement FunctionDef
  | NakedStatement FunctionStatementWithLoc
  | TopLevelComment Comment
  deriving (Show, Eq)

type Program = [ProgramStatement]

instance ToSource ProgramStatement where
  toSource (FunctionDefStatement def) = toSource def
  toSource (NakedStatement fns)       = toSource fns
  toSource (TopLevelComment cmm)      = toSource cmm

instance HasAstParser ProgramStatement where
  astParser
    =   (FunctionDefStatement <$> astParser)
    <|> (NakedStatement <$> (astParser @FunctionStatementWithLoc))
    <|> (TopLevelComment <$> parseComment)

instance HasGen ProgramStatement where
  getGen = choice
    [ FunctionDefStatement <$> getGen
    , NakedStatement <$> getGen
    ]

instance HasAstParser Program where
  astParser = many (astParser @ProgramStatement)