forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icalc_ulex.ml
82 lines (75 loc) · 1.78 KB
/
icalc_ulex.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(*
* (c) 2004-2010 Anastasia Gornostaeva
*)
open Icalc
let create_hashtable size init =
let tbl = Hashtbl.create size in
List.iter (fun (key, data) -> Hashtbl.add tbl key data) init;
tbl
let fun_table =
create_hashtable 16 [
("sin", sin);
("cos", cos);
("tan", tan);
("asin", asin);
("acos", acos);
("atan", atan);
("cosh", cosh);
("sinh", sinh);
("tanh", tanh);
("log", log);
("log10", log10);
("exp", exp);
("sqrt", sqrt);
("fib", Math.fib)
]
let regexp ident_char = xml_letter | xml_digit | '_' | xml_combining_char | xml_extender
let regexp ident_first = xml_letter | '_' | xml_combining_char | xml_extender
let rec token = lexer
| [' ' '\t' '\n'] ->
token lexbuf
| xml_digit+
| "." xml_digit+
| xml_digit+ "." xml_digit+
| xml_digit+ ("." xml_digit)* ("e"|"E")('-'|'+')? xml_digit+ ->
let num = Ulexing.utf8_lexeme lexbuf in
NUM (float_of_string num)
| '0' ('x'|'X') ['a'-'f' 'A'-'F' '0'-'9']+ ->
NUM (float_of_string (Ulexing.utf8_lexeme lexbuf))
| '0' ['b' 'B'] ['0' '1']+ ->
NUM (float_of_int (int_of_string (Ulexing.utf8_lexeme lexbuf)))
| '0' ['o' 'O'] ['0'-'8']+ ->
NUM (float_of_int (int_of_string (Ulexing.utf8_lexeme lexbuf)))
| '+' ->
PLUS
| '-' ->
MINUS
| '*' ->
MUL
| '/' ->
DIVIDE
| '%' ->
MOD
| '^' ->
CARET
| "max_float" ->
MAX_FLOAT
| ['p' 'P']['i' 'I'] ->
PI
| "(" ->
LPAREN
| ")" ->
RPAREN
| '=' ->
EQ
| '!' ->
FACT
| ident_first ident_char* ->
let word = Ulexing.utf8_lexeme lexbuf in
(try let f = Hashtbl.find fun_table word in
FUNC f
with Not_found -> VAR word)
| eof ->
EOL
| _ ->
token lexbuf