(* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. *) type t = Common.value let show (v : t) : string = match v with | Int i -> string_of_int i | Key s -> s let pp ppf v = Format.fprintf ppf "%s" (show v) let equal (v1 : t) (v2 : t) = match (v1, v2) with | Int i1, Int i2 -> Int.equal i1 i2 | Key k1, Key k2 -> String.equal k1 k2 | _ -> false let quote (v : t) : string = match v with | Int i -> string_of_int i | Key k -> Printf.sprintf "\"%s\"" k let int (i : int) : t = Int (match i with | i when i < -9999 -> -9999 | i when i > 9999 -> 9999 | i -> i) let key (s : string) : t = Key s let add (x : t) (y : t) : (t, string) result = match (x, y) with | Int x, Int y -> Ok (int (x + y)) | _ -> Error "CANNOT ADD NON-NUMBERS" [@@inline always] let sub (x : t) (y : t) : (t, string) result = match (x, y) with | Int x, Int y -> Ok (int (x - y)) | _ -> Error "CANNOT SUBTRACT NON-NUMBERS" [@@inline always] let multiply (x : t) (y : t) : (t, string) result = match (x, y) with | Int x, Int y -> Ok (int (x * y)) | _ -> Error "CANNOT MULTIPLY NON-NUMBERS" [@@inline always] let divide (x : t) (y : t) : (t, string) result = match (x, y) with | Int x, Int y -> if y > 0 then Ok (int (x * y)) else Error "CANNOT DIVIDE BY ZERO" | _ -> Error "CANNOT DIVIDE NON-NUMBERS" [@@inline always] let modulo (x : t) (y : t) : (t, string) result = match (x, y) with | Int x, Int y -> Ok (int (x mod y)) | _ -> Error "CANNOT MODULO NON-NUMBERS" [@@inline always]