Skip to content

Commit 3fa74e5

Browse files
committed
More
1 parent 58f8c8a commit 3fa74e5

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

imlformat/iml-prettier.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { group, indent, indentIfBreak, dedent, join, ifBreak, breakParent, line,
66

77
import { iml2json } from './iml2json.bc';
88
import { assert } from 'node:console';
9+
import { DEFAULT_ENCODING } from 'node:crypto';
910

1011
export const languages = [
1112
{
@@ -330,11 +331,16 @@ function print_string_loc(node: AST, options: Options): Doc {
330331
}
331332

332333
function print_attributes(node: AST, level: number, options: Options): Doc {
334+
let filter = [
335+
"ocaml.comment",
336+
"imandra_verify", "imandra_instance", "imandra_theorem",
337+
"imandra_eval", "imandra_axiom", "imandra_rule_spec"
338+
];
333339
return join(line, node.map(x => {
334-
if (x.attr_name.txt != "ocaml.comment")
335-
return print_attribute(x, level, options);
336-
else
340+
if (filter.find(y => y == x.attr_name.txt))
337341
return [];
342+
else
343+
return print_attribute(x, level, options);
338344
}));
339345
}
340346

@@ -344,12 +350,8 @@ function print_comment(node: AST, options: Options) {
344350
}
345351

346352
function print_comments(node: AST, options: Options): Doc {
347-
return join(line, node.map(x => {
348-
if (x.attr_name.txt == "ocaml.comment")
349-
return print_comment(x, options);
350-
else
351-
return [];
352-
}));
353+
let filtered = node.filter(x => x.attr_name.txt == "ocaml.comment");
354+
return join(line, filtered.map(x => print_comment(x, options)));
353355
}
354356

355357
function print_arg_label(node: AST, options: Options): Doc {
@@ -1041,7 +1043,7 @@ function print_expression_desc(node: AST, options: Options, need_pars: boolean):
10411043
case "Pexp_match":
10421044
// | Pexp_match of expression * case list
10431045
// (** [match E0 with P1 -> E1 | ... | Pn -> En] *)
1044-
const cs = join([line, "|", " "], args[1].map(x => {
1046+
const cs = join([line, "| "], args[1].map(x => {
10451047
return f([print_pattern(x.pc_lhs, options), " ", "->", line, print_expression(x.pc_rhs, options, true)]);
10461048
}));
10471049
return g(["match", indent([line, print_expression(args[0], options, true), line]), "with", line,
@@ -1361,7 +1363,7 @@ function print_module_binding(node: AST, options: Options): Doc {
13611363

13621364
function get_attr_payload_string(node: AST): Doc {
13631365
// Comments have special string payloads without quotes. Sigh.
1364-
return join(line, node.attr_payload[1][0].pstr_desc[1].pexp_desc[1].pconst_desc[1].split(" "));
1366+
return node.attr_payload[1][0].pstr_desc[1].pexp_desc[1].pconst_desc[1];
13651367
}
13661368

13671369
function print_attribute(node: AST, level: number, options: Options): Doc {
@@ -1461,13 +1463,10 @@ function print_structure_item_desc(node: AST, options: Options): Doc {
14611463
let r: Doc[] = [];
14621464
const comments = ifnonempty(line, print_comments(args[1], options));
14631465
if (args.length > 1 && has_attribute(args[1], "imandra_eval"))
1464-
r = [comments, "eval", line, print_expression(args[0], options, false)];
1466+
r = [comments, "eval", line, "(", softline, print_expression(args[0], options, false), softline, ")"];
14651467
else
14661468
r = [comments, print_expression(args[0], options, false)];
1467-
if (args[1].length == 0)
1468-
return [r];
1469-
else
1470-
return [r, line, print_attributes(args[1], 3, options)];
1469+
return f([r, ifnonempty(line, print_attributes(args[1], 3, options))]);
14711470
}
14721471
case "Pstr_value":
14731472
// | Pstr_value of rec_flag * value_binding list
@@ -1531,16 +1530,17 @@ function print_structure_item_desc(node: AST, options: Options): Doc {
15311530
// For function definitions we want to hoist the arguments
15321531
return g([comments, r,
15331532
f([indent([line, print_pattern(pvb.pvb_pat, options), line,
1534-
join(line, pvb.pvb_expr.pexp_desc[1].map(x => print_function_param(x, options))), line,
1535-
"=", line]),
1533+
join(line, pvb.pvb_expr.pexp_desc[1].map(x => print_function_param(x, options))),
1534+
]),
1535+
line, "=", line,
15361536
g([indent([print_function_body(pvb.pvb_expr.pexp_desc[3], options)]),
15371537
ifnonempty(hardline, print_attributes(attrs, 2, options)),])])]);
15381538
}
15391539
}
15401540
// Generic version
15411541
return g([comments, r, indent([line, join([line, "and", line], args[1].map(x => print_value_binding(x, options)))]),
15421542
ifnonempty(hardline, print_attributes(attrs, 2, options))]);
1543-
case "Pstr_primitive": // TODO
1543+
case "Pstr_primitive":
15441544
// | Pstr_primitive of value_description
15451545
// (** - [val x: T]
15461546
// - [external x: T = "s1" ... "sn" ]*)
@@ -1689,7 +1689,18 @@ function print(path: AstPath<Tree>, options: Options, print
16891689
) {
16901690
return join([hardline, hardline], path.node.top_defs.map(n => {
16911691
let q = print_toplevel_phrase(n, options);
1692-
// console.log(q);
1692+
if (false) {
1693+
if (n[0] == "Ptop_def") {
1694+
let src = get_source(n[1][0].pstr_loc, n[1][0].pstr_loc, options);
1695+
console.log(src);
1696+
}
1697+
else {
1698+
let src = get_source(n[1].pdir_loc, n[1].pdir_loc, options);
1699+
console.log(src);
1700+
}
1701+
console.log(n);
1702+
console.log(q);
1703+
}
16931704
return [q];
16941705
}
16951706
));

0 commit comments

Comments
 (0)