generate: Fix a bunch of semantic issues with the generated code

This commit is contained in:
Sasha Koshka 2025-07-08 14:52:05 -04:00
parent e75d7534c1
commit cdba8ee601

View File

@ -174,7 +174,7 @@ func (this *Generator) generateMessage(method uint16, message Message) (n int, e
nn, err = this.iprintf("\n// Encode encodes this message's tag and value.\n")
n += nn; if err != nil { return n, err }
nn, err = this.iprintf(
"func(this %s) Encode(encoder *tape.Encoder) (n int, err error) {\n",
"func(this *%s) Encode(encoder *tape.Encoder) (n int, err error) {\n",
this.resolveMessageName(message.Name))
n += nn; if err != nil { return n, err }
this.push()
@ -311,7 +311,7 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
case TypeTableDefined:
// KTV: <length: UN> (<key: U16> <tag: Tag> <value>)*
nn, err := this.iprintf(
"nn, err = encoder.WriteUintN(%s.CN(), %d)\n",
"nn, err = encoder.WriteUintN(uint64(%s.CN()), %d)\n",
tagSource, len(typ.Fields))
n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck()
@ -379,29 +379,29 @@ func (this *Generator) generateTag(typ Type, source string) (n int, err error) {
switch typ := typ.(type) {
case TypeInt:
if typ.Bits <= 5 {
nn, err := this.printf("tape.TagSI")
nn, err := this.printf("tape.SI")
n += nn; if err != nil { return n, err }
} else {
nn, err := this.printf("tape.TagLI.WithCN(%d)", bitsToCN(typ.Bits))
nn, err := this.printf("tape.LI.WithCN(%d)", bitsToCN(typ.Bits))
n += nn; if err != nil { return n, err }
}
case TypeFloat:
nn, err := this.printf("tape.TagFP.WithCN(%d)", bitsToCN(typ.Bits))
nn, err := this.printf("tape.FP.WithCN(%d)", bitsToCN(typ.Bits))
n += nn; if err != nil { return n, err }
case TypeString:
nn, err := this.generateTag(TypeBuffer { }, source)
n += nn; if err != nil { return n, err }
case TypeBuffer:
nn, err := this.printf("bufferTag(%s)", source)
nn, err := this.printf("tape.BufferTag(%s)", source)
n += nn; if err != nil { return n, err }
case TypeArray:
nn, err := this.printf("arrayTag(tape.TagOTA.WithCN(tape.IntBytes(uint64(len(%s)))))", source)
nn, err := this.printf("arrayTag(tape.OTA.WithCN(tape.IntBytes(uint64(len(%s)))))", source)
n += nn; if err != nil { return n, err }
case TypeTable:
nn, err := this.printf("tape.TagKTV.WithCN(tape.IntBytes(uint64(len(%s))))", source)
nn, err := this.printf("tape.KTV.WithCN(tape.IntBytes(uint64(len(%s))))", source)
n += nn; if err != nil { return n, err }
case TypeTableDefined:
nn, err := this.printf("tape.TagKTV.WithCN(%d)", tape.IntBytes(uint64(len(typ.Fields))))
nn, err := this.printf("tape.KTV.WithCN(%d)", tape.IntBytes(uint64(len(typ.Fields))))
n += nn; if err != nil { return n, err }
case TypeNamed:
resolved, err := this.resolveTypeName(typ.Name)