-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.ml
50 lines (44 loc) · 1.59 KB
/
parser.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
open Core
open Evaluator
module type PARSER =
sig
type token
val prompt : string -> string
val build_ast : string -> token
val print_result : token -> string
exception ParsingError
end
module Make (Evaluator : EVALUATOR) : PARSER =
struct
type token = Evaluator.token
exception ParsingError
let tokenize str =
let lefts = Str.global_replace (Str.regexp "[ ]*)") " ) "
and rights = Str.global_replace (Str.regexp "[ ]*(") " ( "
and cut = Str.split (Str.regexp "[ \t]+")
in
cut (lefts (rights (str)))
let parse (tokens : string list) =
let rec pom tokens acc =
match tokens with
| "(" :: t -> let (tokens_2, acc_2) = pom t [] in pom tokens_2 (acc_2::acc)
| ")" :: t -> (t, (Evaluator.TList acc))
| h :: t -> pom t (Evaluator.TString h::acc)
| _ -> (tokens, Evaluator.TList acc)
and rev t = match t with
| Evaluator.TString s -> Evaluator.TString s
| Evaluator.TList l -> Evaluator.TList (List.rev (List.map rev l))
in
let (_, tokeny) = pom tokens []
in match tokeny with
| Evaluator.TList [Evaluator.TList t] -> rev (Evaluator.TList t)
| Evaluator.TList [Evaluator.TString s] -> Evaluator.TString s
| _ -> raise ParsingError
let build_ast (t : string) = parse (tokenize t)
let rec print_result (t : Evaluator.token) =
match t with
| Evaluator.TString s -> s
| Evaluator.TList l -> String.concat "" ["("; (String.concat " " (List.map print_result l)); ")"]
let pseudo_prompt (t : string) = print_result (Evaluator.eval_ast (build_ast t))
let prompt = pseudo_prompt
end