rotation.go raw
1 package lattice
2
3 // Rotation is the transition between two positions on the ternary lattice
4 // triangle, or a punctuation operator. Values 1-3 rotate, 4+ punctuate.
5 // This is the go-ref rotation model; no iskra .mx file defines it.
6 //
7 // noun (1)
8 // / \
9 // left right
10 // / \
11 // mod (3) -------- verb (2)
12 //
13 // Right = clockwise N->V, V->M, M->N
14 // Left = counterwise N->M, M->V, V->N
15 // Stay = same branch N->N, V->V, M->M
16 type Rotation uint8
17
18 const (
19 RotUnknown Rotation = 0
20 RotRight Rotation = 1
21 RotLeft Rotation = 2
22 RotStay Rotation = 3
23 RotList Rotation = 4
24 RotDefine Rotation = 5
25 RotRelate Rotation = 6
26 RotStop Rotation = 7
27 RotExclaim Rotation = 8
28 RotQuestion Rotation = 9
29 RotQuoteOpen Rotation = 10
30 RotQuoteClose Rotation = 11
31 )
32
33 // BranchRotation returns the rotation from one primary branch to another, or
34 // RotUnknown if either branch is TritEnd.
35 func BranchRotation(from, to uint8) Rotation {
36 if from < BranchType || from > BranchData || to < BranchType || to > BranchData {
37 return RotUnknown
38 }
39 if from == to {
40 return RotStay
41 }
42 if to == (from%3)+1 {
43 return RotRight
44 }
45 return RotLeft
46 }
47
48 // IsPunctuation reports whether the rotation is a structural operator.
49 func (r Rotation) IsPunctuation() bool { return r >= RotList }
50
51 // IsBreak reports whether the punctuation ends a phrase (. ! ?).
52 func (r Rotation) IsBreak() bool { return r == RotStop || r == RotExclaim || r == RotQuestion }
53
54 // String returns the operator's surface form.
55 func (r Rotation) String() string {
56 switch r {
57 case RotRight:
58 return "right"
59 case RotLeft:
60 return "left"
61 case RotStay:
62 return "stay"
63 case RotList:
64 return ","
65 case RotDefine:
66 return ":"
67 case RotRelate:
68 return ";"
69 case RotStop:
70 return "."
71 case RotExclaim:
72 return "!"
73 case RotQuestion:
74 return "?"
75 case RotQuoteOpen, RotQuoteClose:
76 return "\""
77 default:
78 return "?"
79 }
80 }
81
82 // RuneToRotation maps punctuation to its operator, or RotUnknown.
83 func RuneToRotation(r rune) Rotation {
84 switch r {
85 case ',':
86 return RotList
87 case ':':
88 return RotDefine
89 case ';':
90 return RotRelate
91 case '.':
92 return RotStop
93 case '!':
94 return RotExclaim
95 case '?':
96 return RotQuestion
97 case '"', '\u201C', '\u00AB':
98 return RotQuoteOpen
99 case '\u201D', '\u00BB':
100 return RotQuoteClose
101 default:
102 return RotUnknown
103 }
104 }
105
106 // TritPathDistance is the lattice distance between two coordinates. -1 when
107 // either is unassigned. This is Distance over TritPath; the moxie source keeps
108 // both names.
109 func TritPathDistance(a, b TritPath) int { return Distance(a, b) }
110
111 // SetBigramTransition stores the target coordinate and the rotation on a
112 // bigram's MetaEntry.
113 func (m *MetaEntry) SetBigramTransition(rot Rotation, target TritPath) {
114 m.SetTritPath(target)
115 m.SetRotation(rot)
116 }
117
118 // GetBigramTransition reads the rotation and target coordinate back.
119 func (m *MetaEntry) GetBigramTransition() (Rotation, TritPath) {
120 return m.Rotation(), m.TritPath()
121 }
122