2022-01-10 01:31:47 +09:00
|
|
|
type t =
|
|
|
|
| Int of int
|
|
|
|
| Ident of string
|
|
|
|
| Plus
|
|
|
|
| Minus
|
|
|
|
| Asterisk
|
|
|
|
| Slash
|
2022-01-13 01:13:41 +09:00
|
|
|
| Carret
|
|
|
|
| Percent
|
2022-01-10 01:31:47 +09:00
|
|
|
| LParen
|
|
|
|
| RParen
|
|
|
|
|
|
|
|
let of_char = function
|
|
|
|
| '+' -> Plus
|
|
|
|
| '-' -> Minus
|
|
|
|
| '*' -> Asterisk
|
|
|
|
| '/' -> Slash
|
2022-01-13 01:13:41 +09:00
|
|
|
| '^' -> Carret
|
|
|
|
| '%' -> Percent
|
2022-01-10 01:31:47 +09:00
|
|
|
| '(' -> LParen
|
|
|
|
| ')' -> RParen
|
|
|
|
| _ -> invalid_arg "Token.of_char"
|
|
|
|
|
|
|
|
let of_string str =
|
|
|
|
let fc = Char.code str.[0] in
|
|
|
|
if Char.(code '0' <= fc && fc <= code '9') then
|
|
|
|
Int (int_of_string str)
|
|
|
|
else
|
|
|
|
match str with
|
|
|
|
| _ when String.length str = 1 -> of_char str.[0]
|
|
|
|
| _ -> failwith "Token.of_string"
|
|
|
|
|
|
|
|
let to_string = function
|
|
|
|
| Int n -> string_of_int n
|
|
|
|
| Ident s -> s
|
|
|
|
| Plus -> "+"
|
|
|
|
| Minus -> "-"
|
|
|
|
| Asterisk -> "*"
|
|
|
|
| Slash -> "/"
|
2022-01-13 01:13:41 +09:00
|
|
|
| Carret -> "^"
|
|
|
|
| Percent -> "%"
|
2022-01-10 01:31:47 +09:00
|
|
|
| LParen -> "("
|
|
|
|
| RParen -> ")"
|