Building the AST
Parser Structure
parse :: String -> String -> IO (Either String AT.Program)
parse sourceFile input = do
(result, _) <- S.runStateT (M.runParserT (PP.parseProgram sourceFile) sourceFile input) PS.parserState
case result of
(Left err) -> return $ Left (M.errorBundlePretty err)
(Right program) -> return $ Right programAST Node Types
data SrcLoc = SrcLoc { srcFile :: String, srcLine :: Int, srcCol :: Int }data Literal = LInt Integer | LFloat Double | LDouble Double | LChar Char | LBool Bool | LArray [Literal] | LNull | LStruct [(String, Literal)]data Type = TInt Int | TFloat | TDouble | TChar | TBoolean | TVoid | TMutable Type | TPointer Type | TArray Type (Maybe Int) | TFunction { returnType :: Type, paramTypes :: [Type], isVariadic :: Bool } | TStruct { structName :: String, fields :: [(String, Type)] } | TUnion { unionName :: String, variants :: [(String, Type)] } | TTypedef String Type | TUnknowndata Expr = Lit SrcLoc Literal | Var SrcLoc String Type | Function { funcLoc :: SrcLoc, funcName :: String, funcType :: Type, funcParams :: [String], funcBody :: Expr } | ForeignFunction { funcLoc :: SrcLoc, funcName :: String, funcType :: Type } | Declaration { declLoc :: SrcLoc, declName :: String, declType :: Type, declInit :: Maybe Expr } | Assignment { assignLoc :: SrcLoc, assignTarget :: Expr, assignValue :: Expr } | Call { callLoc :: SrcLoc, callFunc :: Expr, callArgs :: [Expr] } | If { ifLoc :: SrcLoc, ifCond :: Expr, ifThen :: Expr, ifElse :: Maybe Expr } | While { whileLoc :: SrcLoc, whileCond :: Expr, whileBody :: Expr } | From { fromLoc :: SrcLoc, fromStart :: Expr, fromEnd :: Expr, fromStep :: Expr, fromVar :: Expr, fromBody :: Expr } | Block [Expr] | Return SrcLoc (Maybe Expr) | Break SrcLoc | Continue SrcLoc | Op SrcLoc Operation Expr Expr | UnaryOp SrcLoc UnaryOperation Expr | StructAccess SrcLoc Expr Expr | ArrayAccess SrcLoc Expr Expr | Cast SrcLoc Type Expr | Assembly { asmLoc :: SrcLoc, asmExpr :: AsmExpr }
Parsing Process
AST Traversal and Manipulation
Last updated