Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/oscar/formatter' into phated/for…
Browse files Browse the repository at this point in the history
…matter-record-pattern
  • Loading branch information
phated committed Feb 19, 2024
2 parents 7431b85 + 3811390 commit 5ea244f
Show file tree
Hide file tree
Showing 30 changed files with 1,009 additions and 614 deletions.
54 changes: 46 additions & 8 deletions compiler/src/formatting/fmt.re
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ let needs_grouping = (~parent, ~side: infix_side, expr) => {
} else {
FormatterGrouping;
}
| (PExpConstant(PConstNumber(PConstNumberRational(_, _))), _)
| (PExpConstant(PConstNumber(PConstNumberRational(_))), _)
when op_precedence('/') <= precedence(parent) =>
ParenGrouping
| _ => FormatterGrouping
Expand All @@ -270,7 +270,7 @@ let has_disable_formatting_comment = (~comment_tree, loc: Location.t) => {
type formatter = {
print_original_code: (formatter, Location.t) => Doc.t,
print_infix_prefix_op: (formatter, expression) => Doc.t,
print_constant: (formatter, ~loc: Location.t, constant) => Doc.t,
print_constant: (formatter, constant) => Doc.t,
print_punnable_pattern: (formatter, (loc(Identifier.t), pattern)) => Doc.t,
print_lambda_argument: (formatter, lambda_argument) => Doc.t,
print_pattern: (formatter, pattern) => Doc.t,
Expand Down Expand Up @@ -367,8 +367,48 @@ let print_infix_prefix_op = (fmt, expr) => {
};
};

let print_constant = (fmt, ~loc, constant) => {
fmt.print_original_code(fmt, loc);
let print_constant = (fmt, constant) => {
switch (constant) {
| PConstNumber(PConstNumberInt({txt: value})) => string(value)
| PConstNumber(PConstNumberFloat({txt: value})) => string(value)
| PConstNumber(PConstNumberRational({numerator, slash, denominator})) =>
string(numerator.txt)
++ fmt.print_comment_range(
~lead=space,
~trail=space,
numerator.loc,
slash,
)
++ string("/")
++ fmt.print_comment_range(
~lead=space,
~trail=space,
slash,
denominator.loc,
)
++ string(denominator.txt)
| PConstInt8({txt: value})
| PConstUint8({txt: value})
| PConstInt16({txt: value})
| PConstUint16({txt: value})
| PConstInt32({txt: value})
| PConstUint32({txt: value})
| PConstInt64({txt: value})
| PConstUint64({txt: value})
| PConstFloat32({txt: value})
| PConstFloat64({txt: value})
| PConstWasmI32({txt: value})
| PConstWasmI64({txt: value})
| PConstWasmF32({txt: value})
| PConstWasmF64({txt: value})
| PConstBigInt({txt: value})
| PConstRational({txt: value})
| PConstBytes({txt: value})
| PConstString({txt: value})
| PConstChar({txt: value}) => string(value)
| PConstBool(value) => string(value ? "true" : "false")
| PConstVoid => string("void")
};
};

let print_punnable_pattern =
Expand Down Expand Up @@ -565,8 +605,7 @@ let print_pattern = (fmt, {ppat_desc, ppat_loc}) => {
typ.ptyp_loc,
)
++ fmt.print_type(fmt, typ)
| PPatConstant(constant) =>
fmt.print_constant(fmt, ~loc=ppat_loc, constant)
| PPatConstant(constant) => fmt.print_constant(fmt, constant)
| PPatRecord(pats, closed_flag) =>
braces(
indent(
Expand Down Expand Up @@ -1300,8 +1339,7 @@ let print_expression =
++ (
switch (expr.pexp_desc) {
| PExpId({txt: ident}) => fmt.print_identifier(fmt, ident)
| PExpConstant(constant) =>
fmt.print_constant(fmt, ~loc=expr.pexp_loc, constant)
| PExpConstant(constant) => fmt.print_constant(fmt, constant)
| PExpConstruct({txt: ident, loc: ident_loc}, cstr_expr) =>
fmt.print_identifier(fmt, ident)
++ (
Expand Down
69 changes: 50 additions & 19 deletions compiler/src/parsing/ast_helper.re
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ let record_pattern_info = record_pats =>
([], Asttypes.Closed),
);

// This normalizes STRING items in the parsetree that aren't constants so we don't need
// to scatter them throughout the rest of the compiler.
let normalize_string = (~loc, item) => {
switch (Grain_utils.Literals.conv_string(item.txt)) {
| Ok(i) => {loc: item.loc, txt: i}
| Error(msg) => raise(SyntaxError(loc, msg))
};
};

module Number = {
let rational = (numerator, slash, denominator) => {
PConstNumberRational({numerator, slash, denominator});
};
};

module Constant = {
let bytes = b => PConstBytes(b);
let string = s => PConstString(s);
Expand All @@ -50,25 +65,18 @@ module Constant = {
let int16 = i => PConstInt16(i);
let int32 = i => PConstInt32(i);
let int64 = i => PConstInt64(i);
let uint8 = (is_neg, i) => PConstUint8(is_neg, i);
let uint16 = (is_neg, i) => PConstUint16(is_neg, i);
let uint32 = (is_neg, i) => PConstUint32(is_neg, i);
let uint64 = (is_neg, i) => PConstUint64(is_neg, i);
let uint8 = i => PConstUint8(i);
let uint16 = i => PConstUint16(i);
let uint32 = i => PConstUint32(i);
let uint64 = i => PConstUint64(i);
let float32 = f => PConstFloat32(f);
let float64 = f => PConstFloat64(f);
let wasmi32 = i => PConstWasmI32(i);
let wasmi64 = i => PConstWasmI64(i);
let wasmf32 = f => PConstWasmF32(f);
let wasmf64 = f => PConstWasmF64(f);
let bigint = i => PConstBigInt(i);
let rational = r => {
let (n, d) =
switch (String.split_on_char('/', r)) {
| [n, d] => (n, d)
| _ => failwith("Impossible: rational literal without forward slash")
};
PConstRational(n, d);
};
let rational = r => PConstRational(r);
let bool = b => PConstBool(b);
let void = PConstVoid;
};
Expand Down Expand Up @@ -349,15 +357,20 @@ module Expression = {
);
switch (f, a, b) {
| (
{pexp_desc: PExpId({txt: IdentName({txt: "/"})})},
{pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(x)))},
{pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(y)))},
{pexp_desc: PExpId({txt: IdentName({txt: "/", loc: slash_loc})})},
{
pexp_desc: PExpConstant(PConstNumber(PConstNumberInt(numerator))),
},
{
pexp_desc:
PExpConstant(PConstNumber(PConstNumberInt(denominator))),
},
) =>
constant(
~loc,
~core_loc,
~attributes?,
PConstNumber(PConstNumberRational(x, y)),
Constant.number(Number.rational(numerator, slash_loc, denominator)),
)
| _ =>
mk(
Expand Down Expand Up @@ -426,14 +439,18 @@ module Toplevel = {

module PrimitiveDescription = {
let mk = (~loc, ~ident, ~name, ()) => {
{pprim_ident: ident, pprim_name: name, pprim_loc: loc};
{
pprim_ident: ident,
pprim_name: normalize_string(~loc, name),
pprim_loc: loc,
};
};
};

module ValueDescription = {
let mk = (~loc, ~mod_, ~name, ~alias, ~typ, ()) => {
{
pval_mod: mod_,
pval_mod: normalize_string(~loc, mod_),
pval_name: name,
pval_name_alias: alias,
pval_type: typ,
Expand All @@ -456,7 +473,11 @@ module MatchBranch = {

module IncludeDeclaration = {
let mk = (~loc, path, alias) => {
{pinc_alias: alias, pinc_path: path, pinc_loc: loc};
{
pinc_alias: alias,
pinc_path: normalize_string(~loc, path),
pinc_loc: loc,
};
};
};

Expand Down Expand Up @@ -504,3 +525,13 @@ module ModuleDeclaration = {
{pmod_name: name, pmod_stmts: stmts, pmod_loc: loc};
};
};

module Attribute = {
let mk = (~loc, attr_name, attr_args) => {
{
Asttypes.attr_name,
attr_args: List.map(normalize_string(~loc), attr_args),
attr_loc: loc,
};
};
};
46 changes: 27 additions & 19 deletions compiler/src/parsing/ast_helper.rei
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,31 @@ type id = loc(Identifier.t);
type str = loc(string);
type loc = Location.t;

module Number: {
let rational: (str, Location.t, str) => number_type;
};

module Constant: {
let bytes: string => constant;
let string: string => constant;
let char: string => constant;
let bytes: str => constant;
let string: str => constant;
let char: str => constant;
let number: number_type => constant;
let int8: string => constant;
let int16: string => constant;
let int32: string => constant;
let int64: string => constant;
let uint8: (bool, string) => constant;
let uint16: (bool, string) => constant;
let uint32: (bool, string) => constant;
let uint64: (bool, string) => constant;
let float32: string => constant;
let float64: string => constant;
let wasmi32: string => constant;
let wasmi64: string => constant;
let wasmf32: string => constant;
let wasmf64: string => constant;
let bigint: string => constant;
let rational: string => constant;
let int8: str => constant;
let int16: str => constant;
let int32: str => constant;
let int64: str => constant;
let uint8: str => constant;
let uint16: str => constant;
let uint32: str => constant;
let uint64: str => constant;
let float32: str => constant;
let float64: str => constant;
let wasmi32: str => constant;
let wasmi64: str => constant;
let wasmf32: str => constant;
let wasmf64: str => constant;
let bigint: str => constant;
let rational: str => constant;
let bool: bool => constant;
let void: constant;
};
Expand Down Expand Up @@ -551,3 +555,7 @@ module LambdaArgument: {
module ModuleDeclaration: {
let mk: (~loc: loc, str, list(toplevel_stmt)) => module_declaration;
};

module Attribute: {
let mk: (~loc: loc, str, list(str)) => attribute;
};
Loading

0 comments on commit 5ea244f

Please sign in to comment.