From: Mart Lubbers Date: Wed, 27 Mar 2019 07:49:02 +0000 (+0100) Subject: Merge branch 'master' of git.martlubbers.net:minfp X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=1eafed23b26438ab8b7244e2545ad5678d768d57;p=minfp.git Merge branch 'master' of git.martlubbers.net:minfp --- 1eafed23b26438ab8b7244e2545ad5678d768d57 diff --cc parse.icl index 50539f8,da55517..b812ef9 --- a/parse.icl +++ b/parse.icl @@@ -15,15 -14,11 +15,13 @@@ import StdEn import ast +cons x xs = [x:xs] + (<:>) infixl 0 :: a (m [a]) -> m [a] | Functor m -(<:>) l r = (\xs->[l:xs]) <$> r +(<:>) l r = cons l <$> r - :: Token - = TTEq | TTSemiColon | TTLambda | TTDot | TTBrackOpen | TTBrackClose - | TTComma | TTBool Bool | TTInt Int | TTOp [Char] | TTIdent [Char] - | TTDColon | TTPipe + :: Token = TTSemiColon | TTBrackOpen | TTBrackClose | TTBool Bool | TTInt Int + | TTOp [Char] | TTIdent [Char] derive gEq Token derive gPrint Token @@@ -94,36 -83,12 +86,30 @@@ pChainl op p = foldl (flip ($)) <$> p < pChainr :: (Parser (a a -> a)) (Parser a) -> Parser a pChainr op p = flip ($) <$> p <*> (flip <$> op <*> pChainr op p) <|> p -parse :: [Token] -> Either [String] [Function] +parse :: [Token] -> Either [String] [Either TypeDef Function] parse ts = fst <$> runStateT (reverse <$> pAST <* pEof) {tokens=ts, ifxs=[]} where - pAST :: Parser [Function] - pAST = many pFunction >>= mapM \(id, args, body)->Function id args <$ - modify (\t->{t & tokens=body}) <*> pExpression <* pEof + pAST :: Parser [Either TypeDef Function] + pAST = many (Right <$> pFunction <|> Left <$> pTypeDef) + >>= mapM (either (pure o Left) \(id, args, body)->Right o + Function id args <$ modify (\t->{t & tokens=body}) <*> pExpression <* pEof) + + pTypeDef :: Parser TypeDef + pTypeDef = TypeDef - <$ pToken TTDColon ++ <$ pToken (TTOp ['::']) + <*> pId + <*> many pId - <* pToken TTEq - <*> (cons <$> pCons <*> many (pToken TTPipe *> pCons)) ++ <* pToken (TTOp ['=']) ++ <*> (cons <$> pCons <*> many (pToken (TTOp ['|']) *> pCons)) + <* pToken TTSemiColon + + pCons = tuple <$> pId <*> many pType + + pType + = TInt <$ pTop ? (\t->t=:(TTIdent ['Int'])) + <|> TBool <$ pTop ? (\t->t=:(TTIdent ['Bool'])) + <|> TVar <$> pId - <|> - :: Type - = TVar [Char] - | TTuple Type Type - | TInt - | TBool - | (-->) infixr 9 Type Type ++// <|> pId = (\(TTIdent i)->i) <$> pTop ? (\t->t=:(TTIdent _)) pOp = (\(TTOp i)->i) <$> pTop ? (\t->t=:(TTOp _)) @@@ -131,10 -97,10 +118,10 @@@ pFunction :: Parser ([Char], [[Char]], [Token]) pFunction - = (\x y z->(x, y, z)) + = tuple3 <$> (pFunId <|> pId) <*> many pId - <* pToken TTEq + <* pToken (TTOp ['=']) <*> many (pTop ? ((=!=)TTSemiColon)) <* pToken TTSemiColon