Skip to content

Commit

Permalink
anormal done
Browse files Browse the repository at this point in the history
  • Loading branch information
y-tak6 committed Jun 12, 2024
1 parent f4c08be commit c4b25d3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
44 changes: 28 additions & 16 deletions src/anormal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,6 @@ let application_mut mut = function
| Bop _ | HashFind | Caller -> Abi.stronger_mutability View mut
| _ -> Abi.stronger_mutability Nonpayable mut

let rename_cexp rename e mut =
match e with
| AVal (Var id) -> (
match List.find_opt (fun (x, _) -> x = id) rename with
| Some (_, ids) -> (ATuple (List.map (fun x -> Var x) ids), mut)
| None -> (AVal (Var id), mut))
| AApp (f, args, t) ->
let mut = application_mut mut f in
(AApp (f, rename_avals rename args, t), mut)
| ATuple el -> (ATuple (rename_avals rename el), mut)
| AIf _ -> assert false
| _ -> (e, mut)

let cexp_to_exp e =
match e with
| AVal v -> Rexp (RVal v)
Expand All @@ -54,11 +41,33 @@ let cexp_to_exp e =
| ATuple el -> Rexp (RTuple el)
| AIf _ -> assert false

let rec remove_tuple rename e mut =
let rename_cexp rename e mut =
match e with
| AVal (Var id) -> (
match List.find_opt (fun (x, _) -> x = id) rename with
| Some (_, ids) -> (ATuple (List.map (fun x -> Var x) ids), mut)
| None -> (AVal (Var id), mut))
| AApp (f, args, t) ->
let mut = application_mut mut f in
(AApp (f, rename_avals rename args, t), mut)
| ATuple el -> (ATuple (rename_avals rename el), mut)
| AIf (v, e1, e2) ->
let v2 = match v with
| Var id -> (
let v2' = match List.find_opt (fun (x, _) -> x = id) rename with
| Some (_, ids) -> (match ids with [v] -> Var v | _ -> assert false)
| None -> Var id in v2')
| _ -> v in
(AIf (v2, e1, e2), mut)
| _ -> (e, mut)

let rec remove_tuple rename e mut : exp * Abi.state_mutability =
match e with
| ACexp e' ->
let e, mut = rename_cexp rename e' mut in
(cexp_to_exp e, mut)
(match e with
| AIf _ -> (cexp_to_exp e, mut)
| _ -> (cexp_to_exp e, mut))
| ASeq (e1, e2) -> (
match rename_cexp rename e1 mut with
| AApp (f, args, _), mut ->
Expand All @@ -80,7 +89,10 @@ let rec remove_tuple rename e mut =
(gen_tuple_let (vars, el), mut)
| AVal arg -> (Letin (vars, LVal arg, e2'), mut)
| AApp (f, args, _) -> (Letin (vars, LApp (f, args), e2'), mut)
| AIf _ -> assert false)
| AIf (v, e1'', e2'') ->
let e3, mut1 = remove_tuple rename e1'' mut in
let e4, mut2 = remove_tuple rename e2'' mut in
(Letin (vars, LIf(v, e3, e4), e2'), Abi.stronger_mutability mut1 mut2))

let normalize { name = func_name; arg_pats = args; body; mutability = mut } =
let renames, args =
Expand Down
9 changes: 3 additions & 6 deletions src/anormal_ir.ml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let pdot_to_aval p s =
| _ -> assert false

(* The first argument p is a storage. To check whether the storage changes, it is needed.
The last argument k is a continue. A first argument of k is hole, and a first element of a return value is AST with the hole.*)
The last argument k is a continuation. A first argument of k is hole, and a first element of a return value is AST with the hole.*)
let rec normalize_aux p { exp_desc = e; exp_type = t; _ } k :aexp * bool=
match e with
| Texp_ident (Pident s, _, _) ->
Expand Down Expand Up @@ -105,13 +105,10 @@ let rec normalize_aux p { exp_desc = e; exp_type = t; _ } k :aexp * bool=
let a, b = normalize_aux p e2 (fun (x, _, b) -> (ACexp x, b)) in
let e3' = match e3 with Some e -> e | _ -> assert false in
let a2, b2 = normalize_aux p e3' (fun (x, _, b) -> (ACexp x, b)) in
normalize_name e1 (fun x -> k (AIf(x, a, a2), t, b && b2 ))
let a, b = normalize_name e1 (fun x -> k (AIf(x, a, a2), t, b && b2 )) in
a, b
| _ -> assert false

(* | A.If(e1,e2,e3) ->
normalize_name e1 (fun x ->
k (A.If(x, normalize e2 id, normalize e3 id))) *)

(* when a new variable is needed *)
and normalize_name e k =
normalize_aux None e (fun (e', t, _) ->
Expand Down
26 changes: 17 additions & 9 deletions src/normalized_ast.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
open Normalized_common_ast

type letexp = LVal of value | LApp of (value * value list)
exception Whoo of int

type resexp = RVal of value | RTuple of value list

type exp =
type letexp = LVal of value | LApp of (value * value list) | LIf of value * exp * exp

and exp =
| Rexp of resexp
| Seq of letexp * exp
| Letin of string list * letexp * exp
| If of value * exp * exp


type decl = {
name : Ident.t;
Expand All @@ -15,27 +20,30 @@ type decl = {
mutability : Abi.state_mutability;
}

let string_of_letexp = function
| LVal v -> string_of_value v
| LApp (f, xs) ->
string_of_value f
^ List.fold_left (fun acc x -> acc ^ " " ^ string_of_value x) "" xs

let string_of_resexp = function
| RVal v -> string_of_value v
| RTuple vs ->
"("
^ List.fold_left (fun acc x -> acc ^ ", " ^ string_of_value x) "" vs
^ ")"

let rec string_of_exp e =
let rec string_of_letexp = function
| LVal v -> string_of_value v
| LApp (f, xs) ->
string_of_value f
^ List.fold_left (fun acc x -> acc ^ " " ^ string_of_value x) "" xs
| LIf (v, e1, e2) -> "if " ^ string_of_value v ^ " then " ^ string_of_exp e1 ^ " else " ^ string_of_exp e2
and string_of_exp e =
match e with
| Rexp e -> string_of_resexp e
| Seq (e1, e2) -> string_of_letexp e1 ^ "; " ^ string_of_exp e2
| Letin (vars, e1, e2) ->
"let"
^ List.fold_left (fun acc x -> acc ^ ", " ^ x) "" vars
^ " = " ^ string_of_letexp e1 ^ " in " ^ string_of_exp e2
| If (v, e1, e2) -> "if " ^ string_of_value v ^ " then " ^ string_of_exp e1 ^ " else " ^ string_of_exp e2



let string_of_decl
{ name = func_name; arg_ids = args; body = e; mutability = mut } =
Expand Down
8 changes: 5 additions & 3 deletions src/normalized_ast.mli
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
open Normalized_common_ast

(** expressions that can be placed at the right-hand side of a let-binding *)
type letexp = LVal of value | LApp of (value * value list)


(** expressions that can be placed at the last of let-binding sequences *)
type resexp = RVal of value | RTuple of value list
(** expressions that can be placed at the right-hand side of a let-binding *)
type letexp = LVal of value | LApp of (value * value list) | LIf of value * exp * exp

type exp =
and exp =
| Rexp of resexp
| Seq of letexp * exp
| Letin of string list * letexp * exp
| If of value * exp * exp

(** a function declaration with stateMutability field of ABI *)
type decl = {
Expand Down

0 comments on commit c4b25d3

Please sign in to comment.