rotation.mx raw

   1  package iskra
   2  
   3  // Rotation encodes transitions on the ternary code lattice.
   4  //
   5  //	       type (1)
   6  //	      /        \
   7  //	   left        right
   8  //	    /              \
   9  //	data (3) -------- func (2)
  10  //
  11  // Right = clockwise:        T→F, F→D, D→T
  12  // Left  = counterclockwise: T→D, D→F, F→T
  13  // Stay  = same branch
  14  //
  15  // Punctuation operators mark structural boundaries in code:
  16  //   block-open ({)   = enter sub-scope
  17  //   block-close (})  = leave sub-scope
  18  //   separator (;)    = statement boundary
  19  //   list (,)         = parameter/argument list
  20  type Rotation uint8
  21  
  22  const (
  23  	RotUnknown    Rotation = 0
  24  	RotRight      Rotation = 1
  25  	RotLeft       Rotation = 2
  26  	RotStay       Rotation = 3
  27  	RotBlockOpen  Rotation = 4
  28  	RotBlockClose Rotation = 5
  29  	RotSeparator  Rotation = 6
  30  	RotList       Rotation = 7
  31  )
  32  
  33  func BranchRotation(from, to uint8) Rotation {
  34  	if from < BranchType || from > BranchData ||
  35  		to < BranchType || to > BranchData {
  36  		return RotUnknown
  37  	}
  38  	if from == to {
  39  		return RotStay
  40  	}
  41  	if to == (from%3)+1 {
  42  		return RotRight
  43  	}
  44  	return RotLeft
  45  }
  46  
  47  func (r Rotation) IsStructural() bool {
  48  	return r >= RotBlockOpen
  49  }
  50  
  51  func (r Rotation) String() string {
  52  	switch r {
  53  	case RotRight:
  54  		return "right"
  55  	case RotLeft:
  56  		return "left"
  57  	case RotStay:
  58  		return "stay"
  59  	case RotBlockOpen:
  60  		return "{"
  61  	case RotBlockClose:
  62  		return "}"
  63  	case RotSeparator:
  64  		return ";"
  65  	case RotList:
  66  		return ","
  67  	default:
  68  		return "?"
  69  	}
  70  }
  71  
  72  func (m *MetaEntry) SetRotation(r Rotation) {
  73  	m.Extra[4] = (m.Extra[4] & 0xF0) | byte(r&0x0F)
  74  }
  75  
  76  func (m *MetaEntry) GetRotation() Rotation {
  77  	return Rotation(m.Extra[4] & 0x0F)
  78  }
  79  
  80  func (m *MetaEntry) SetBigramTransition(rot Rotation, targetPath TritPath) {
  81  	m.SetTritPath(targetPath)
  82  	m.SetRotation(rot)
  83  }
  84  
  85  func (m *MetaEntry) GetBigramTransition() (Rotation, TritPath) {
  86  	return m.GetRotation(), m.GetTritPath()
  87  }
  88