compiler and vm: added division
This commit is contained in:
@@ -60,7 +60,8 @@ let default_global_table =
|
|||||||
("PRINT", (0, Native 0));
|
("PRINT", (0, Native 0));
|
||||||
("+", (1, Native 1));
|
("+", (1, Native 1));
|
||||||
("-", (2, Native 2));
|
("-", (2, Native 2));
|
||||||
("*", (3, Native 3))
|
("*", (3, Native 3));
|
||||||
|
("/", (4, Native 4))
|
||||||
]
|
]
|
||||||
|
|
||||||
(* extract all defined global symbols, given the top-level expressions
|
(* extract all defined global symbols, given the top-level expressions
|
||||||
|
|||||||
+14
-3
@@ -17,6 +17,10 @@ let of_numeric = function
|
|||||||
| NInt x -> Int x
|
| NInt x -> Int x
|
||||||
| NDouble x -> Double 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
|
let numeric_generic fi fd = function
|
||||||
| NInt x -> (function
|
| NInt x -> (function
|
||||||
| NInt y -> NInt (fi x y)
|
| NInt y -> NInt (fi x y)
|
||||||
@@ -27,7 +31,8 @@ let numeric_generic fi fd = function
|
|||||||
let numeric_add = numeric_generic (+) (+.)
|
let numeric_add = numeric_generic (+) (+.)
|
||||||
let numeric_sub = numeric_generic (-) (-.)
|
let numeric_sub = numeric_generic (-) (-.)
|
||||||
let numeric_mul = 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) =
|
let builtin_print (v : Types.value ref list) =
|
||||||
List.iter (fun r -> print_endline (print_value !r)) v;
|
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)
|
| NInt x -> NInt (Int.neg x)
|
||||||
| NDouble x -> NDouble (Float.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))
|
| 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 =
|
let builtin_mul vs =
|
||||||
of_numeric (List.fold_left numeric_mul (NInt 1) (List.map (fun r -> to_numeric !r) 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 = [|
|
let table = [|
|
||||||
builtin_print;
|
builtin_print;
|
||||||
builtin_add;
|
builtin_add;
|
||||||
builtin_sub;
|
builtin_sub;
|
||||||
builtin_mul
|
builtin_mul;
|
||||||
|
builtin_div
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user