add sugar for if/then/else

This commit is contained in:
2024-11-16 15:49:27 -08:00
parent b185065fb0
commit 454dccaa72
7 changed files with 31 additions and 3 deletions

View File

@@ -860,6 +860,9 @@ undo ((DoLet fc nm tm) :: xs) = RLet fc nm (RImplicit fc) tm <$> undo xs
undo ((DoArrow fc nm tm) :: xs) = pure $ RApp fc (RApp fc (RVar fc "_>>=_") tm Explicit) (RLam fc nm Explicit !(undo xs)) Explicit
check ctx tm ty = case (tm, !(forceType ctx.env ty)) of
(RIf fc a b c, ty) =>
let tm' = RCase fc a [ MkAlt (RVar (getFC b) "True") b, MkAlt (RVar (getFC c) "False") c ] in
check ctx tm' ty
(RDo fc stmts, ty) => check ctx !(undo stmts) ty
(RCase fc rsc alts, ty) => do
(sc, scty) <- infer ctx rsc

View File

@@ -240,6 +240,16 @@ doStmt
doExpr : Parser Raw
doExpr = RDo <$> getPos <* keyword "do" <*> (startBlock $ someSame doStmt)
ifThenElse : Parser Raw
ifThenElse = do
fc <- getPos
keyword "if"
a <- term
keyword "then"
b <- term
keyword "else"
c <- term
pure $ RIf fc a b c
-- This hits an idris codegen bug if parseOp is last and Lazy
term = caseExpr
@@ -247,6 +257,7 @@ term = caseExpr
<|> lamExpr
<|> doExpr
<|> parseOp
<|> ifThenElse
varname : Parser String
varname = (ident <|> uident <|> keyword "_" *> pure "_")

View File

@@ -78,6 +78,7 @@ data Raw : Type where
RImplicit : (fc : FC) -> Raw
RHole : (fc : FC) -> Raw
RDo : (fc : FC) -> List DoStmt -> Raw
RIf : (fc : FC) -> Raw -> Raw -> Raw -> Raw
%name Raw tm
@@ -96,6 +97,8 @@ HasFC Raw where
getFC (RImplicit fc) = fc
getFC (RHole fc) = fc
getFC (RDo fc stmts) = fc
getFC (RIf fc _ _ _) = fc
-- derive some stuff - I'd like json, eq, show, ...
@@ -189,6 +192,7 @@ Show Raw where
show (RCase _ x xs) = foo [ "Case", show x, show xs]
show (RDo _ stmts) = foo [ "DO", "FIXME"]
show (RU _) = "U"
show (RIf _ x y z) = foo [ "If", show x, show y, show z]
export
Pretty Literal where
@@ -240,6 +244,7 @@ Pretty Raw where
asDoc p (RImplicit _) = text "_"
asDoc p (RHole _) = text "?"
asDoc p (RDo _ stmts) = text "TODO - RDo"
asDoc p (RIf _ x y z) = par p 0 $ text "if" <+> asDoc 0 x <+/> "then" <+> asDoc 0 y <+/> "else" <+> asDoc 0 z
export
Pretty Module where

View File

@@ -9,6 +9,7 @@ keywords : List String
keywords = ["let", "in", "where", "case", "of", "data", "U", "do",
"ptype", "pfunc", "module", "infixl", "infixr", "infix",
"", "forall",
"if", "then", "else",
"->", "", ":", "=>", ":=", "=", "<-", "\\", "_"]
checkKW : String -> Token Kind