Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- Copyright (C) 2021 Caden Haustein <mlatu@brightlysalty.33mail.com>
- This file is part of mlatu.
- 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
- at <https://git.pixie.town/thufie/npl-builder>.
- mlatu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. See the CNPL for details.
- *)
- open! Batteries
- open Opal
- type term =
- | Call of Mlatu__Word.t
- | Def of Mlatu__Word.t * term list
- | BoolLit of bool
- | NumLit of int
- | QuoteLit of term list
- let word = many1 (satisfy (fun c -> c != ' ' && c != '\t' && c!= '\r' && c != '\n')) => (fun cs -> Mlatu__Word.of_string (implode cs))
- let def_tok = token "def"
- let rec term () =
- let definition =
- def_tok
- >> word >>= (fun w -> exactly '=' >> (many (spaces >> term ()) => fun t -> Def (w, t)) << spaces << exactly '.')
- in
- let quotation =
- exactly '('
- >> (many (spaces >> term ()) << spaces)
- << exactly ')'
- => fun q -> QuoteLit q in
- let boolean =
- token "true" => (fun _ -> BoolLit true) <|> (token "false" => (fun _ -> BoolLit false)) in
- let number = many1 digit => fun cs -> NumLit (Int.of_string (implode cs)) in
- let call = word => (fun w -> Call w) in
- choice
- [ boolean; number
- ; definition; quotation
- ; call ]
- let top = many (spaces >> term ()) << (spaces << eof ())
- let parse_program_from_channel c =
- let ls = Opal.LazyStream.of_channel c in
- match top ls with
- | Some (program, _) -> Result.ok program
- | None -> Result.error "Parse error"
- let parse_program_from_string s =
- let ls = Opal.LazyStream.of_string s in
- match top ls with
- | Some (program, _) -> Result.ok program
- | None -> Result.error "Parse error"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement