This commit is contained in:
parent
aa2ff7b2f0
commit
2f3113f55d
11742 changed files with 1223940 additions and 0 deletions
|
|
@ -0,0 +1,11 @@
|
|||
#include <caml/alloc.h>
|
||||
#include <caml/memory.h>
|
||||
#include <math.h>
|
||||
|
||||
value calc_log10 (value vx)
|
||||
{
|
||||
CAMLparam1(vx);
|
||||
double x = Double_val(vx);
|
||||
double r = log10(x);
|
||||
CAMLreturn(caml_copy_double(r));
|
||||
}
|
||||
|
|
@ -0,0 +1,60 @@
|
|||
type value = VInt of int | VFloat of float
|
||||
|
||||
let value_to_string = function
|
||||
| VInt n -> string_of_int n
|
||||
| VFloat f -> Printf.sprintf "%.6g" f
|
||||
|
||||
let eval_number_op f_int f_float va vb =
|
||||
match (va, vb) with
|
||||
| VInt na, VInt nb -> VInt (f_int na nb)
|
||||
| VFloat fa, VFloat fb -> VFloat (f_float fa fb)
|
||||
| VInt na, VFloat fb -> VFloat (f_float (float_of_int na) fb)
|
||||
| VFloat fa, VInt nb -> VFloat (f_float fa (float_of_int nb))
|
||||
|
||||
let as_float = function
|
||||
| VInt n -> float_of_int n
|
||||
| VFloat f -> f
|
||||
|
||||
external log10_c : float -> float = "calc_log10"
|
||||
|
||||
let rec eval = function
|
||||
| Ast.Int n -> VInt n
|
||||
| Float f -> VFloat f
|
||||
| Ident "pi" -> VFloat (2. *. Stdlib.acos 0.)
|
||||
| Ident _ -> failwith "unknown ident"
|
||||
| Op (Add, a, b) -> eval_number_op ( + ) ( +. ) (eval a) (eval b)
|
||||
| Op (Mul, a, b) -> eval_number_op ( * ) ( *. ) (eval a) (eval b)
|
||||
| Op (Div, a, b) -> eval_number_op ( / ) ( /. ) (eval a) (eval b)
|
||||
| Call ("sin", e) -> VFloat (Stdlib.sin (as_float (eval e)))
|
||||
| Call ("log10", e) -> VFloat (log10_c (as_float (eval e)))
|
||||
| Call _ -> failwith "unknown function"
|
||||
|
||||
let info = Cmdliner.Cmd.info "calc"
|
||||
|
||||
let eval_lb lb =
|
||||
try
|
||||
let expr = Parser.main Lexer.token lb in
|
||||
let v = eval expr in
|
||||
Printf.printf "%s\n" (value_to_string v)
|
||||
with Parser.Error ->
|
||||
Printf.printf "parse error near character %d" lb.lex_curr_pos
|
||||
|
||||
let repl () =
|
||||
while true do
|
||||
Printf.printf ">> %!";
|
||||
let lb = Lexing.from_channel Stdlib.stdin in
|
||||
eval_lb lb
|
||||
done
|
||||
|
||||
let term =
|
||||
let open Cmdliner.Term.Syntax in
|
||||
let+ expr_opt =
|
||||
let open Cmdliner.Arg in
|
||||
value & opt (some string) None & info [ "e" ]
|
||||
in
|
||||
match expr_opt with
|
||||
| Some s -> eval_lb (Lexing.from_string s)
|
||||
| None -> repl ()
|
||||
|
||||
let cmd = Cmdliner.Cmd.v info term
|
||||
let main () = Cmdliner.Cmd.eval cmd |> Stdlib.exit
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
(library
|
||||
(name calc)
|
||||
(libraries cmdliner)
|
||||
(foreign_stubs
|
||||
(language c)
|
||||
(names calc_stubs)))
|
||||
|
||||
(ocamllex lexer)
|
||||
|
||||
(menhir
|
||||
(modules parser))
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
let space = [' ']+
|
||||
|
||||
let digit = ['0'-'9']+
|
||||
|
||||
let letter = ['a'-'z']
|
||||
|
||||
let ident = letter (letter | digit)+
|
||||
|
||||
rule token = parse
|
||||
| eof { Parser.Eof }
|
||||
| space { token lexbuf }
|
||||
| '\n' { Parser.Eof }
|
||||
| '+' { Parser.Plus }
|
||||
| '*' { Parser.Star }
|
||||
| '/' { Parser.Slash }
|
||||
| '(' { Parser.Lpar }
|
||||
| ')' { Parser.Rpar }
|
||||
| digit+ { Parser.Int (int_of_string (Lexing.lexeme lexbuf)) }
|
||||
| digit+ '.' digit+ { Parser.Float (float_of_string (Lexing.lexeme lexbuf)) }
|
||||
| ident { Parser.Ident (Lexing.lexeme lexbuf) }
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
$ calc -e '1+2'
|
||||
3
|
||||
|
||||
$ calc -e '1+'
|
||||
parse error near character 2
|
||||
|
||||
$ calc -e '1+2.5'
|
||||
3.5
|
||||
|
||||
$ calc -e '1+pi'
|
||||
4.14159
|
||||
|
||||
$ calc -e '1+2*3'
|
||||
7
|
||||
|
||||
$ calc -e '4/2'
|
||||
2
|
||||
|
||||
$ calc -e 'sin (pi / 6)'
|
||||
0.5
|
||||
|
||||
$ calc -e 'log10(123456)'
|
||||
5.09151
|
||||
Loading…
Add table
Add a link
Reference in a new issue