ce/token.ml

34 lines
540 B
OCaml
Raw Normal View History

2022-01-10 01:31:47 +09:00
type t =
| Value of Ast.Value.t
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-21 00:17:01 +09:00
| Equal
2022-01-10 01:31:47 +09:00
2022-01-17 15:17:18 +09:00
let tokens = ref [
"+", Plus;
"-", Minus;
"*", Asterisk;
"/", Slash;
"^", Carret;
"%", Percent;
"(", LParen;
")", RParen;
2022-01-21 00:17:01 +09:00
"=", Equal;
2022-01-17 15:17:18 +09:00
]
2022-01-10 01:31:47 +09:00
let to_string = function
| Value v -> Ast.Value.to_string v
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