Skip to content

Commit

Permalink
nits
Browse files Browse the repository at this point in the history
  • Loading branch information
strub committed Dec 5, 2023
1 parent c44ac36 commit 234a59c
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 44 deletions.
1 change: 1 addition & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
dune-site
(ocaml-inifiles (>= 1.2))
(pcre (>= 7))
ppx_deriving
(why3 (and (>= 1.6.0) (< 1.7)))
yojson
(zarith (>= 1.10))
Expand Down
1 change: 1 addition & 0 deletions libs/lospecs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.exe
1 change: 1 addition & 0 deletions libs/lospecs/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
(public_name easycrypt.lospecs)
(flags (:standard -open Batteries))
(modules :standard \ lospecs_test)
(preprocess (pps ppx_deriving.show))
(libraries batteries menhirLib)
)

Expand Down
Binary file removed libs/lospecs/lospecs_test.exe
Binary file not shown.
4 changes: 2 additions & 2 deletions libs/lospecs/lospecs_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ open Lospecs

(* -------------------------------------------------------------------- *)
let _ =
Format.printf "%a@." Ptree.pp_program (Io.parse IO.stdin)
Format.printf "%a@." Ptree.pp_pprogram (Io.parse IO.stdin)

(* -------------------------------------------------------------------- *)
module List : sig
Expand Down Expand Up @@ -45,7 +45,7 @@ end


(* -------------------------------------------------------------------- *)
let () =
let main () =
let open Deps in

let f0 = copy ~offset:(0 * 256) ~size:256 "a" in (* f0 = a[u256 4*i ]; *)
Expand Down
13 changes: 10 additions & 3 deletions libs/lospecs/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
| (* empty *) { () }
*)

%inline vname:
| x=IDENT
{ x }

%inline wname:
| x=IDENT t=wtype
| x=vname t=wtype
{ (x, t) }

%inline wtype:
Expand All @@ -61,8 +65,11 @@ fname:
{ (f, Some (List.map (fun x -> W x) p)) }

sexpr:
| f=fname args=parens(list0(expr, COMMA))?
{ PEApp (f, Option.default [] args) }
| x=vname
{ PEVar x }

| f=fname args=parens(list0(expr, COMMA))
{ PEApp (f, args) }

| e=parens(expr)
{ PEParens e }
Expand Down
49 changes: 10 additions & 39 deletions libs/lospecs/ptree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,49 @@
(* Typing hint: env[symbol] *)
type symbol =
string
[@@deriving show]

type pword =
W of int
[@@deriving show]

type ptype =
| Unsigned
| Signed
| Word of int

let pp_type (fmt: Format.formatter) (t: ptype) =
match t with
| Unsigned -> Format.fprintf fmt "unsigned"
| Signed -> Format.fprintf fmt "signed"
| Word x -> Format.fprintf fmt "W%@%d" x
[@@deriving show]

type parg =
symbol * pword

let pp_arg (fmt: Format.formatter) (arg: parg) =
let (name, len) = arg in
let W len = len in
Format.fprintf fmt "@[%s@%d@]" name len
[@@deriving show]

type pargs =
parg list

let pp_args (fmt: Format.formatter) (args: pargs) =
Format.pp_print_list ~pp_sep:(fun out () -> Format.fprintf out ",@ ") pp_arg fmt args
[@@deriving show]

type pfname =
symbol * pword list option

let pp_fname (fmt: Format.formatter) (fname: pfname) =
let name, ns = fname in
match ns with
| Some ns -> Format.fprintf fmt "@[%s<%a>@]" name (fun out l -> Format.pp_print_list ~pp_sep:(fun out () -> Format.fprintf out ",") (fun fmt w -> let W w = w in Format.fprintf fmt "%d" w) out l) ns
| None -> Format.fprintf fmt "@[%s@]" name
[@@deriving show]

type pexpr =
| PEParens of pexpr (* type: typeof(pexpr) *)
| PEVar of symbol
| PEInt of int (* type: int *)
| PECast of ptype * pexpr (* type: ptype (check if convertible) *)
| PEFun of pargs * pexpr (* type: add ret type to env (calculate on the fly?) *)
| PELet of (symbol * pexpr) * pexpr (* typeof: second pexpr with symbol added to env as type of first expr *)
| PESlice of pexpr * (pexpr * pexpr * pexpr option) (* type: same type as first expr *)
| PEApp of pfname * pexpr list (* if args match fun type then fun ret else type error *)

let rec pp_expr (fmt: Format.formatter) (expr: pexpr) =
match expr with
| PEParens e -> Format.fprintf fmt "@[(%a)@]" pp_expr e
| PEInt n -> Format.fprintf fmt "%d" n
| PECast (t, e) -> Format.fprintf fmt "@[(%a) %a@]" pp_type t pp_expr e
| PEFun (a, e) -> Format.fprintf fmt "@[f (%a) -> %a@]" pp_args a pp_expr e
| PELet ((s, e1), e2) -> Format.fprintf fmt "@[let %s = %a in @, %a@]" s pp_expr e1 pp_expr e2
| PESlice (e1, (e2, e3, eo)) -> ( match eo with
| Some e4 -> Format.fprintf fmt "@[%a[%a:%a:%a]@]" pp_expr e1 pp_expr e2 pp_expr e3 pp_expr e4
| None -> Format.fprintf fmt "@[%a[%a:%a]@]" pp_expr e1 pp_expr e2 pp_expr e3 )
| PEApp (fn, el) -> Format.fprintf fmt "@[%a(%a)@]" pp_fname fn (Format.pp_print_list ~pp_sep:(fun out () -> Format.fprintf out ",@ ") pp_expr) el
[@@deriving show]

type pdef = {
name : symbol;
args : pargs;
rty : pword;
body : pexpr;
}

let pp_def (fmt: Format.formatter) (def: pdef) =
let W rty = def.rty in
Format.fprintf fmt "@[%s : (%a) @. rty: %d @. %a @]" def.name pp_args def.args rty pp_expr def.body
[@@deriving show]

type pprogram =
pdef list

let pp_program (fmt: Format.formatter) (prog: pprogram) =
Format.pp_print_list ~pp_sep:(fun out () -> Format.fprintf out "@.@.") pp_def fmt prog
[@@deriving show]

0 comments on commit 234a59c

Please sign in to comment.