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