Simple parse-dump main function, also fixed about everything

This commit is contained in:
vegowotenks 2025-01-25 20:55:26 +01:00
parent f35ca83d7a
commit 01fafec1c0
18 changed files with 179 additions and 214 deletions

View file

@ -1,6 +1,7 @@
module Ubc.Parse.Syntax.Function
( Function(..)
, parseFunction
, parse
, parsePrefixed
)
where
@ -9,7 +10,7 @@ import Control.Monad ((<$!>))
import Text.Parsec (lookAhead, try, ParsecT)
import Ubc.Parse.Syntax.VariableType (VariableType)
import Ubc.Parse.Syntax.Expression (Expression, expressionParser)
import {-# SOURCE #-} Ubc.Parse.Syntax.Expression (Expression, expressionParser)
import qualified Ubc.Parse.Syntax.Language as UbcLanguage
import qualified Ubc.Parse.Syntax.VariableType as VariableType
@ -22,19 +23,23 @@ data Function = Function
}
deriving (Show)
parseFunction :: Monad m => ParsecT String u m Function
parseFunction = do
parsePrefixed :: Monad m => VariableType -> String -> ParsecT String u m Function
parsePrefixed ftype fname = do
argumentList <- UbcLanguage.parens (UbcLanguage.commaSeparated argumentDefinition)
expressionBody <- expressionParser
return $ Function fname ftype expressionBody argumentList
parse :: Monad m => ParsecT String u m Function
parse = 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
parsePrefixed resultType name
argumentDefinition :: Monad m => ParsecT String u m (VariableType, String)
argumentDefinition = do