compiler and vm: implemented add

This commit is contained in:
2026-05-17 20:38:31 +03:00
parent 47ffeb4934
commit 3668265b6f
4 changed files with 31 additions and 4 deletions
+27 -1
View File
@@ -5,12 +5,38 @@
*)
open Types
type numeric_val =
| NInt of int
| NDouble of float
let to_numeric = function
| Int x -> NInt x
| Double x -> NDouble x
| v -> failwith ((print_value v) ^ " is not a numeric value")
let of_numeric = function
| NInt x -> Int x
| NDouble x -> Double x
let numeric_add = 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) =
List.iter (fun r -> print_endline (print_value !r)) v;
Types.Nil
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))
let table = [|
builtin_print
builtin_print;
builtin_add
|]