package iskra import "git.smesh.lol/iskradb/lattice" // NullLatticeRec mirrors lattice.NullRec for use in iskra without importing lattice everywhere. const NullLatticeRec = lattice.NullRec // KindToBranch maps a NodeKind to the iskradb branch it belongs to. func KindToBranch(kind NodeKind) lattice.Branch { if kind.Has(KindFunc | KindMethod) { return lattice.Bverb } if kind.Has(KindType | KindPkg | KindImport | KindConst | KindVar) { return lattice.Bnoun } return lattice.Bmodifier } // otherBranches returns the two branches that are not b, in a stable order. func otherBranches(b lattice.Branch) [2]lattice.Branch { switch b { case lattice.Bnoun: return [2]lattice.Branch{lattice.Bverb, lattice.Bmodifier} case lattice.Bverb: return [2]lattice.Branch{lattice.Bnoun, lattice.Bmodifier} default: // Bmodifier return [2]lattice.Branch{lattice.Bnoun, lattice.Bverb} } } // SetFormOnRecord stores form in rec.Inline (up to 23 bytes) or overflows // into pool with rec.DataFile=1, rec.DataOff/DataLen pointing into pool. func SetFormOnRecord(rec *lattice.Record, form string, pool *[]byte) { b := []byte(form) if len(b) <= 23 { copy(rec.Inline[:], b) rec.Inline[23] = byte(len(b)) // length in last byte rec.DataFile = 0 rec.DataOff = 0 rec.DataLen = 0 } else { copy(rec.Inline[:23], b[:23]) rec.Inline[23] = 0 // overflow marker: length=0 means overflow rec.DataFile = 1 rec.DataOff = uint32(len(*pool)) rec.DataLen = uint32(len(b)) *pool = append(*pool, b...) } } // FormFromRecord retrieves the surface form from rec. func FormFromRecord(rec *lattice.Record, pool []byte) string { if rec.Inline[23] != 0 { // inline: length stored in Inline[23] n := int(rec.Inline[23]) return string(rec.Inline[:n]) } if rec.DataFile == 1 && rec.DataLen > 0 { end := rec.DataOff + rec.DataLen if int(end) <= len(pool) { return string(pool[rec.DataOff:end]) } } return "" }