package minicaml

  1. Overview
  2. Docs

Source file dictp.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
open Types
open Typecheck
open Util

(** Insert a key-value pair in a dictionary *)
let insert_dict act_params =
  let (k, v, d) = (match act_params with
    | [k; v; d] -> (k, v, unpack_dict d)
    | _ -> raise WrongBindList) in
  if key_exist k d 
  then EvtDict (isvalidkey (k, v) :: (delete_key k d))
  else EvtDict (isvalidkey (k, v) :: d)


(** Remove a key-value pair from a dictionary *)
let delete_dict act_params  =
  let (key, ed) = (match act_params with
    | [key; d] -> (key, unpack_dict d)
    | _ -> raise WrongPrimitiveArgs) in
  if not (key_exist key ed) then raise (DictError "key not found") else
  EvtDict (delete_key key ed)

(** Check if a key-value pair is in a dictionary *)
let haskey act_params =
  let (key, ed) = (match act_params with
    | [key; d] -> (key, unpack_dict d)
    | _ -> raise WrongPrimitiveArgs) in
  EvtBool(key_exist key ed)

(** Check if a dict contains a key *)
let getkey act_params =
  let (key, ed) = (match act_params with
    | [key; d] -> (key, unpack_dict d)
    | _ -> raise WrongPrimitiveArgs) in
  if not (key_exist key ed) then raise (DictError "key not found") else
  get_key_val key ed


(** Check if a dict contains a key *)
let filterkeys act_params =
  let (kll, ed) = (match act_params with
    | [kl; d] -> (unpack_list kl, unpack_dict d)
    | _ -> raise WrongPrimitiveArgs) in
  EvtDict(filter_by_keys kll ed)

let table = [
  ("insert", (insert_dict, 3));
  ("delete", (delete_dict, 2));
  ("haskey", (haskey, 2));
  ("getkey", (getkey, 2));
  ("filterkeys", (filterkeys, 2))
]