Refactoring into Syntax Parser, also float parsing

This commit is contained in:
vegowotenks 2025-01-20 19:53:28 +01:00
parent 212eecfdf7
commit 52e04d28cf
18 changed files with 287 additions and 176 deletions

View file

@ -0,0 +1,45 @@
module Ubc.Parse.Syntax.Function
( Function(..)
, parseFunction
)
where
import Control.Monad ((<$!>))
import Text.Parsec (lookAhead, try, ParsecT)
import Ubc.Parse.Syntax.VariableType (VariableType)
import Ubc.Parse.Syntax.Expression (Expression, expressionParser)
import qualified Ubc.Parse.Syntax.Language as UbcLanguage
import qualified Ubc.Parse.Syntax.VariableType as VariableType
data Function = Function
{ identifier :: String
, returnType :: VariableType
, body :: Expression
, arguments :: [(VariableType, String)]
}
deriving (Show)
parseFunction :: Monad m => ParsecT String u m Function
parseFunction = do
(resultType, name) <- try $ do
resultType <- UbcLanguage.typeName
name <- UbcLanguage.identifier
_ <- lookAhead $ UbcLanguage.symbol "("
return (VariableType.fromString resultType, name)
argumentList <- UbcLanguage.parens (UbcLanguage.commaSeparated argumentDefinition)
expressionBody <- expressionParser
return $ Function name resultType expressionBody argumentList
argumentDefinition :: Monad m => ParsecT String u m (VariableType, String)
argumentDefinition = do
argumentType <- VariableType.fromString <$!> UbcLanguage.typeName
argumentName <- UbcLanguage.identifier
return (argumentType, argumentName)