Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
trdthg committed Aug 7, 2024
1 parent e070ec0 commit 822c9ee
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 234 deletions.
12 changes: 6 additions & 6 deletions sail_config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"fmt": {
"indent": 4,
"preserve_structure": false,
"line_width": 80,
"ribbon_width": 1.0
}
"fmt": {
"indent": 2,
"preserve_structure": false,
"line_width": 120,
"ribbon_width": 1.0
}
}
151 changes: 85 additions & 66 deletions src/lib/format_sail.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ let rec map_last f = function
let is_comment chunk = match chunk with Comment (_, _, _, _, _) | Doc_comment _ -> true | _ -> false
let is_block chunk = match chunk with Block (_, _) -> true | _ -> false
let is_match chunk = match chunk with Match _ -> true | _ -> false
let is_struct chunk = match chunk with Struct_update _ -> true | _ -> false
let is_tuple chunk = match chunk with Tuple _ -> true | _ -> false
let is_let chunk = match chunk with Binder (_, _, _, _) -> true | _ -> false
let is_delim chunk = match chunk with Delim _ | Opt_delim _ -> true | _ -> false
let is_binary chunk = match chunk with Binary _ -> true | _ -> false
let is_op chunk = match chunk with Binary _ -> true | Ternary _ | Infix_sequence _ -> true | _ -> false

let rec count_chunks_by_rule ?(ignore_rule = is_delim) chunks target_rule =
let rec go chunks target_count other_count =
Expand All @@ -97,6 +101,10 @@ let rec count_chunks_by_rule ?(ignore_rule = is_delim) chunks target_rule =
in
go chunks 0 0

let rec is_chunks_block_like chunks =
let chunks = List.of_seq (Queue.to_seq chunks) in
count_chunks_by_rule chunks (fun chunk -> is_block chunk || is_match chunk || is_tuple chunk) = (1, 0)

(* Remove additional (> 1) trailing newlines at the end of a string *)
let discard_extra_trailing_newlines s =
let len = String.length s in
Expand Down Expand Up @@ -508,6 +516,7 @@ module NoWrapChecker = struct
(* [foo with a = 1, b = 2] *)
check_chunks_nowrap ~opt [cq] && List.fold_left (fun res c -> res && check_no_wrap opt c) true cs
| Ternary (x, op1, y, op2, z) ->
(* with 37..36 = 0b00 *)
check_chunks_nowrap ~opt [x] && check_chunks_nowrap ~opt [y] && check_chunks_nowrap ~opt [z]
| Tuple (_, _, _, cqs) -> (* (1, 2) *) check_chunks_nowrap ~opt cqs
| _ -> false
Expand Down Expand Up @@ -604,32 +613,51 @@ module Make (Config : CONFIG) = struct
| Infix_sequence infix_chunks ->
let outer_prec = max_precedence infix_chunks in
let chunks_count = ref 0 in
let doc, _ =

let op = ref None in
let prefix = ref None in
let doc =
List.fold_left
(fun (acc, i) chunk ->
let doc =
match chunk with
| Infix_prefix op -> string op
| Infix_chunks chunks ->
chunks_count := !chunks_count + 1;
let doc = doc_chunks (opts |> atomic |> expression_like) chunks in
let doc = group doc in
if !chunks_count > 1 then group (ifflat (space ^^ doc) (hardline ^^ repeat indent space ^^ doc))
else doc
| Infix_op op -> space ^^ string op
in
(acc ^^ doc, i + 1)
(fun acc c ->
match c with
| Infix_prefix p ->
let p = String.trim p in
let p = if String.length p = 1 then p else p ^ " " in
prefix := Some (string p);
acc
| Infix_chunks cs ->
chunks_count := !chunks_count + 1;
let doc = doc_chunks (opts |> atomic |> expression_like) cs in
let doc = match !prefix with Some p -> p ^^ doc | None -> doc in
let doc =
match !op with
| Some op ->
let sep_op_chunk = ifflat space (hardline ^^ repeat indent space) in
space ^^ op ^^ sep_op_chunk ^^ group doc
| None -> doc
in
op := None;
prefix := None;
acc ^^ doc
| Infix_op o ->
op := Some (string o);
acc
)
(empty, 0) infix_chunks
empty infix_chunks
in
let doc = group doc in
if outer_prec > opts.precedence then parens doc else doc
| Binary (lhs, op, rhs) ->
let outer_prec, lhs_prec, rhs_prec, spacing = operator_precedence op in
let doc_l, nowrap_l = doc_chunks_nowrap_or_prefix (opts |> lhs_prec |> expression_like) lhs in
let doc_r, nowrap_r = doc_chunks_nowrap_or_prefix ~sep_on_op:true (opts |> rhs_prec |> expression_like) rhs in
let doc_r, nowrap_r = doc_chunks_nowrap_or_prefix (opts |> rhs_prec |> expression_like) rhs in
let padding_l = if string op = string ".." then empty else space in
let padding_r = if string op = string ".." then empty else ifflat space (hardline ^^ repeat indent space) in

let padding_r =
if string op = string ".." then empty
else if not nowrap_r then space
else ifflat space (if sep_on_op then hardline ^^ repeat indent space else space)
in
let doc = doc_l ^^ padding_l ^^ string op ^^ padding_r ^^ group doc_r in
let doc = group doc in
if outer_prec > opts.precedence then parens doc else doc
Expand All @@ -648,7 +676,7 @@ module Make (Config : CONFIG) = struct
if outer_prec > opts.precedence then parens doc else doc
| If_then_else (bracing, i, t, e) ->
let insert_braces = opts.statement || bracing.then_brace || bracing.else_brace in
let i_doc, i_nowrap = doc_chunks_nowrap_or_prefix (opts |> nonatomic |> expression_like) i in
let i_doc, i_nowrap = doc_chunks_nowrap_or_prefix ~sep_on_op:true (opts |> nonatomic |> expression_like) i in
(* check_nowrap with strict mode here *)
let check_opt = NoWrapChecker.default_opt |> NoWrapChecker.strict in
let t_nowrap = NoWrapChecker.check_chunks_nowrap ~opt:check_opt [t] in
Expand All @@ -668,8 +696,9 @@ module Make (Config : CONFIG) = struct
}
*)
let nowrap = i_nowrap && t_nowrap && e_nowrap in
Printf.printf "nowrap = %b %b %b\n" i_nowrap t_nowrap e_nowrap;
let block_nogroup = true in
let t_doc, t_wrap =
let t_doc, _ =
if insert_braces && (not preserve_structure) && not bracing.then_brace then
(doc_chunk opts (Block (true, [t])), false)
else doc_chunks_nowrap_or_prefix ~block_nogroup ~nowrap (opts |> nonatomic |> expression_like) t
Expand Down Expand Up @@ -704,29 +733,27 @@ module Make (Config : CONFIG) = struct
let sep_if_else = if bracing.then_brace then space else hardline in
let sep_if_then = ifflat (if i_nowrap then space else hardline) hardline in
let res =
if nowrap then (
Printf.printf "不换行\n";
if nowrap then
(* Printf.printf "不换行\n"; *)
ifflat
(doc_i ^^ sep_if_then ^^ doc_t ^^ space ^^ doc_e)
( if bracing.then_brace && i_nowrap then group (doc_i ^^ sep_if_then) ^^ doc_t ^^ sep_then_else ^^ doc_e
else doc_i ^^ nest indent (hardline ^^ doc_t ^^ sep_then_else ^^ doc_e)
)
|> atomic_parens opts
)
else (
Printf.printf "换行\n";
else
(* Printf.printf "换行\n"; *)
ifflat
(doc_i ^^ sep_if_then ^^ doc_t ^^ space ^^ doc_e)
( if bracing.then_brace && i_nowrap then doc_i ^^ nest 0 (space ^^ doc_t ^^ sep_then_else ^^ doc_e)
( if bracing.then_brace && i_nowrap then group (doc_i ^^ sep_if_then) ^^ doc_t ^^ sep_then_else ^^ doc_e
else if not i_nowrap then doc_i ^^ nest 0 (hardline ^^ doc_t ^^ sep_then_else ^^ doc_e)
else doc_i ^^ nest indent (hardline ^^ doc_t ^^ sep_then_else ^^ doc_e)
)
|> atomic_parens opts
)
in
group res
| If_then (bracing, i, t) ->
let i, i_nowrap = doc_chunks_nowrap_or_prefix (opts |> nonatomic |> expression_like) i in
let i, i_nowrap = doc_chunks_nowrap_or_prefix ~sep_on_op:true (opts |> nonatomic |> expression_like) i in
let t, t_nowrap =
if opts.statement && (not preserve_structure) && not bracing then (doc_chunk opts (Block (true, [t])), false)
else doc_chunks_nowrap_or_prefix (opts |> nonatomic |> expression_like) t
Expand Down Expand Up @@ -862,26 +889,18 @@ module Make (Config : CONFIG) = struct
(fun no_semi chunks -> doc_block_exp_chunks (opts |> nonatomic |> statement_like) no_semi chunks)
exps
in
let sep =
if always_hardline || List.exists snd exps then hardline
else (
Printf.printf "使用换行\n";
space
)
in
let sep = if always_hardline || List.exists snd exps then hardline else space in
let exps = List.map fst exps in
surround_hardline ~nogroup:block_nogroup always_hardline indent 1 (char '{') (separate sep exps) (char '}')
|> atomic_parens opts
| Block_binder (binder, x, y) ->
if can_hang y then
separate space
[string (binder_keyword binder); doc_chunks (atomic opts) x; char '='; doc_chunks (nonatomic opts) y]
else
group (separate space [string (binder_keyword binder); doc_chunks (atomic opts) x; char '='])
^^ nest indent (hardline ^^ doc_chunks (nonatomic opts) y)
(* let y, _ = doc_chunks_nowrap_or_prefix ~sep_on_right:true (nonatomic opts) y in *)
let doc1 = group (separate space [string (binder_keyword binder); doc_chunks (atomic opts) x; char '=']) in
let doc2, _ = doc_chunks_nowrap_or_prefix ~sep_on_op:true (nonatomic opts) y in
doc1 ^^ space ^^ doc2
| Binder (binder, x, y, z) ->
let x, _ = doc_chunks_nowrap_or_prefix (atomic opts) x in
let y, _ = doc_chunks_nowrap_or_prefix (atomic opts) y in
let y, _ = doc_chunks_nowrap_or_prefix ~sep_on_op:true (atomic opts) y in
let doc = separate space [string (binder_keyword binder); x; char '='; y; string "in"; hardline] in
Queue.fold
(fun acc chunk ->
Expand All @@ -900,25 +919,28 @@ module Make (Config : CONFIG) = struct
| Foreach loop ->
let nowrap = false in
let to_keyword = string (if loop.decreasing then "downto" else "to") in
string "foreach" ^^ space
^^ group
(surround indent 0 (char '(')
(separate (break 1)
([
doc_chunks (opts |> atomic) loop.var;
string "from" ^^ space ^^ doc_chunks (opts |> atomic |> expression_like) loop.from_index;
to_keyword ^^ space ^^ doc_chunks (opts |> atomic |> expression_like) loop.to_index;
]
@
match loop.step with
| Some step -> [string "by" ^^ space ^^ doc_chunks (opts |> atomic |> expression_like) step]
| None -> []
)
)
(char ')')
)
^^ space
^^ group (doc_chunks ~nowrap (opts |> nonatomic |> statement_like) loop.body)
let doc =
string "foreach" ^^ space
^^ group
(surround indent 0 (char '(')
(separate (break 1)
([
doc_chunks (opts |> atomic) loop.var;
string "from" ^^ space ^^ doc_chunks (opts |> atomic |> expression_like) loop.from_index;
to_keyword ^^ space ^^ doc_chunks (opts |> atomic |> expression_like) loop.to_index;
]
@
match loop.step with
| Some step -> [string "by" ^^ space ^^ doc_chunks (opts |> atomic |> expression_like) step]
| None -> []
)
)
(char ')')
)
in
let body, nowrap = doc_chunks_nowrap_or_prefix ~nowrap (opts |> nonatomic |> statement_like) loop.body in
if is_chunks_block_like loop.body then doc ^^ space ^^ group body
else doc ^^ nest indent (hardline ^^ group body)
| While loop ->
let measure =
match loop.termination_measure with
Expand Down Expand Up @@ -970,10 +992,7 @@ module Make (Config : CONFIG) = struct
(fun doc chunk -> doc ^^ doc_chunk ~sep_on_op ~toplevel ~nowrap ~ungroup_tuple ~block_nogroup opts chunk)
empty chunks
in
let rec count_chunks_block_like chunks =
count_chunks_by_rule chunks (fun chunk -> is_block chunk || is_match chunk)
in
let no_prefix = count_chunks_block_like (List.of_seq (Queue.to_seq chunks)) = (1, 0) in
let no_prefix = is_chunks_block_like chunks in
let doc = if not no_prefix then group doc else doc in
(* test if can nowrap with optional strict mode *)
let can_nowrap = NoWrapChecker.check_chunks_nowrap [chunks] in
Expand Down Expand Up @@ -1051,8 +1070,8 @@ module Make (Config : CONFIG) = struct
let doc = splice_into_doc chunks empty in
(group doc, !requires_hardline)

and doc_chunks ?(ungroup_tuple = false) ?(nowrap = true) opts chunks =
Queue.fold (fun doc chunk -> doc ^^ doc_chunk ~nowrap ~ungroup_tuple opts chunk) empty chunks
and doc_chunks ?(ungroup_tuple = false) ?(nowrap = true) ?(sep_on_op = false) opts chunks =
Queue.fold (fun doc chunk -> doc ^^ doc_chunk ~sep_on_op ~nowrap ~ungroup_tuple opts chunk) empty chunks

let to_string doc =
let b = Buffer.create 1024 in
Expand Down
104 changes: 0 additions & 104 deletions test/format/default/comments.sail

This file was deleted.

7 changes: 4 additions & 3 deletions test/format/default/demo.sail
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

let a =
1 // memory
register mtimecmp : bits(64) /* memory */
function a () = {
foreach (i from 0 to (sizeof(xlen) - 8) by 8)
result[(i + 7)..i] = rs1_val[(sizeof(xlen) - i - 1)..(sizeof(xlen) - i - 8)]
}
Loading

0 comments on commit 822c9ee

Please sign in to comment.