cleanup hack in Ast.math_string_of
authorWalther Neuper <wneuper@ist.tugraz.at>
Wed, 02 Aug 2017 18:04:23 +0200
changeset 5194d01fce37569d
parent 5193 dfdf1b9349bb
child 5195 7151cbfc9f54
cleanup hack in Ast.math_string_of
isac-java/src/java-tests/isac/gui/mawen/TestUseCases.scala
isac-java/src/java-tests/isac/gui/mawen/syntax/TestAst.scala
isac-java/src/java/isac/gui/mawen/syntax/ast.scala
     1.1 --- a/isac-java/src/java-tests/isac/gui/mawen/TestUseCases.scala	Wed Aug 02 11:51:30 2017 +0200
     1.2 +++ b/isac-java/src/java-tests/isac/gui/mawen/TestUseCases.scala	Wed Aug 02 18:04:23 2017 +0200
     1.3 @@ -313,7 +313,7 @@
     1.4      assertEquals(ast,
     1.5        Appl(List(Constant("HOL.eq"), Appl(List(Variable("Program"), Constant("Simplify.Simplify"), Variable("t_t"))), Appl(List(Constant("Script.Repeat"), Appl(List(Constant("Script.Seq"), Appl(List(Constant("Script.Try"), Appl(List(Constant("Script.Rewrite'_Set"), Variable("klammern_aufloesen"), Constant("HOL.False"))))), Appl(List(Constant("Script.Seq"), Appl(List(Constant("Script.Try"), Appl(List(Constant("Script.Rewrite'_Set"), Variable("ordne_alphabetisch"), Constant("HOL.False"))))), Appl(List(Constant("Script.Seq"), Appl(List(Constant("Script.Try"), Appl(List(Constant("Script.Rewrite'_Set"), Variable("fasse_zusammen"), Constant("HOL.False"))))), Appl(List(Constant("Script.Try"), Appl(List(Constant("Script.Rewrite'_Set"), Variable("verschoenere"), Constant("HOL.False"))))))))))), Variable("t_t")))))
     1.6      )
     1.7 -    assertEquals(math_string_of(ast), "Program Simplify  t_t = Repeat Seq Try (Rewrite'_Set klammern_aufloesen False ) Seq Try (Rewrite'_Set ordne_alphabetisch False ) Seq Try (Rewrite'_Set fasse_zusammen False ) Try (Rewrite'_Set verschoenere False ) t_t")
     1.8 +    assertEquals(math_string_of(ast), "Program Simplify  t_t = Repeat (Seq (Try (Rewrite'_Set klammern_aufloesen False )) (Seq (Try (Rewrite'_Set ordne_alphabetisch False )) (Seq (Try (Rewrite'_Set fasse_zusammen False )) (Try (Rewrite'_Set verschoenere False ))))) t_t")
     1.9     
    1.10      println("\\--END isac.gui.mawen.TestUseCases#testUC_ifthenelse");
    1.11    }
     2.1 --- a/isac-java/src/java-tests/isac/gui/mawen/syntax/TestAst.scala	Wed Aug 02 11:51:30 2017 +0200
     2.2 +++ b/isac-java/src/java-tests/isac/gui/mawen/syntax/TestAst.scala	Wed Aug 02 18:04:23 2017 +0200
     2.3 @@ -109,7 +109,7 @@
     2.4    //test additional examples with BOX, CURSOR, GAP
     2.5    def test_BOX_CURSOR_GAP() {
     2.6      println("/--BEGIN isac.gui.mawen.asttrans.TestAst#test_BOX_CURSOR_GAP");
     2.7 -    println(Ast.math_string_of(TestDATAeditor.nest_CURSOR_BOX_3B))
     2.8 +
     2.9      assertEquals("aaa + bbb", Ast.math_string_of(TestDATAeditor.nest_BOX_1))   
    2.10      assertEquals("aaa + bbb", Ast.math_string_of(TestDATAeditor.nest_BOX_2))   
    2.11      assertEquals("aaa + bbb", Ast.math_string_of(TestDATAeditor.nest_BOX_3))
    2.12 @@ -121,9 +121,9 @@
    2.13      assertEquals("aaa + bbb", Ast.math_string_of(TestDATAeditor.nest_CURSOR_BOX_3A))   
    2.14      assertEquals("aaa + bbb", Ast.math_string_of(TestDATAeditor.nest_CURSOR_BOX_3B))
    2.15      
    2.16 -    assertEquals("d_d x x + d_d x (sin (x ^ 2))", Ast.math_string_of(TestDATAeditor.box_05))   
    2.17 -    assertEquals("d_d bdv (sin (u)) = cos (u) * d_d bdv u", Ast.math_string_of(TestDATAeditor.box_06))   
    2.18 -    assertEquals("d_d x x + cos (x ^ 2) * d_d x #", Ast.math_string_of(TestDATAeditor.box_07_with_cursor))   
    2.19 +    assertEquals("d_d x x + d_d x (sin (x ^ 2))", Ast.math_string_of(TestDATAeditor.box_05))
    2.20 +    assertEquals("d_d bdv (sin (u)) = cos (u) * d_d bdv (u)", Ast.math_string_of(TestDATAeditor.box_06))   
    2.21 +    assertEquals("d_d x x + cos (x ^ 2) * d_d x (#)", Ast.math_string_of(TestDATAeditor.box_07_with_cursor))   
    2.22      assertEquals("a + (# + c)", Ast.math_string_of(TestDATAeditor.box_gap_cursor))   
    2.23      assertEquals("#", Ast.math_string_of(TestDATAeditor.box_with_cursor_on_gap))   
    2.24      assertEquals("# + #", Ast.math_string_of(TestDATAeditor.in_1a))   
     3.1 --- a/isac-java/src/java/isac/gui/mawen/syntax/ast.scala	Wed Aug 02 11:51:30 2017 +0200
     3.2 +++ b/isac-java/src/java/isac/gui/mawen/syntax/ast.scala	Wed Aug 02 18:04:23 2017 +0200
     3.3 @@ -56,30 +56,38 @@
     3.4      case ast => to_str(ast, 0, "none") //the last element
     3.5    }
     3.6    // set parentheses according to priority and to left/right associativity
     3.7 -  def to_binop(str: String, a1: Ast, a2: Ast, prior: Int, l_r: String): String = { val pri = XSyntax.isa_prior(str)
     3.8 -    if (prior > pri) {
     3.9 -       XSyntax.isa_fix(str) match {
    3.10 -       case "infixl" => XLibrary.enclose("(", ")", to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right"))
    3.11 -       case "infixr" => XLibrary.enclose("(", ")", to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right"))
    3.12 -       case "prefix" => XLibrary.enclose("(", ")", XSyntax.isa_math(str) + to_str(a1, pri, "left") + to_str(a2, pri, "right"))}
    3.13 -    }else{
    3.14 -       XSyntax.isa_fix(str) match {
    3.15 -       case "infixl" => {
    3.16 -         if (prior == pri & l_r == "right") {
    3.17 -           XLibrary.enclose("(", ")", to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right"))
    3.18 -         }else{
    3.19 -           to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right")
    3.20 +  def to_binop(str: String, a1: Ast, a2: Ast, prior: Int, l_r: String): String = {
    3.21 +    if (l_r == "prefix") XSyntax.isa_math(str) + to_prefix(List(a1, a2))
    3.22 +    else {
    3.23 +      val pri = XSyntax.isa_prior(str)
    3.24 +      if (prior > pri) {
    3.25 +         XSyntax.isa_fix(str) match {
    3.26 +         case "infixl" => XLibrary.enclose("(", ")", to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right"))
    3.27 +         case "infixr" => XLibrary.enclose("(", ")", to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right"))
    3.28 +         case "prefix" => XLibrary.enclose("(", ")", XSyntax.isa_math(str) + to_prefix(List(a1, a2))) }
    3.29 +      }else{
    3.30 +         XSyntax.isa_fix(str) match {
    3.31 +         case "infixl" => {
    3.32 +           if (prior == pri & l_r == "right") {
    3.33 +             XLibrary.enclose("(", ")", to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right"))
    3.34 +           }else{
    3.35 +             to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right")
    3.36 +           }
    3.37           }
    3.38 -       }
    3.39 -       case "infixr" => {
    3.40 -         if (prior == pri & l_r == "left") {
    3.41 -           XLibrary.enclose("(", ")", to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right"))
    3.42 -         }else{
    3.43 -           to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right")
    3.44 +         case "infixr" => {
    3.45 +           if (prior == pri & l_r == "left") {
    3.46 +             XLibrary.enclose("(", ")", to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right"))
    3.47 +           }else{
    3.48 +             to_str(a1, pri, "left") + XSyntax.isa_math(str) + to_str(a2, pri, "right")
    3.49 +           }
    3.50           }
    3.51 -       }
    3.52 -       case "prefix" => XSyntax.isa_math(str) + to_str(a1, pri, "none") + " " + to_str(a2, pri, "none")}
    3.53 -    } }
    3.54 +         case "prefix" => XSyntax.isa_math(str) + to_prefix(List(a1, a2)) }
    3.55 +    } } }
    3.56 +  def to_prefix(asts : List[Ast]) = {
    3.57 +    XLibrary.strs_to_str(asts.map(x => {
    3.58 +      val str = Ast.to_str(x, 0, "none"); if (is_atom(x)) str else XLibrary.enclose("(", ")", str)
    3.59 +      }))
    3.60 +  }
    3.61    
    3.62    /* transform an Ast into a String, which corresponds to Isabelle's 
    3.63     * pretty_printing within Isac's Knowledge, restricted for prototyping.
    3.64 @@ -103,8 +111,6 @@
    3.65      case Appl(List(Constant(str), a)) => {
    3.66        val uniop = XSyntax.isa_math(str)
    3.67        if (Set("Diff ", "Integrate ", "solve ").contains(uniop)) uniop + to_tuple(a, 0, "none")
    3.68 -      else if (prior == 99 & XSyntax.isa_prior(str) == 70) //FIXME: handles "d_d x (! sin(_) !)" as special case
    3.69 -        XLibrary.enclose("(", ")", uniop + XLibrary.enclose("(", ")", to_str(a, 0, "none")))
    3.70        else
    3.71          uniop + XLibrary.enclose("(", ")", to_str(a, 0, "none"))
    3.72        }