aboutsummaryrefslogtreecommitdiff
path: root/rego-proofs/compiler-rewrite/bin/main.ml
blob: 2d4811c824b27974a2ec7148a4f8a05546f75c61 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
open Core
open Yojson.Basic.Util

type expr = Term of term
          | Call of reference * (term list)
          | Every of (string list) * every_item * query
[@@deriving show]
and every_item = Term of term
               | Call of (string list) * (term list)
[@@deriving show]
and literal = expr * (with_modifier option)
[@@deriving show]
and query = literal list
[@@deriving show]
and term = Scalar of scalar
          | Var of string
          | Ref of reference
          | Array of term list
          | Object of (object_key * term) list
          | Set of term list
          | ArrayComprehension of term * query
          | ObjectComprehension of (object_key * term) * query
          | SetComprehension of term * query
[@@deriving show]
and scalar = String of string
           | Number of float
           | Bool of bool
           | Null
[@@deriving show]
and reference = ref_target * (ref_arg list)
[@@deriving show]
and ref_target = Var of string
               | Array of term list
               | Object of (object_key * term) list
               | Set of term list
               | ArrayComprehension of term * query
               | ObjectComprehension of (object_key * term) * query
               | SetComprehension of term * query
               | Call of reference * (term list)
[@@deriving show]
and ref_arg = Scalar of scalar
            | Var of string
            | Array of term list
            | Object of (object_key * term) list
            | Set of term list
[@@deriving show]
and object_key = Scalar of scalar
               | Ref of reference
               | Var of string
[@@deriving show]
and with_modifier = (term * term)
[@@deriving show]

type assign_operator = Assign | Unify
[@@deriving show]

type rule_head_lhs = reference
[@@deriving show]
type rule_head_rhs = Assignment of assign_operator * term
                   | Object of term * assign_operator * term
                   | Function of (term list) * assign_operator * term
                   | Set of term
[@@deriving show]
type rule_head = rule_head_lhs * rule_head_rhs
[@@deriving show]
type rule =
  { default : bool;
    head : rule_head;
    body: literal list;
    otherwise : rule option;
  }
[@@deriving show]

type import =
  { alias : string option;
    path : reference;
  }
[@@deriving show]

type package =
  { path : reference;
    imports: import list;
    policy : rule list;
  }
[@@deriving show]

let to_bool_or_false : Yojson.Basic.t -> bool = function
  | `Bool b -> b
  | _ -> false

let to_list_or_empty : Yojson.Basic.t -> Yojson.Basic.t list = function
  | `Null -> []
  | `List xs -> xs
  | _ -> failwith "Not a list or null"

let object_key_of_term : term -> object_key = function
  | Scalar s -> Scalar s
  | Ref r -> Ref r
  | Var v -> Var v
  | _ -> failwith "failed to convert term to object_key"

let rec parse_term (tree : Yojson.Basic.t) : term =
  Printf.printf "%s: %s\n" "parse_term" (Yojson.Basic.to_string tree);
  let term_type = tree |> member "type" |> to_string in
  match term_type with
  | "var" -> Var (tree |> member "value" |> to_string)
  | "ref" -> Ref (tree |> member "value" |> to_list |> parse_ref)
  | "boolean" -> Scalar (Bool (tree |> member "value" |> to_bool))
  | "number" -> Scalar (Number (tree |> member "value" |> to_number))
  | "string" -> Scalar (String (tree |> member "value" |> to_string))
  | "array" -> Array (tree |> member "value" |> to_list |> List.map ~f:parse_term)
  | "set" -> Set (tree |> member "value" |> to_list |> List.map ~f:parse_term)
  | "object" ->
     Object
       (tree
        |> member "value"
        |> to_list
        |> List.map ~f:(function
               | `List [x; y] ->
                  (x |> parse_term |> object_key_of_term, y |> parse_term)
               | _ -> failwith "ill-formed object"))
  | "arraycomprehension" ->
     ArrayComprehension
       (tree |> member "value" |> member "term" |> parse_term,
        tree |> member "value" |> member "body" |> to_list |> List.map ~f:(fun x -> (parse_expr x, None)))
  | "setcomprehension" ->
     SetComprehension
       (tree |> member "value" |> member "term" |> parse_term,
        tree |> member "value" |> member "body" |> to_list |> List.map ~f:(fun x -> (parse_expr x, None)))
  | "objectcomprehension" ->
     ObjectComprehension
       ((tree |> member "value" |> member "key" |> parse_term |> object_key_of_term,
         tree |> member "value" |> member "value" |> parse_term),
        tree |> member "value" |> member "body" |> to_list |> List.map ~f:(fun x -> (parse_expr x, None)))
  | _ -> failwith ("unsupported term type " ^ term_type)

and parse_expr tree : expr =
  Printf.printf "%s: %s\n" "parse_expr" (Yojson.Basic.to_string tree);
  match tree |> member "terms" with
  | `List (func :: args) ->
     Call (func |> member "value" |> to_list |> parse_ref,
           List.map ~f:parse_term args)
  | `List _ -> failwith "todo"
  | x -> Term (parse_term x)

and parse_ref_target tree : ref_target =
  Printf.printf "%s: %s\n" "parse_ref_target" (Yojson.Basic.to_string tree);
  if String.equal (tree |> member "type" |> to_string) "call" then
    match tree |> member "value" |> to_list with
    | func :: args ->
       Call (func |> member "value" |> to_list |> parse_ref,
             List.map ~f:parse_term args)
    | _ -> failwith "ill_formed call"
  else
     let term = parse_term tree in
     match term with
     | Var term -> Var term
     | Array term -> Array term
     | Object term -> Object term
     | Set term -> Set term
     | ArrayComprehension (term, y) -> ArrayComprehension (term, y)
     | ObjectComprehension (term, y) -> ObjectComprehension (term, y)
     | SetComprehension (term, y) -> SetComprehension (term, y)
     | _ -> failwith ("unsupported ref_target " ^ (show_term term))

and parse_ref_arg tree : ref_arg =
  Printf.printf "%s: %s\n" "parse_ref_arg" (Yojson.Basic.to_string tree);
  let term = parse_term tree in
  match term with
  | Scalar x -> Scalar x
  | Var x -> Var x
  | Array x -> Array x
  | Object x -> Object x
  | Set x -> Set x
  | _ -> failwith ("unsupported ref_arg " ^ (show_term term))

and parse_ref tree =
  Printf.printf "%s: %s\n" "parse_ref" (String.concat (List.map ~f:Yojson.Basic.to_string tree));
  match tree with
  | target :: args -> (parse_ref_target target, List.map ~f:parse_ref_arg args)
  | _ -> failwith "empty reference"

let parse_head tree =
  Printf.printf "%s: %s\n" "parse_head" (Yojson.Basic.to_string tree);
  (* Variables are desugared to empty references. *)
  let lhs = tree |> member "ref" |> to_list |> parse_ref in
  let rhs =
    if tree |> member "assign" |> to_bool_or_false then
      Assignment (Assign, (tree |> member "value" |> parse_term))
                 (* TODO: Rest cases? *)
    else
      Assignment (Unify, (tree |> member "value" |> parse_term))
  in (lhs, rhs)

let rec parse_rule tree =
  Printf.printf "%s: %s\n" "rec parse_rule tree" (Yojson.Basic.to_string tree);
  { default = tree |> member "default" |> to_bool_or_false;
    head = tree |> member "head" |> parse_head;
    body = List.map ~f:(fun x -> ((parse_expr x, None) : literal)) (tree |> member "body" |> to_list_or_empty);
    otherwise =
      match tree |> member "else" with
      | `Null -> None
      | else_branch -> Some (parse_rule else_branch)
  }

(* --- *)

let parse_import_path tree =
  Printf.printf "%s: %s\n" "parse_import_path" (Yojson.Basic.to_string tree);
  match (tree |> member "type" |> to_string) with
  | "ref" -> tree |> member "value" |> to_list |> parse_ref
  | _ -> failwith "unimplemented"

let parse_import tree =
  Printf.printf "%s: %s\n" "parse_import" (Yojson.Basic.to_string tree);
  { alias = tree |> member "alias" |> to_string_option;
    path = tree |> member "path" |> parse_import_path
  }

(* --- *)

let parse_package tree =
  Printf.printf "%s: %s\n" "parse_package" (Yojson.Basic.to_string tree);
  tree |> member "path" |> to_list |> parse_ref

(* --- *)

let parse_tree tree =
  Printf.printf "%s: %s\n" "parse_tree" (Yojson.Basic.to_string tree);
  { path = tree |> member "package" |> parse_package;
    imports = List.map ~f:parse_import (tree |> member "imports" |> to_list_or_empty);
    policy = List.map ~f:parse_rule (tree |> member "rules" |> to_list_or_empty)
  }

let invoke ?(stdin="") ?(env=[| |]) cmd =
  let ic, oc, ec = UnixLabels.open_process_full cmd ~env:env in
  Out_channel.output_string oc stdin ;
  Out_channel.close oc;
  (In_channel.input_all ic,
   In_channel.input_all ec)

let () =
  let file_name = Array.get (Sys.get_argv ()) 1 in
  let policy_code = In_channel.read_all file_name in
  let (stdout, _) = invoke ("/home/jakob/.local/bin/opa parse -f json /dev/stdin") ~stdin:policy_code ~env:[| |] in
  Yojson.Basic.from_string stdout |> parse_tree |> show_package |> print_endline

© 2015 - 2026 Jakob L. Kreuze