1 package isac.gui.mawen.scalaterm
3 import isac.gui.mawen.syntax.XSyntax
4 import isac.gui.mawen.syntax.Ast
5 import isac.gui.mawen.termtree.TreeNodeContent
6 import javax.swing.tree.DefaultMutableTreeNode
8 import edu.tum.cs.isabelle.api.XML
9 import edu.tum.cs.isabelle.pure._
10 import edu.tum.cs.isabelle._ // for Codec
14 def int(s: String): Integer = new Integer(s)
16 /* transform a Term into a String, which corresponds to Isabelle's
17 * pretty_printing within Isac's Knowledge.
18 * mawen uses this for string representation of Terms.
20 def str_of(t: Term, prior: Int, l_r: String): String = t match {
21 case App(App(Const("Product_Type.Pair", _), t1), t2)
22 => str_of(t1, prior, "none") + ", " + str_of(t2, prior, "none")
23 case App(App(Const(str, _), t1), t2)
24 => { val pri = XSyntax.isa_prior(str)
26 XSyntax.isa_fix(str) match {
27 case "infixl" => "(" + str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right") + ")"
28 case "infixr" => "(" + str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right") + ")"
29 case "prefix" => "(" + XSyntax.isa_math(str) + str_of(t1, pri, "left") + " (2" + str_of(t2, pri, "right") + "2)" + ")"}
31 XSyntax.isa_fix(str) match {
33 if (prior == pri & l_r == "right") {
34 "(" + str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right") + ")"
36 str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right")
40 if (prior == pri & l_r == "left") {
41 "(" + str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right") + ")"
43 str_of(t1, pri, "left") + XSyntax.isa_math(str) + str_of(t2, pri, "right")
46 case "prefix" => XSyntax.isa_math(str) + str_of(t1, pri, "none") + " " + str_of(t2, pri, "none")}
48 case App(Const(str, _), t1) => XSyntax.isa_math(str) + "(" + str_of(t1, prior, "none") + ")"
49 case Free(str, _) => str
50 case Const(str, _) => str
51 case _ => throw new IllegalArgumentException("str_of WRONG arg: " + t)
53 // infix: "left" or "right" argument ----------vvvv
54 def string_of(t: Term): String = str_of(t, 0, "none")
57 def insert_nodes(t: Ast.Ast, n: DefaultMutableTreeNode): DefaultMutableTreeNode = t match {
58 case Ast.Appl(List(Ast.Appl(List(Ast.Constant("Product_Type.Pair"), t1)), t2))
60 case Ast.Appl(List(Ast.Appl(List(Ast.Constant(str), t1)), t2))
61 => { val tmp1 = n.clone().asInstanceOf[DefaultMutableTreeNode]
62 n.setUserObject(new TreeNodeContent(XSyntax.ast_math(str),
63 Ast.math_string_of(Ast.Appl(List(Ast.Appl(List(Ast.Constant(str), t1)), t2)))))
64 n.add(insert_nodes(t1, tmp1))
65 val tmp2 = n.clone().asInstanceOf[DefaultMutableTreeNode]
66 n.add(insert_nodes(t2, tmp2))
69 case Ast.Appl(List(Ast.Constant(str), t1))
70 => { val tmp = n.clone().asInstanceOf[DefaultMutableTreeNode]
71 n.setUserObject(new TreeNodeContent(XSyntax.ast_math(str),
72 Ast.math_string_of(Ast.Appl(List(Ast.Constant(str), t1)))))
73 n.add(insert_nodes(t1, tmp))
76 case Ast.Variable(str) => { n.setUserObject(str); n }
77 case Ast.Constant(str) => { n.setUserObject(str); n }
78 case _ => throw new IllegalArgumentException("jtree_of WRONG arg: " + t)
80 def jtree_of(t: Ast.Ast): DefaultMutableTreeNode = insert_nodes(t, new DefaultMutableTreeNode)
83 * Code not adapted to migration from Term to Ast.
85 structure beeped in java class SoundUtils - necessary to beep other components?
86 def sound_of_sub(t: Term, line: Integer, indent: Integer): Unit = t match {
87 case App(App(Const("Product_Type.Pair", _), t1), t2)
88 => isac.gui.mawen.termtree.SoundUtils.tone(261, 500)
89 case App(App(Const(str, typ), t1), t2)
90 => { isac.gui.mawen.termtree.SoundUtils.repeatTone(261+(10*line), 500,indent) //tonhoehe: line, wiederholung: intent
91 sound_of_sub(t1, line + 1, indent + 1)
92 sound_of_sub(t2, line + 1, indent + 1)
94 case App(Const(str, typ), t1)
95 => { isac.gui.mawen.termtree.SoundUtils.repeatTone(261+(10*line), 500,indent) //tonhoehe: line, wiederholung: intent
96 sound_of_sub(t1, line + 1, indent + 1)
98 case Free(str, _) => {
99 isac.gui.mawen.termtree.SoundUtils.repeatTone(1000+(10*line), 500,indent)
101 case Const(str, _) => {
102 isac.gui.mawen.termtree.SoundUtils.repeatTone(2000+(10*line), 500,indent)
104 case _ => throw new IllegalArgumentException("sound_of WRONG arg: " + t)
106 def sound_of(t: Term) = sound_of_sub(t, 0, 0)
109 def is_numeral(s: String): Boolean = {
113 case e: Exception => false
116 def matml_of(t: Term, prior: Int, l_r: String): String = t match {
117 case App(App(Const("Product_Type.Pair", _), t1), t2)
118 => matml_of(t1, prior, "none") + ", " + matml_of(t2, prior, "none")
119 case App(App(Const("Fields.inverse_class.divide", _), t1), t2)
121 val str = "Fields.inverse_class.divide"
122 val pri = XSyntax.isa_prior(str)
123 "<mfrac><mrow>" + matml_of(t1, pri, "left") + "</mrow><mrow>" + matml_of(t2, pri, "right") + "</mrow></mfrac>"
125 case App(App(Const(str, _), t1), t2)
126 => { val pri = XSyntax.isa_prior(str)
128 XSyntax.isa_fix(str) match {
129 case "prefix" => "<mfenced separators=\'\'>" + "<mo>" + XSyntax.isa_math(str) + "</mo>" + matml_of(t1, pri, "left") +
130 "<mfenced separators=\'\'>" + matml_of(t2, pri, "right") + "</mfenced>" + "</mfenced>"
131 case _ => "<mfenced separators=\'\'>" + matml_of(t1, pri, "left") + "<mo>" +
132 XSyntax.isa_math(str) + "</mo>" + matml_of(t2, pri, "right") + "</mfenced>"
135 XSyntax.isa_fix(str) match {
136 case "prefix" => "<mo>" + XSyntax.isa_math(str) + "</mo>" + matml_of(t1, pri, "none") + " " + matml_of(t2, pri, "none")
138 if (prior == pri & l_r == "right") {
139 matml_of(t1, pri, "left") + "<mo>" + XSyntax.isa_math(str) + "</mo>" + matml_of(t2, pri, "right")
141 matml_of(t1, pri, "left") + "<mo>" + XSyntax.isa_math(str) + "</mo>" + matml_of(t2, pri, "right")
146 case App(Const(str, _), t1) => "<mo>" + XSyntax.isa_math(str) + "</mo>" + "<mfenced separators=\'\'>" + matml_of(t1, prior, "none") + "</mfenced>"
147 case Free(str, _) => if (is_numeral(str)) "<mn>" + str + "</mn>" else "<mi>" + str + "</mi>"
148 case Const(str, _) => str
149 case _ => throw new IllegalArgumentException("matml_of WRONG arg: " + t)
151 def mathml_of(t: Term): String = "<math xmlns="+"\"&mathml;\""+"><mrow>" + matml_of(t, 0, "none") + "</mrow></math>"