ce/token.ml

34 lines
576 B
OCaml
Raw Normal View History

2022-01-10 01:31:47 +09:00
type t =
| Int of int
2022-01-18 15:33:56 +09:00
| Float of float
2022-01-10 01:31:47 +09:00
| Ident of string
| Plus
| Minus
| Asterisk
| Slash
| Carret
| Percent
2022-01-10 01:31:47 +09:00
| LParen
| RParen
2022-01-17 15:17:18 +09:00
let tokens = ref [
"+", Plus;
"-", Minus;
"*", Asterisk;
"/", Slash;
"^", Carret;
"%", Percent;
"(", LParen;
")", RParen;
]
2022-01-10 01:31:47 +09:00
let to_string = function
2022-01-18 15:33:56 +09:00
| Int n -> Printf.sprintf "[int: %d]" n
| Float n -> Printf.sprintf "[float: %f]" n
2022-01-10 01:31:47 +09:00
| Ident s -> s
2022-01-17 15:17:18 +09:00
| t ->
begin match List.find_opt (fun (_, tok) -> t = tok) !tokens with
| None -> failwith "Token.to_string"
| Some (s, _) -> s
end