Skip to content
Snippets Groups Projects
Commit e75f3acc authored by ik1g19's avatar ik1g19
Browse files

almost finished challenge 4

parent fd638c3c
No related branches found
No related tags found
No related merge requests found
...@@ -278,10 +278,11 @@ prettyPrint (LamDef ms e) = exprBrackets e ...@@ -278,10 +278,11 @@ prettyPrint (LamDef ms e) = exprBrackets e
--applies brackets to expr if needed --applies brackets to expr if needed
exprBrackets :: LamExpr -> String exprBrackets :: LamExpr -> String
exprBrackets e | fst (head (parse expr str)) == e = str --omit brackets exprBrackets e | foldl1 (||) $ map (e==) parsed = str --omit brackets
| otherwise = "(" ++ str ++ ")" --include brackets | otherwise = "(" ++ str ++ ")" --include brackets
where where
str = exprToStr e str = exprToStr e
parsed = map fst (parse expr str) --possible parsings of str
--converts expr to string --converts expr to string
...@@ -317,11 +318,25 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV ...@@ -317,11 +318,25 @@ ex3'4 = LamDef [ ("F", LamAbs 1 (LamVar 1) ) ] (LamAbs 2 (LamApp (LamAbs 1 (LamV
--Digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9” --Digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”
--MacroExpr ::= "def" MacroName "=" Expr "in" MacroExpr | Expr
--Expr ::= Var | MacroName | Applicative | Function | Expression
--MacroName ::= UChar | UChar MacroName
--UChar ::= "A" | "B" | ... | "Z"
--Var ::= “x” Digits
--Digits ::= Digit | Digit Digits
--Digit ::= “0” | “1” | “2” | “3” | “4” | “5” | “6” | “7” | “8” | “9”
--Applicative ::= Expr Expr
--Function ::= “\” Var “->” Expr
--Expression ::= “(“ Expr “)”
parseLamMacro :: String -> Maybe LamMacroExpr parseLamMacro :: String -> Maybe LamMacroExpr
parseLamMacro str | parsed == [] = Nothing parseLamMacro str | parsed == [] = Nothing
| otherwise = Just parsed | otherwise = Just $ fstHead parsed
where where
parsed = fst (head (parse (macroExpr []) str)) --HEAD WILL NOT WORK parsed = parse (macroExpr []) str
fstHead = fst . head
macroExpr :: [ (String,LamExpr) ] -> Parser LamMacroExpr macroExpr :: [ (String,LamExpr) ] -> Parser LamMacroExpr
...@@ -337,36 +352,16 @@ macroExpr ms = do string "def" ...@@ -337,36 +352,16 @@ macroExpr ms = do string "def"
return $ LamDef ms e return $ LamDef ms e
-- macroExpr :: Parse LamMacroExpr
-- macroExpr = do string "def"
-- name <- token macroName
-- symbol "="
-- e <- token expr
-- token $ string "in"
-- macros <- macroLoop
-- return $ LamDef macros
-- where
-- macroLoop :: Parse [(String,LamExpr)]
-- macroLoop = do string "def"
-- name <- token macroName
-- symbol "="
-- e <- token expr
-- token $ string "in"
-- ms <- many macroExpr
-- return ((name,e):ms) <|>
-- do {e <- token expr;return []}
expr :: Parser LamExpr expr :: Parser LamExpr
expr = do {x <- var; return $ LamVar x} expr = do terms <- some (token term)
return $ foldl1 LamApp terms
<|> do {name <- macroName;return $ LamMacro name}
<|> do e1 <- expr term :: Parser LamExpr
space term = do char '('
e2 <- expr e <- token expr
return $ LamApp e1 e2 char ')'
return e
<|> do char '\\' <|> do char '\\'
x <- var x <- var
...@@ -374,10 +369,30 @@ expr = do {x <- var; return $ LamVar x} ...@@ -374,10 +369,30 @@ expr = do {x <- var; return $ LamVar x}
e <- expr e <- expr
return $ LamAbs x e return $ LamAbs x e
<|> do char '(' <|> do {x <- var; return $ LamVar x}
e <- token expr
char ')' <|> do {name <- macroName;return $ LamMacro name}
return e
-- expr :: Parser LamExpr
-- expr = do e1 <- expr
-- space
-- e2 <- expr
-- return $ LamApp e1 e2
-- <|> do {x <- var; return $ LamVar x}
-- <|> do {name <- macroName;return $ LamMacro name}
-- <|> do char '\\'
-- x <- var
-- symbol "->"
-- e <- expr
-- return $ LamAbs x e
-- <|> do char '('
-- e <- token expr
-- char ')'
-- return e
macroName :: Parser String macroName :: Parser String
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment