ce/token.ml

54 lines
950 B
OCaml
Raw Permalink Normal View History

2022-01-10 01:31:47 +09:00
type t =
2022-02-01 02:06:18 +09:00
| Int of int
| Float of float
| String of string
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-02-08 22:02:00 +09:00
| Not_equal
| Greater
| Less
| Greater_equal
| Less_equal
2022-02-08 00:26:03 +09:00
| Hash
2022-02-01 21:38:00 +09:00
| Right_arrow
2022-01-10 01:31:47 +09:00
2022-02-08 22:02:00 +09:00
(* list of tokens. because front tokens are detected first, longer
* tokens must come earlier than shorter tokens. *)
2022-01-17 15:17:18 +09:00
let tokens = ref [
2022-02-01 21:38:00 +09:00
"->", Right_arrow;
2022-02-08 22:02:00 +09:00
"<>", Not_equal;
">=", Greater_equal;
"<=", Less_equal;
">", Greater;
"<", Less;
2022-01-17 15:17:18 +09:00
"+", Plus;
"-", Minus;
"*", Asterisk;
"/", Slash;
"^", Carret;
"%", Percent;
"(", LParen;
")", RParen;
2022-01-21 00:17:01 +09:00
"=", Equal;
2022-02-08 00:26:03 +09:00
"#", Hash;
2022-01-17 15:17:18 +09:00
]
2022-01-10 01:31:47 +09:00
let to_string = function
2022-02-01 02:06:18 +09:00
| Int n -> string_of_int n
| Float n -> string_of_float n
| String x -> x
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