Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make exponentiation operator print with right associativity. #1678

Merged
merged 1 commit into from
Dec 4, 2017

Conversation

IwanKaramazow
Copy link
Contributor

@IwanKaramazow IwanKaramazow commented Dec 3, 2017

Fixes #1676

The exponentiation operator should be printed with right associativity.

let foo = (100. /. 2.) ** 2. +. (200. /. 2.) ** 2.;
let foo = 100. /. 2. ** 2. +. (200. /. 2.) ** 2.;
let foo = 100. /. 2. ** 2. +. 200. /. 2. ** 2.;

now compiles into

Math.pow(100 / 2, 2) + Math.pow(200 / 2, 2);
100 / Math.pow(2, 2) + Math.pow(200 / 2, 2);
var foo = 100 / Math.pow(2, 2) + 200 / Math.pow(2, 2);

with bucklescript, which is the correct output.

@jordwalke
Copy link
Member

Why was this not an issue before?
And to be clear, if you don't refmt there is no bug, correct?

@chenglou
Copy link
Member

chenglou commented Dec 4, 2017

Seems introduced in v3.0.0. Printer issue only

@IwanKaramazow
Copy link
Contributor Author

It's indeed printer only. Parser seems to parse with the correct precedence.
The parsetree looks the same, aside from a subtle difference between & .
But that might me from using different ocaml versions, I think. Can't re-compile reason 1 at the moment because opam...

/* input */
let foo = (100. /. 2.) ** 2. +. (200. /. 2.) ** 2.;

reason 1

[
  structure_item (test.re[1,0+0]..[1,0+51])
    Pstr_value Nonrec
    [
      <def>
        pattern (test.re[1,0+4]..[1,0+7])
          Ppat_var "foo" (test.re[1,0+4]..[1,0+7])
        expression (test.re[1,0+10]..[1,0+50])
          Pexp_apply
          expression ([0,0+-1]..[0,0+-1])
            Pexp_ident "+." ([0,0+-1]..[0,0+-1])
          [
            <arg>
            Nolabel
              expression (test.re[1,0+10]..[1,0+28])
                Pexp_apply
                expression ([0,0+-1]..[0,0+-1])
                  Pexp_ident "**" ([0,0+-1]..[0,0+-1])
                [
                  <arg>
                  Nolabel
                    expression (test.re[1,0+10]..[1,0+22])
                      Pexp_apply
                      expression ([0,0+-1]..[0,0+-1])
                        Pexp_ident "/." ([0,0+-1]..[0,0+-1])
                      [
                        <arg>
                        Nolabel
                          expression (test.re[1,0+11]..[1,0+15])
                            Pexp_constant PConst_float (100.,None)
                        <arg>
                        Nolabel
                          expression (test.re[1,0+19]..[1,0+21])
                            Pexp_constant PConst_float (2.,None)
                      ]
                  <arg>
                  Nolabel
                    expression (test.re[1,0+26]..[1,0+28])
                      Pexp_constant PConst_float (2.,None)
                ]
            <arg>
            Nolabel
              expression (test.re[1,0+32]..[1,0+50])
                Pexp_apply
                expression ([0,0+-1]..[0,0+-1])
                  Pexp_ident "**" ([0,0+-1]..[0,0+-1])
                [
                  <arg>
                  Nolabel
                    expression (test.re[1,0+32]..[1,0+44])
                      Pexp_apply
                      expression ([0,0+-1]..[0,0+-1])
                        Pexp_ident "/." ([0,0+-1]..[0,0+-1])
                      [
                        <arg>
                        Nolabel
                          expression (test.re[1,0+33]..[1,0+37])
                            Pexp_constant PConst_float (200.,None)
                        <arg>
                        Nolabel
                          expression (test.re[1,0+41]..[1,0+43])
                            Pexp_constant PConst_float (2.,None)
                      ]
                  <arg>
                  Nolabel
                    expression (test.re[1,0+48]..[1,0+50])
                      Pexp_constant PConst_float (2.,None)
                ]
          ]
    ]
]

reason 3

[
  structure_item (exp.re[1,0+0]..[1,0+51])
    Pstr_value Nonrec
    [
      <def>
        pattern (exp.re[1,0+4]..[1,0+7])
          Ppat_var "foo" (exp.re[1,0+4]..[1,0+7])
        expression (exp.re[1,0+10]..[1,0+50])
          Pexp_apply
          expression (exp.re[1,0+29]..[1,0+31])
            Pexp_ident "+." (exp.re[1,0+29]..[1,0+31])
          [
            <label> ""
              expression (exp.re[1,0+10]..[1,0+28])
                Pexp_apply
                expression (exp.re[1,0+23]..[1,0+25])
                  Pexp_ident "**" (exp.re[1,0+23]..[1,0+25])
                [
                  <label> ""
                    expression (exp.re[1,0+10]..[1,0+22])
                      Pexp_apply
                      expression (exp.re[1,0+16]..[1,0+18])
                        Pexp_ident "/." (exp.re[1,0+16]..[1,0+18])
                      [
                        <label> ""
                          expression (exp.re[1,0+11]..[1,0+15])
                            Pexp_constant Const_float 100.
                        <label> ""
                          expression (exp.re[1,0+19]..[1,0+21])
                            Pexp_constant Const_float 2.
                      ]
                  <label> ""
                    expression (exp.re[1,0+26]..[1,0+28])
                      Pexp_constant Const_float 2.
                ]
            <label> ""
              expression (exp.re[1,0+32]..[1,0+50])
                Pexp_apply
                expression (exp.re[1,0+45]..[1,0+47])
                  Pexp_ident "**" (exp.re[1,0+45]..[1,0+47])
                [
                  <label> ""
                    expression (exp.re[1,0+32]..[1,0+44])
                      Pexp_apply
                      expression (exp.re[1,0+38]..[1,0+40])
                        Pexp_ident "/." (exp.re[1,0+38]..[1,0+40])
                      [
                        <label> ""
                          expression (exp.re[1,0+33]..[1,0+37])
                            Pexp_constant Const_float 200.
                        <label> ""
                          expression (exp.re[1,0+41]..[1,0+43])
                            Pexp_constant Const_float 2.
                      ]
                  <label> ""
                    expression (exp.re[1,0+48]..[1,0+50])
                      Pexp_constant Const_float 2.
                ]
          ]
    ]
]

@chenglou
Copy link
Member

chenglou commented Dec 4, 2017

Merging! Thanks

@chenglou chenglou merged commit cd089bd into reasonml:master Dec 4, 2017
chenglou added a commit that referenced this pull request Dec 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants