You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
2.9 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. include import "./lang.ml"
  2. include import "./lexer.ml"
  3. open import "prelude.ml"
  4. let parse = lex
  5. let laid_out_block p = between obrace cbrace (sep_end_by_1 semi p)
  6. let rec atom : forall 'm. monad 'm => parser_t 'm expr =
  7. map Ref (try varid)
  8. <|> map Ref (try conid)
  9. <|> map Lit (try integer)
  10. <+> between (try oparen) cparen expr
  11. and fexp : forall 'm. monad 'm => parser_t 'm expr =
  12. let! a = atom
  13. let! ats = many atom
  14. pure (foldl (curry App) a ats)
  15. and expr : forall 'm. monad 'm => parser_t 'm expr =
  16. let lam =
  17. let! _ = back
  18. let! vs = many (try varid)
  19. let! _ = arrow
  20. let! b = expr
  21. pure (foldr ((Lam #) # curry id) b vs)
  22. let case =
  23. let! _ = keyword "case"
  24. let! e = fexp
  25. let! _ = keyword "of"
  26. let! arms =
  27. laid_out_block (
  28. let! c = conid
  29. let! vs = many (try varid)
  30. let! _ = arrow
  31. let! e = expr
  32. pure (c, vs, e)
  33. )
  34. pure (Case (e, arms))
  35. let hslet =
  36. let binding =
  37. let! c = varid
  38. let! vs = many (try varid)
  39. let! _ = equals
  40. let! e = expr
  41. pure (c, foldr ((Lam #) # curry id) e vs)
  42. let! _ = keyword "let"
  43. let! bs = laid_out_block binding
  44. let! _ = keyword "in"
  45. let! b = expr
  46. pure (Let (bs, b))
  47. try lam <|> try case <|> try hslet <+> fexp
  48. let rec ty_atom : forall 'm. monad 'm => parser_t 'm hstype =
  49. map Tyvar (try varid)
  50. <|> map Tycon (try conid)
  51. <+> between (try oparen) cparen ty_tup
  52. and ty_fexp : forall 'm. monad 'm => parser_t 'm hstype =
  53. let! a = ty_atom
  54. let! ats = many ty_atom
  55. pure (foldl (curry Tyapp) a ats)
  56. and ty_exp : forall 'm. monad 'm => parser_t 'm hstype =
  57. chainr1 ty_fexp (map (const (curry Tyarr)) arrow)
  58. and ty_tup : forall 'm. monad 'm => parser_t 'm hstype =
  59. let tytup = function
  60. | [x] -> x
  61. | x -> Tytup x
  62. tytup <$> sep_by comma ty_exp
  63. let datadec : forall 'm. monad 'm => parser_t 'm decl =
  64. let! _ = try (keyword "data")
  65. let datacon =
  66. let! nm = conid
  67. let! args = many ty_atom
  68. pure (Constr (nm, args))
  69. let! nm = conid
  70. let! args = many (try varid)
  71. let! cs = optionally (
  72. let! _ = equals
  73. sep_by_1 pipe (try datacon)
  74. )
  75. pure (Data (nm, args, match cs with | Some cs -> cs | None -> []))
  76. let fdecl : forall 'm. monad 'm => parser_t 'm fdecl =
  77. let! _ = try (keyword "import")
  78. let! cc =
  79. ( (Lua <$ try (keyword "lua"))
  80. <|> (Prim <$ try (keyword "prim"))
  81. )
  82. let! fent = string
  83. let! var = varid
  84. let! _ = dcolon
  85. let! ftype = ty_exp
  86. pure (Fimport { cc, fent, var, ftype })
  87. let func : forall 'm. monad 'm => parser_t 'm decl =
  88. let! nm = varid
  89. let! args = many (try varid)
  90. let! _ = equals
  91. map (fun e -> Decl (nm, args, e)) expr
  92. let dec : forall 'm. monad 'm => parser_t 'm decl =
  93. let foreign =
  94. let! _ = try (keyword "foreign")
  95. map Foreign fdecl
  96. try datadec <|> try foreign <|> func
  97. let prog : forall 'm. monad 'm => parser_t 'm prog =
  98. sep_end_by_1 semi dec <* eof