diff --git a/lib/compiler/scope_analysis.ml b/lib/compiler/scope_analysis.ml index b3cc8a0..1d71bc5 100644 --- a/lib/compiler/scope_analysis.ml +++ b/lib/compiler/scope_analysis.ml @@ -60,7 +60,8 @@ let default_global_table = ("PRINT", (0, Native 0)); ("+", (1, Native 1)); ("-", (2, Native 2)); - ("*", (3, Native 3)) + ("*", (3, Native 3)); + ("/", (4, Native 4)) ] (* extract all defined global symbols, given the top-level expressions diff --git a/lib/vm/native.ml b/lib/vm/native.ml index a4d6297..e107c6b 100644 --- a/lib/vm/native.ml +++ b/lib/vm/native.ml @@ -17,6 +17,10 @@ let of_numeric = function | NInt x -> Int x | NDouble x -> Double x +let float_of_numeric = function + | NInt x -> float_of_int x + | NDouble x -> x + let numeric_generic fi fd = function | NInt x -> (function | NInt y -> NInt (fi x y) @@ -27,7 +31,8 @@ let numeric_generic fi fd = function let numeric_add = numeric_generic (+) (+.) let numeric_sub = numeric_generic (-) (-.) let numeric_mul = numeric_generic ( * ) ( *. ) -let numeric_div = numeric_generic ( / ) ( /. ) +let numeric_div x y = + NDouble ((float_of_numeric x) /. (float_of_numeric y)) let builtin_print (v : Types.value ref list) = List.iter (fun r -> print_endline (print_value !r)) v; @@ -41,18 +46,24 @@ let builtin_sub (vs : Types.value ref list) = | NInt x -> NInt (Int.neg x) | NDouble x -> NDouble (Float.neg x)) | f :: rest -> of_numeric (List.fold_left numeric_sub (to_numeric !f) (List.map (fun r -> to_numeric !r) rest)) - | [] -> failwith "invalid number of arguments for subtraction" + | [] -> failwith "invalid number of arguments for subtraction: 0" let builtin_mul vs = of_numeric (List.fold_left numeric_mul (NInt 1) (List.map (fun r -> to_numeric !r) vs)) +let builtin_div vs = + match vs with + | f :: [] -> of_numeric (numeric_div (NDouble 1.0) (to_numeric !f)) + | f :: rest -> of_numeric (List.fold_left numeric_div (to_numeric !f) (List.map (fun r -> to_numeric !r) rest)) + | [] -> failwith "invalid number of arguments for division: 0" let table = [| builtin_print; builtin_add; builtin_sub; - builtin_mul + builtin_mul; + builtin_div |]