(<:>) infixl 0 :: a (m [a]) -> m [a] | Functor m
(<:>) l r = (\xs->[l:xs]) <$> r
-:: Token
- = TTEq | TTSemiColon | TTLambda | TTDot | TTBrackOpen | TTBrackClose
- | TTComma | TTBool Bool | TTInt Int | TTOp [Char] | TTIdent [Char]
+:: Token = TTSemiColon | TTBrackOpen | TTBrackClose | TTBool Bool | TTInt Int
+ | TTOp [Char] | TTIdent [Char]
derive gEq Token
derive gPrint Token
lex [t:ts]
| isSpace t = lex ts
| isDigit t
- # (i, ts) = span isDigit [t:ts]
- = TTInt (toInt (toString i)) <:> lex ts
+ # (d, ts) = span isDigit [t:ts]
+ = TTInt (toInt (toString d)) <:> lex ts
| isAlpha t
- # (i, ts) = span isAlpha [t:ts]
- = TTIdent i <:> lex ts
+ # (d, ts) = span isAlpha [t:ts]
+ = TTIdent d <:> lex ts
| isOp t
- # (i, ts) = span isOp [t:ts]
- | i =: ['='] = TTEq <:> lex ts
- | i =: [','] = TTComma <:> lex ts
- | i =: ['.'] = TTDot <:> lex ts
- | i =: ['\\'] = TTLambda <:> lex ts
- = TTOp i <:> lex ts
+ # (d, ts) = span isOp [t:ts]
+ = TTOp d <:> lex ts
= Left ["Unexpected: " +++ toString t +++ " ord: " +++ toString (toInt t)]
where
isOp c = isMember c ['!@#$%^&*=+/?-_|\\\'",<>.:']
pId = (\(TTIdent i)->i) <$> pTop ? (\t->t=:(TTIdent _))
pOp = (\(TTOp i)->i) <$> pTop ? (\t->t=:(TTOp _))
+ pInt = (\(TTInt i)->i) <$> pTop ? (\t->t=:(TTInt _))
pBrack p = pToken TTBrackOpen *> p <* pToken TTBrackClose
pFunction :: Parser ([Char], [[Char]], [Token])
= (\x y z->(x, y, z))
<$> (pFunId <|> pId)
<*> many pId
- <* pToken TTEq
+ <* pToken (TTOp ['='])
<*> many (pTop ? ((=!=)TTSemiColon))
<* pToken TTSemiColon
pFunId :: Parser [Char]
pFunId = pOp
>>= \i->pChainr <$ pId ? ((==) ['ifxr']) <|> pChainl <$ pId ? ((==)['ifxl'])
- >>= \p->(\(TTInt i)->i) <$> pTop ? (\t->t=:(TTInt _))
+ >>= \p->pInt
>>= \s->modify (\t->{t & ifxs=[(p (App o App (Var i) <$ pOp ? ((==)i)), s):t.ifxs]})
>>| pure i
pExpression = getState >>= \{ifxs}->flip (foldr ($))
(map fst $ sortBy (on (<) snd) ifxs)
$ pChainl (pure App)
- $ Lambda <$ pToken TTLambda <*> pId <* pToken TTDot <*> pExpression
- <|> pBrack ( Tuple <$> pExpression <* pToken TTComma <*> pExpression
+ $ Lambda <$ pToken (TTOp ['\\']) <*> pId <* pToken (TTOp ['.']) <*> pExpression
+ <|> pBrack ( Tuple <$> pExpression <* pToken (TTOp [',']) <*> pExpression
<|> Var <$> pOp
<|> pExpression)
- <|> (\(TTInt i)->Lit (Int i)) <$> pTop ? (\t->t=:(TTInt _))
+ <|> Lit o Int <$> pInt
<|> (\(TTBool i)->Lit (Bool i)) <$> pTop ? (\t->t=:(TTBool _))
- <|> (\x->Var ['_':x]) <$ pId ? ((==)['code']) <*> pId
+ <|> (\x->Var ['_':x]) <$ pToken (TTIdent ['code']) <*> pId
<|> Var <$> pId