Advertisement
Guest User

Untitled

a guest
Aug 20th, 2021
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.81 KB | None | 0 0
  1. (*
  2. Copyright (C) 2021 Caden Haustein <mlatu@brightlysalty.33mail.com>
  3. This file is part of mlatu.
  4. mlatu is non-violent software: you can use, redistribute, and/or modify it under the terms of the CNPL-NA v7+ as found in the LICENSE file in the source code root directory or
  5. at <https://git.pixie.town/thufie/npl-builder>.
  6. mlatu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. See the CNPL for details.
  7. *)
  8.  
  9. open! Batteries
  10. open Opal
  11.  
  12. type term =
  13.   | Call of Mlatu__Word.t
  14.   | Def of Mlatu__Word.t * term list
  15.   | BoolLit of bool
  16.   | NumLit of int
  17.   | QuoteLit of term list
  18.  
  19. let word = many1 (satisfy (fun c -> c != ' ' && c != '\t' && c!= '\r' && c != '\n')) => (fun cs ->  Mlatu__Word.of_string (implode cs))
  20.  
  21. let def_tok = token "def"
  22.  
  23. let rec term () =
  24.   let definition =
  25.     def_tok
  26.     >> word >>= (fun w -> exactly '=' >> (many (spaces >> term ())  => fun t -> Def (w, t)) << spaces << exactly '.')
  27.      in
  28.   let quotation =
  29.     exactly '('
  30.     >> (many (spaces >> term ()) << spaces)
  31.     << exactly ')'
  32.     => fun q -> QuoteLit q in
  33.   let boolean =
  34.     token "true" => (fun _ -> BoolLit true) <|> (token "false" => (fun _ -> BoolLit false)) in
  35.   let number = many1 digit => fun cs -> NumLit (Int.of_string (implode cs)) in
  36.   let call = word => (fun w -> Call w) in
  37.   choice
  38.     [ boolean; number
  39.      ; definition; quotation
  40.     ; call  ]
  41.  
  42. let top = many (spaces >> term ()) << (spaces  << eof ())
  43.  
  44. let parse_program_from_channel c =
  45.   let ls = Opal.LazyStream.of_channel c in
  46.   match top ls with
  47.   | Some (program, _) -> Result.ok program
  48.   | None -> Result.error "Parse error"
  49.  
  50. let parse_program_from_string s =
  51.   let ls = Opal.LazyStream.of_string s in
  52.   match top ls with
  53.   | Some (program, _) -> Result.ok program
  54.   | None -> Result.error "Parse error"
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement