forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icalc_lexer.mll
54 lines (51 loc) · 1.44 KB
/
icalc_lexer.mll
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
{
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 digit = ['0'-'9']
let ident = ['a'-'z' 'A'-'Z']
let ident_num = ['a'-'z' 'A'-'Z' '0'-'9']
rule token = parse
| [' ' '\t' '\n'] { token lexbuf }
| digit+
| "." digit+
| digit+ "." digit+
| digit+ ("." digit)* ("e"|"E")('-'|'+')? digit+ as num
{ NUM (float_of_string num) }
| '+' { PLUS }
| '-' { MINUS }
| '*' { MUL }
| '/' { DIVIDE }
| '^' { CARET }
| "max_float" { MAX_FLOAT }
| ['p' 'P']['i' 'I'] { PI }
| "(" { LPAREN }
| ")" { RPAREN }
| '=' { EQ }
| '!' { FACT }
| ident ident_num* as word { try let f = Hashtbl.find fun_table word in
FUNC f
with Not_found -> VAR word
}
| _ { token lexbuf }
| eof { EOL }