forked from ermine/sulci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
icalc.mly
56 lines (44 loc) · 1.27 KB
/
icalc.mly
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
%{
(*
* (c) 2004-2010 Anastasia Gornostaeva
*)
open Printf
open Math
let var_table = Hashtbl.create 16
%}
%token <float> NUM
%token LPAREN RPAREN EQ FACT
%token PLUS MINUS MUL DIVIDE MOD CARET NEG
%token MAX_FLOAT PI
%token EOL
%token <string> VAR
%token <float -> float> FUNC
%start line
%type <string> line
%nonassoc EQ FUNC FACT
%left PLUS MINUS
%left MUL DIVIDE MOD
%right NEG
%right CARET
%%
line:
| expr EOL { sprintf "%.10g" $1 }
expr:
| MAX_FLOAT { max_float }
| PI { 4. *. (atan (1./.2.) +. atan (1./.3.)) }
| NUM { $1 }
| LPAREN expr RPAREN { $2 }
| VAR { try Hashtbl.find var_table $1 with Not_found -> 0.0 }
| VAR EQ expr { Hashtbl.replace var_table $1 $3; $3 }
| FUNC LPAREN expr RPAREN { $1 $3 }
| expr PLUS expr { $1 +. $3 }
| expr MINUS expr { $1 -. $3 }
| expr MUL expr { $1 *. $3 }
| expr DIVIDE expr { if $3 <> 0.0 then $1 /. $3
else failwith "plugin_calc_divide_by_zero"}
| expr MOD expr { if $3 <> 0.0 then mod_float $1 $3
else failwith "plugin_calc_divide_by_zero" }
| expr CARET expr { $1 ** $3 }
| MINUS expr %prec NEG { -. $2 }
| expr FACT { fact $1 }
%%