From 376a3f1b463b46c2cd2c9441fe412d74e3bdd505 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Fri, 20 Jun 2025 18:41:11 -0400 Subject: [PATCH] generate: Use tape.EncodeAny for encoding undefined tables --- generate/generate.go | 55 ++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/generate/generate.go b/generate/generate.go index 939d399..67328b8 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -146,7 +146,10 @@ func (this *Generator) generateTypedef(name string, typ Type) (n int, err error) name) n += nn; if err != nil { return n, err } 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() nn, err = this.iprintf("}\n") 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 } +// 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) { nn, err := this.iprintf( "\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 } +// 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) { switch typ := typ.(type) { case TypeInt: @@ -288,31 +303,11 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri case TypeTable: // KTV: ( )* nn, err := this.iprintf( - "nn, err = encoder.WriteUintN(%s.CN(), uint64(len(%s)))\n", - tagSource, valueSource) + "nn, err = tape.EncodeAny(encoder, %s, %s)\n", + valueSource, tagSource) n += nn; if err != nil { return n, err } nn, err = this.generateErrorCheck() 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: // KTV: ( )* nn, err := this.iprintf( @@ -359,6 +354,20 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri 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) { return this.iprintf("n += nn; if err != nil { return n, err }\n") }