@@ -31,14 +31,31 @@ class Term {
3131 }
3232 }
3333
34+ precedence ( ) {
35+ return 3
36+ }
37+
3438 // checks if we should put parens around this formula
3539 shouldParen ( ) {
3640 return ! ( this instanceof TermVar || this instanceof TermInt || this instanceof TermFun )
3741 }
3842
3943 // parenthesize the formula if necessary in the Unicode or LaTeX rendering
40- punicode ( ) { return this . shouldParen ( ) ? `(${ this . unicode ( ) } )` : this . unicode ( ) }
41- platex ( ) { return this . shouldParen ( ) ? `(${ this . latex ( ) } )` : this . latex ( ) }
44+ punicode ( parentPrec = null , position = 'left' ) {
45+ const prec = this . precedence ( )
46+ const needsParen = parentPrec !== null &&
47+ ( prec < parentPrec || ( position === 'right' && prec === parentPrec ) )
48+ const text = this . unicode ( )
49+ return needsParen ? `(${ text } )` : ( this . shouldParen ( ) ? `(${ text } )` : text )
50+ }
51+
52+ platex ( parentPrec = null , position = 'left' ) {
53+ const prec = this . precedence ( )
54+ const needsParen = parentPrec !== null &&
55+ ( prec < parentPrec || ( position === 'right' && prec === parentPrec ) )
56+ const text = this . latex ( )
57+ return needsParen ? `(${ text } )` : ( this . shouldParen ( ) ? `(${ text } )` : text )
58+ }
4259}
4360
4461class TermVar extends Term {
@@ -132,6 +149,8 @@ class TermIte extends Term {
132149 this . e . subst ( v , term ) )
133150 }
134151
152+ precedence ( ) { return 0 }
153+
135154 unicode ( ) { return `if ${ this . cond . punicode ( ) } then ${ this . t . punicode ( ) } else ${ this . e . punicode ( ) } ` }
136155 latex ( ) { return `\\text{if}\\; ${ this . cond . platex ( ) } \\;\\text{then}\\; ${ this . t . platex ( ) } \\;\\text{else}\\; ${ this . e . platex ( ) } ` }
137156 smtlib ( ) { return `(ite ${ this . cond . platex ( ) } ${ this . t . platex ( ) } ${ this . e . platex ( ) } )` }
@@ -1276,8 +1295,19 @@ class AddTerms extends TermFun {
12761295 this . second . subst ( v , term ) )
12771296 }
12781297
1279- unicode ( ) { return `${ this . first . punicode ( ) } + ${ this . second . punicode ( ) } ` }
1280- latex ( ) { return `${ this . first . platex ( ) } + ${ this . second . platex ( ) } ` }
1298+ precedence ( ) { return 1 }
1299+
1300+ unicode ( ) {
1301+ const left = this . first . punicode ( this . precedence ( ) , 'left' )
1302+ const right = this . second . punicode ( this . precedence ( ) , 'right' )
1303+ return `${ left } + ${ right } `
1304+ }
1305+
1306+ latex ( ) {
1307+ const left = this . first . platex ( this . precedence ( ) , 'left' )
1308+ const right = this . second . platex ( this . precedence ( ) , 'right' )
1309+ return `${ left } + ${ right } `
1310+ }
12811311 reconstructor ( ) {
12821312 return `new AddTerms(${ this . first . reconstructor ( ) } , ${ this . second . reconstructor ( ) } )`
12831313 }
@@ -1298,8 +1328,19 @@ class SubtractTerms extends TermFun {
12981328 this . second . subst ( v , term ) )
12991329 }
13001330
1301- unicode ( ) { return `${ this . first . punicode ( ) } - ${ this . second . punicode ( ) } ` }
1302- latex ( ) { return `${ this . first . platex ( ) } - ${ this . second . platex ( ) } ` }
1331+ precedence ( ) { return 1 }
1332+
1333+ unicode ( ) {
1334+ const left = this . first . punicode ( this . precedence ( ) , 'left' )
1335+ const right = this . second . punicode ( this . precedence ( ) , 'right' )
1336+ return `${ left } - ${ right } `
1337+ }
1338+
1339+ latex ( ) {
1340+ const left = this . first . platex ( this . precedence ( ) , 'left' )
1341+ const right = this . second . platex ( this . precedence ( ) , 'right' )
1342+ return `${ left } - ${ right } `
1343+ }
13031344 reconstructor ( ) {
13041345 return `new SubtractTerms(${ this . first . reconstructor ( ) } , ${ this . second . reconstructor ( ) } )`
13051346 }
@@ -1320,8 +1361,19 @@ class MultiplyTerms extends TermFun {
13201361 this . second . subst ( v , term ) )
13211362 }
13221363
1323- unicode ( ) { return `${ this . first . punicode ( ) } * ${ this . second . punicode ( ) } ` }
1324- latex ( ) { return `${ this . first . platex ( ) } * ${ this . second . platex ( ) } ` }
1364+ precedence ( ) { return 2 }
1365+
1366+ unicode ( ) {
1367+ const left = this . first . punicode ( this . precedence ( ) , 'left' )
1368+ const right = this . second . punicode ( this . precedence ( ) , 'right' )
1369+ return `${ left } * ${ right } `
1370+ }
1371+
1372+ latex ( ) {
1373+ const left = this . first . platex ( this . precedence ( ) , 'left' )
1374+ const right = this . second . platex ( this . precedence ( ) , 'right' )
1375+ return `${ left } * ${ right } `
1376+ }
13251377 reconstructor ( ) {
13261378 return `new MultiplyTerms(${ this . first . reconstructor ( ) } , ${ this . second . reconstructor ( ) } )`
13271379 }
@@ -1342,8 +1394,19 @@ class DivideTerms extends TermFun {
13421394 this . second . subst ( v , term ) )
13431395 }
13441396
1345- unicode ( ) { return `${ this . first . punicode ( ) } / ${ this . second . punicode ( ) } ` }
1346- latex ( ) { return `${ this . first . platex ( ) } / ${ this . second . platex ( ) } ` }
1397+ precedence ( ) { return 2 }
1398+
1399+ unicode ( ) {
1400+ const left = this . first . punicode ( this . precedence ( ) , 'left' )
1401+ const right = this . second . punicode ( this . precedence ( ) , 'right' )
1402+ return `${ left } / ${ right } `
1403+ }
1404+
1405+ latex ( ) {
1406+ const left = this . first . platex ( this . precedence ( ) , 'left' )
1407+ const right = this . second . platex ( this . precedence ( ) , 'right' )
1408+ return `${ left } / ${ right } `
1409+ }
13471410 reconstructor ( ) {
13481411 return `new DivideTerms(${ this . first . reconstructor ( ) } , ${ this . second . reconstructor ( ) } )`
13491412 }
0 commit comments