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.

102 lines
2.8 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
  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. Tytup <$> sep_by comma ty_exp
  60. let datadec : forall 'm. monad 'm => parser_t 'm decl =
  61. let! _ = try (keyword "data")
  62. let datacon =
  63. let! nm = conid
  64. let! args = many ty_atom
  65. pure (Constr (nm, args))
  66. let! nm = conid
  67. let! args = many (try varid)
  68. let! _ = equals
  69. let! c = sep_by_1 pipe (try datacon)
  70. pure (Data (nm, args, c))
  71. let fdecl : forall 'm. monad 'm => parser_t 'm fdecl =
  72. let! _ = try (keyword "import")
  73. let! cc =
  74. ( (Lua <$ try (keyword "lua"))
  75. <|> (Prim <$ try (keyword "prim"))
  76. )
  77. let! fent = string
  78. let! var = varid
  79. let! _ = dcolon
  80. let! ftype = ty_exp
  81. pure (Fimport { cc, fent, var, ftype })
  82. let func : forall 'm. monad 'm => parser_t 'm decl =
  83. let! nm = varid
  84. let! args = many (try varid)
  85. let! _ = equals
  86. map (fun e -> Decl (nm, args, e)) expr
  87. let dec : forall 'm. monad 'm => parser_t 'm decl =
  88. let foreign =
  89. let! _ = try (keyword "foreign")
  90. map Foreign fdecl
  91. try datadec <|> try foreign <|> func
  92. let prog : forall 'm. monad 'm => parser_t 'm prog =
  93. sep_end_by_1 semi dec <* eof