generate: Use tape.EncodeAny for encoding undefined tables

This commit is contained in:
Sasha Koshka 2025-06-20 18:41:11 -04:00
parent c4407d9759
commit 376a3f1b46

View File

@ -146,7 +146,10 @@ func (this *Generator) generateTypedef(name string, typ Type) (n int, err error)
name) name)
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
this.push() this.push()
// TODO nn, err = this.generateDecodeValue(typ, "this", "tag")
n += nn; if err != nil { return n, err }
nn, err = this.iprintf("return n, nil\n")
n += nn; if err != nil { return n, err }
this.pop() this.pop()
nn, err = this.iprintf("}\n") nn, err = this.iprintf("}\n")
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
@ -154,6 +157,8 @@ func (this *Generator) generateTypedef(name string, typ Type) (n int, err error)
return n, nil return n, nil
} }
// generateMessage generates the structure, as well as encoding decoding
// functions for the given message.
func (this *Generator) generateMessage(method uint16, message Message) (n int, err error) { func (this *Generator) generateMessage(method uint16, message Message) (n int, err error) {
nn, err := this.iprintf( nn, err := this.iprintf(
"\n// %s represents the protocol message M%04X %s.\n", "\n// %s represents the protocol message M%04X %s.\n",
@ -196,6 +201,16 @@ func (this *Generator) generateMessage(method uint16, message Message) (n int, e
return n, nil return n, nil
} }
// generateEncodeValue generates code to encode a value of a specified type. It
// pulls from the variable (or parenthetical statement) specified by
// valueSource, and the value will be encoded according to the tag stored in
// the variable (or parenthetical statement) specified by tagSource.
// the code generated is a BLOCK and expects these variables to be defined:
//
// - encoder *tape.Encoder
// - n int
// - err error
// - nn int
func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource string) (n int, err error) { func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource string) (n int, err error) {
switch typ := typ.(type) { switch typ := typ.(type) {
case TypeInt: case TypeInt:
@ -288,31 +303,11 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
case TypeTable: case TypeTable:
// KTV: <length: UN> (<key: U16> <tag: Tag> <value>)* // KTV: <length: UN> (<key: U16> <tag: Tag> <value>)*
nn, err := this.iprintf( nn, err := this.iprintf(
"nn, err = encoder.WriteUintN(%s.CN(), uint64(len(%s)))\n", "nn, err = tape.EncodeAny(encoder, %s, %s)\n",
tagSource, valueSource) valueSource, tagSource)
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck() nn, err = this.generateErrorCheck()
n += nn; if err != nil { return n, err } n += nn; if err != nil { return n, err }
nn, err = this.iprintf("for key, item := range %s {\n", valueSource)
n += nn; if err != nil { return n, err }
this.push()
nn, err = this.iprintf("nn, err = encoder.WriteUint16(key)\n")
n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck()
n += nn; if err != nil { return n, err }
nn, err = this.iprintf("tag := tape.TagAny(tag)\n")
n += nn; if err != nil { return n, err }
nn, err = this.iprintf("nn, err = encoder.WriteUint8(uint8(tag))\n")
n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck()
n += nn; if err != nil { return n, err }
nn, err = this.iprintf("nn, err = tape.EncodeAny(tag)\n")
n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck()
n += nn; if err != nil { return n, err }
this.pop()
nn, err = this.iprintf("}\n")
n += nn; if err != nil { return n, err }
case TypeTableDefined: case TypeTableDefined:
// KTV: <length: UN> (<key: U16> <tag: Tag> <value>)* // KTV: <length: UN> (<key: U16> <tag: Tag> <value>)*
nn, err := this.iprintf( nn, err := this.iprintf(
@ -359,6 +354,20 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
return n, nil return n, nil
} }
// generateDencodeValue generates code to decode a value of a specified type. It
// overwrites memory pointed to by the variable (or parenthetical statement)
// specified by valueSource, and the value will be encoded according to the tag
// stored in the variable (or parenthetical statement) specified by tagSource.
// the code generated is a BLOCK and expects these variables to be defined:
//
// - decoder *tape.Decoder
// - n int
// - err error
// - nn int
func (this *Generator) generateDecodeValue(typ Type, valueSource, tagSource string) (n int, err error) {
// TODO
}
func (this *Generator) generateErrorCheck() (n int, err error) { func (this *Generator) generateErrorCheck() (n int, err error) {
return this.iprintf("n += nn; if err != nil { return n, err }\n") return this.iprintf("n += nn; if err != nil { return n, err }\n")
} }