ce/parser.ml

151 lines
3.5 KiB
OCaml
Raw Normal View History

2022-01-10 01:31:47 +09:00
open Ast
2022-01-18 16:52:33 +09:00
open Ast.Binop
2022-01-10 01:31:47 +09:00
2022-01-11 01:05:29 +09:00
module S = Set.Make(String)
2022-01-10 01:31:47 +09:00
exception Expected of string
exception Unexpected_token of string
2022-01-19 02:10:34 +09:00
exception End_of_tokens
2022-01-10 01:31:47 +09:00
let expected t =
2022-01-18 16:52:33 +09:00
raise @@ Expected t
2022-01-10 01:31:47 +09:00
let unexpected_token t =
raise @@ Unexpected_token (Token.to_string t)
2022-01-10 23:11:13 +09:00
(* precedence table.
* my first thought was using some sort of partially-ordered graph for
* precedency, but infering precedence relation from the graph is hard
* and the graph can be made to have loops, I just used plain table. *)
2022-01-10 01:31:47 +09:00
let precedence = [
2022-01-11 01:05:29 +09:00
Add, 10;
Sub, 10;
Mul, 20;
Div, 20;
Mod, 30;
Exp, 30;
2022-01-10 01:31:47 +09:00
] |> List.to_seq |> Hashtbl.of_seq
let precedence_of op =
2022-01-11 01:05:29 +09:00
Hashtbl.find precedence op
2022-01-10 01:31:47 +09:00
2022-01-19 15:28:41 +09:00
type associativity =
| Left_to_right
| Right_to_left
let oper_assoc = [
Exp, Right_to_left;
] |> List.to_seq |> Hashtbl.of_seq
let op_is_right_to_left op =
let a =
Hashtbl.find_opt oper_assoc op
|> Option.value ~default: Left_to_right
in
a = Right_to_left
2022-01-10 23:11:13 +09:00
2022-01-18 15:36:09 +09:00
let operators = [
Token.Plus, Add;
Minus, Sub;
Asterisk, Mul;
Slash, Div;
Carret, Exp;
Percent, Mod;
] |> List.to_seq |> Hashtbl.of_seq
let token_to_op tok =
try Hashtbl.find operators tok
with _ -> failwith "Parser.token_to_op"
2022-01-10 01:31:47 +09:00
2022-01-19 02:10:34 +09:00
let token_is_operator tok =
Hashtbl.mem operators tok
2022-01-13 00:31:26 +09:00
(* common parsers *)
let idents set seq =
match seq () with
| Seq.Nil ->
let msg = "ident " ^ (S.elements set |> String.concat " or ") in
expected msg
| Seq.Cons (x, seq) -> begin
match x with
| Token.Ident id when S.mem id set -> id, seq
| _ -> unexpected_token x
end
let ident str seq =
idents (S.singleton str) seq
let operator seq =
match seq () with
| Seq.Nil -> expected "operator"
| Seq.Cons (x, seq) ->
try token_to_op x, seq with
| _ -> expected "operator"
(* parser combinators *)
let either f g seq =
try f seq with _ -> g seq
let (@>) f g seq =
let a, seq = f seq in
g a seq
(* parse tokens *)
2022-01-10 23:11:13 +09:00
let parse ts =
2022-01-10 01:31:47 +09:00
(* value := int | ( expr ) *)
let rec value seq =
match seq () with
2022-01-19 02:10:34 +09:00
| Seq.Nil -> raise End_of_tokens
2022-01-10 01:31:47 +09:00
| Seq.Cons (x, seq) -> begin match x with
| Token.Int n -> Value (Int n), seq
2022-01-19 02:10:34 +09:00
| Float n -> Value (Float n), seq
2022-01-19 14:17:04 +09:00
| Ident id -> Var id, seq
2022-01-10 01:31:47 +09:00
| LParen -> expr seq
| _ -> unexpected_token x
end
(* binop := binop op binop *)
and binop pre left seq =
match seq () with
| Seq.Nil -> left, Seq.empty
| Seq.Cons (x, seq) -> begin match x with
2022-01-19 02:10:34 +09:00
| op when token_is_operator op ->
let op = token_to_op op in
let o = precedence_of op in
2022-01-19 02:10:34 +09:00
(* op has to be calculated first *)
if o > pre || op_is_right_to_left op && o = pre then
let v, seq = value seq in
let right, seq = binop o v seq in
binop pre (Ast.binop left op right) seq
else
left, Seq.cons x seq
2022-01-19 02:10:34 +09:00
| Token.RParen -> left, seq
2022-01-10 01:31:47 +09:00
| _ -> unexpected_token x
end
2022-01-19 02:10:34 +09:00
(* level_inner := "get" | "set" [op] *)
and level_inner _ seq =
2022-01-13 00:31:26 +09:00
let id, seq = idents (S.of_list ["get"; "set"]) seq in
2022-01-11 01:05:29 +09:00
let op, seq = operator seq in
if id = "get" then
Get_binop_pre op, seq
2022-01-13 00:31:26 +09:00
else if id = "set" then
2022-01-11 01:05:29 +09:00
let v, seq = value seq in
Set_binop_pre (op, v), seq
2022-01-13 00:31:26 +09:00
else
failwith "Parser.level"
2022-01-10 01:31:47 +09:00
2022-01-19 02:10:34 +09:00
(* expr := "level" level_inner
* | value binop_right
*)
2022-01-10 01:31:47 +09:00
and expr seq =
2022-01-13 00:31:26 +09:00
seq |> either
2022-01-19 02:10:34 +09:00
(ident "level" @> level_inner)
2022-01-13 00:31:26 +09:00
(value @> binop ~-1)
2022-01-10 01:31:47 +09:00
in
2022-01-10 23:11:13 +09:00
let ast, rest = expr ts in
2022-01-10 01:31:47 +09:00
if rest () <> Seq.Nil then failwith "Parser.parse";
ast