This commit is contained in:
parent
aa2ff7b2f0
commit
2f3113f55d
11742 changed files with 1223940 additions and 0 deletions
|
|
@ -0,0 +1,13 @@
|
|||
type op =
|
||||
| Add
|
||||
| Mul
|
||||
| Div
|
||||
[@@deriving show]
|
||||
|
||||
type exp =
|
||||
| Int of int
|
||||
| Float of float
|
||||
| Ident of string
|
||||
| Op of op * exp * exp
|
||||
| Call of string * exp
|
||||
[@@deriving show]
|
||||
|
|
@ -0,0 +1,64 @@
|
|||
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 ~debug_ast lb =
|
||||
try
|
||||
let expr = Parser.main Lexer.token lb in
|
||||
if debug_ast then Format.eprintf "[debug] %a\n" Ast.pp_exp expr;
|
||||
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 ~debug_ast =
|
||||
while true do
|
||||
Printf.printf ">> %!";
|
||||
let lb = Lexing.from_channel Stdlib.stdin in
|
||||
eval_lb ~debug_ast 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" ]
|
||||
and+ debug_ast =
|
||||
let open Cmdliner.Arg in
|
||||
value & flag & info [ "debug-ast" ]
|
||||
in
|
||||
match expr_opt with
|
||||
| Some s -> eval_lb ~debug_ast (Lexing.from_string s)
|
||||
| None -> repl ~debug_ast
|
||||
|
||||
let cmd = Cmdliner.Cmd.v info term
|
||||
let main () = Cmdliner.Cmd.eval cmd |> Stdlib.exit
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
(library
|
||||
(name calc)
|
||||
(libraries cmdliner)
|
||||
(foreign_stubs
|
||||
(language c)
|
||||
(names calc_stubs))
|
||||
(preprocess
|
||||
(pps ppx_deriving.show)))
|
||||
|
||||
(ocamllex lexer)
|
||||
|
||||
(menhir
|
||||
(modules parser))
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
$ 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
|
||||
|
||||
$ calc --debug-ast -e '2 * sin (pi / 2)'
|
||||
2
|
||||
[debug] (Ast.Op (Ast.Mul, (Ast.Int 2),
|
||||
(Ast.Call ("sin",
|
||||
(Ast.Op (Ast.Div, (Ast.Ident "pi"), (Ast.Int 2)))))
|
||||
))
|
||||
Loading…
Add table
Add a link
Reference in a new issue