Skip to content

Commit

Permalink
add: higher order functions without typedef
Browse files Browse the repository at this point in the history
  • Loading branch information
oriollinan committed Jan 9, 2025
1 parent 570c7cb commit e29d5d4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/Ast/Parser/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ arrayType = do
-- TODO: find a way to do it without the parenthesis and avoid the infinite loop of parseType
functionType :: PU.Parser AT.Type
functionType = do
paramTypes <- M.some $ PU.lexeme parseTermType
paramTypes <- M.some $ PU.lexeme (M.between (PU.symbol "(") (PU.symbol ")") functionType M.<|> parseTermType)
returnType <- PU.symbol "->" *> PU.lexeme parseTermType
return $ AT.TFunction {AT.returnType = returnType, AT.paramTypes = paramTypes, AT.isVariadic = False}

Expand Down
7 changes: 3 additions & 4 deletions test/Ast/Parser/ExprSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -384,11 +384,10 @@ spec = do
(AT.Lit normalizeLoc $ AT.LInt 1)
result `shouldBe` expected

it "parses a function that takes a function" $ do
let input = "map: Func int -> char = f x { f(x) }"
it "parses a higher order function" $ do
let input = "map: (int -> char) int -> char = f x { f(x) }"
let functionType = AT.TFunction AT.TChar [AT.TInt 32] False
let env = E.insertType "Func" functionType initialEnv
let result = normalizeExpr <$> fst (S.runState (M.runParserT PE.parseExpr "" input) env)
let result = normalizeExpr <$> parseWithEnv input
let expected =
Right $
AT.Function
Expand Down

0 comments on commit e29d5d4

Please sign in to comment.