This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsexp.ml
68 lines (62 loc) · 2.07 KB
/
sexp.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
open Errors
open Datatypes
let operator out stack =
match !stack, !out with
| f :: stack', x :: y :: out' ->
stack := stack';
out := Symtree [x; f; y] :: out'
| _ -> raise (Other "Invalid application of binary operator")
let nonempty : 'a list -> bool = function
| [] -> false
| _ -> true
let isAtom : sexp -> bool = function
| Atom _ -> true
| _ -> false
let symbol : sexp -> string = function
| Atom s -> s
| _ -> raise (Other "Expected atom")
(* From: https://rosettacode.org/wiki/Parsing/Shunting-yard_algorithm#OCaml *)
let splitWhile p =
let rec go ls xs =
match xs with
| x :: xs' when p x -> go (x :: ls) xs'
| _ -> List.rev ls, xs
in go []
let rec term curr expr =
match expr with
| List (Atom "#" :: xs) -> shuntingyard curr xs
| List xs -> Symtree (List.map (term curr) xs)
| Supp xs -> Symtree (Lit "@" :: List.map (term curr) xs)
| Atom "_" -> Hole
| Atom name ->
if Names.mem name curr.variables then
Var (name, -1)
else Lit name
and shuntingyard curr exprs =
let prec x =
match x with
| Atom op -> Env.find op curr.infix
| _ -> raise (Other "Shunting-yard failed") in
let rec pusher stack queue xs =
match xs with
| [] -> List.rev queue @ stack
| Atom op :: rest when Env.mem op curr.infix ->
let mv, stack' = splitWhile (fun op' -> prec (Atom op) < prec op') stack in
pusher (Atom op :: stack') (mv @ queue) rest
| x :: rest ->
pusher stack (x :: queue) rest in
let rec constr (stack : term list) (xs : sexp list) =
match xs with
| [] -> stack
| Atom op :: rest when Env.mem op curr.infix ->
begin match stack with
| x :: y :: stack' ->
constr (Symtree [y; Lit op; x] :: stack') rest
| _ -> Other (Printf.sprintf "Not enough arguments for “%s”" op) |> raise
end
| x :: rest -> constr (term curr x :: stack) rest in
match constr [] (pusher [] [] exprs) with
| [res] -> res
| _ ->
let src = showSExp (List exprs) in
Other (Printf.sprintf "Redundant arguments in “%s”" src) |> raise