135 lines
2.9 KiB
Go
135 lines
2.9 KiB
Go
package syntaxbaum
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type SyntaxBaum struct {
|
|
Kind string
|
|
Expr string
|
|
Valence int
|
|
Children [](*SyntaxBaum)
|
|
}
|
|
|
|
/* ---------------------------------------------------------------- *
|
|
* METHODS
|
|
* ---------------------------------------------------------------- */
|
|
|
|
func (tree SyntaxBaum) GetChildren() []SyntaxBaum {
|
|
var n int = tree.Valence
|
|
var children = make([]SyntaxBaum, n)
|
|
for i, subtreePtr := range tree.Children {
|
|
children[i] = *subtreePtr
|
|
}
|
|
return children
|
|
}
|
|
|
|
func (tree SyntaxBaum) GetChild(indexOpt ...int) (SyntaxBaum, error) {
|
|
var index int = 0
|
|
if len(indexOpt) > 0 {
|
|
index = indexOpt[0]
|
|
}
|
|
var subtree SyntaxBaum
|
|
var err error
|
|
if 0 <= index && index < tree.Valence {
|
|
subtree = *(tree.Children[index])
|
|
} else {
|
|
err = errors.New(fmt.Sprintf("Instance has no child of index %d !", index))
|
|
}
|
|
return subtree, err
|
|
}
|
|
|
|
func (tree SyntaxBaum) Pretty(preindentOpt ...string) string {
|
|
var preindent string = ""
|
|
if len(preindentOpt) > 0 {
|
|
preindent = preindentOpt[0]
|
|
}
|
|
return tree.pretty(preindent, " ", "", 0)
|
|
}
|
|
|
|
func (tree SyntaxBaum) pretty(preindent string, tab string, prepend string, depth int) string {
|
|
var indent string = preindent + strings.Repeat(tab, depth)
|
|
switch tree.Valence {
|
|
case 0:
|
|
switch kind := tree.Kind; kind {
|
|
case "atom", "generic":
|
|
return indent + prepend + kind + " " + tree.Expr
|
|
default:
|
|
return indent + prepend + kind
|
|
}
|
|
default:
|
|
var lines string = indent + prepend + tree.Kind
|
|
prepend = "|__ "
|
|
for _, subtree := range tree.Children {
|
|
lines += "\n" + subtree.pretty(preindent, tab, prepend, depth+1)
|
|
}
|
|
return lines
|
|
}
|
|
}
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
// METHODS: Recognitong of Formula-Types
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
func (tree SyntaxBaum) IsIrreducible() bool {
|
|
return tree.Valence == 0
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsAtom() bool {
|
|
return tree.Kind == "atom"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsLiteral() bool {
|
|
if tree.IsAtom() {
|
|
return true
|
|
} else if tree.IsNegation() {
|
|
subtree, err := tree.GetChild()
|
|
if err == nil {
|
|
return subtree.IsAtom()
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsGeneric() bool {
|
|
return tree.Kind == "generic"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsTautologySymbol() bool {
|
|
return tree.Kind == "taut"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsContradictionSymbol() bool {
|
|
return tree.Kind == "contradiction"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsConnective() bool {
|
|
return tree.Valence > 0
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsNegation() bool {
|
|
return tree.Kind == "not"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsConjunction2() bool {
|
|
return tree.Kind == "and2"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsConjunction() bool {
|
|
return tree.Kind == "and" || tree.Kind == "and2"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsDisjunction2() bool {
|
|
return tree.Kind == "or2"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsDisjunction() bool {
|
|
return tree.Kind == "or" || tree.Kind == "or2"
|
|
}
|
|
|
|
func (tree SyntaxBaum) IsImplication() bool {
|
|
return tree.Kind == "implies"
|
|
}
|