compiler and vm: added subtraction
This commit is contained in:
@@ -58,7 +58,8 @@ type expression =
|
|||||||
let default_global_table =
|
let default_global_table =
|
||||||
SymbolTable.of_list [
|
SymbolTable.of_list [
|
||||||
("PRINT", (0, Native 0));
|
("PRINT", (0, Native 0));
|
||||||
("ADD", (1, Native 1))
|
("+", (1, Native 1));
|
||||||
|
("-", (2, Native 2))
|
||||||
]
|
]
|
||||||
|
|
||||||
(* extract all defined global symbols, given the top-level expressions
|
(* extract all defined global symbols, given the top-level expressions
|
||||||
|
|||||||
+14
-4
@@ -23,20 +23,30 @@ let numeric_add = function
|
|||||||
| NDouble x -> (function
|
| NDouble x -> (function
|
||||||
| NInt y -> NDouble (x+.(float_of_int y))
|
| NInt y -> NDouble (x+.(float_of_int y))
|
||||||
| NDouble y -> NDouble (x+.y))
|
| NDouble y -> NDouble (x+.y))
|
||||||
|
let numeric_sub = function
|
||||||
|
| NInt x -> (function
|
||||||
|
| NInt y -> NInt (x - y)
|
||||||
|
| NDouble y -> NDouble ((float_of_int x) -. y))
|
||||||
|
| NDouble x -> (function
|
||||||
|
| NInt y -> NDouble (x-.(float_of_int y))
|
||||||
|
| NDouble y -> NDouble (x-.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;
|
||||||
Types.Nil
|
Types.Nil
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let builtin_add (vs : Types.value ref list) =
|
let builtin_add (vs : Types.value ref list) =
|
||||||
of_numeric (List.fold_left numeric_add (NInt 0) (List.map (fun r -> to_numeric !r) vs))
|
of_numeric (List.fold_left numeric_add (NInt 0) (List.map (fun r -> to_numeric !r) vs))
|
||||||
|
let builtin_sub (vs : Types.value ref list) =
|
||||||
|
match vs with
|
||||||
|
| 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"
|
||||||
|
|
||||||
|
|
||||||
let table = [|
|
let table = [|
|
||||||
builtin_print;
|
builtin_print;
|
||||||
builtin_add
|
builtin_add;
|
||||||
|
builtin_sub
|
||||||
|]
|
|]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user