generate: Fix more semantic issues with generated code

This commit is contained in:
Sasha Koshka 2025-07-08 21:32:34 -04:00
parent a210f6112c
commit e48be0bc15

View File

@ -94,7 +94,7 @@ func (this *Generator) generateTypedef(name string, typ Type) (n int, err error)
nn, err = this.println()
n += nn; if err != nil { return n, err }
// Tag method
// 'Tag' method
// to be honest we probably don't need this method at all
// nn, err = this.iprintf("\n// Tag returns the preferred TAPE tag.\n")
// n += nn; if err != nil { return n, err }
@ -238,13 +238,13 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
n += nn; if err != nil { return n, err }
this.push()
nn, err = this.iprintf(
"nn, err = encoder.WriteUintN(%s.CN(), len(%s))\n",
"nn, err = encoder.WriteUintN(uint64(%s.CN()), len(%s))\n",
tagSource, valueSource)
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", tagSource)
nn, err = this.iprintf("}\n")
n += nn; if err != nil { return n, err }
nn, err = this.iprintf("nn, err = encoder.Write([]byte(%s))\n", valueSource)
@ -347,6 +347,11 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
n += nn; if err != nil { return n, err }
case TypeNamed:
// WHATEVER: [WHATEVER]
if builtin := this.resolveBuiltinType(typ.Name); builtin != nil {
nn, err := this.generateEncodeValue(builtin, valueSource, tagSource)
n += nn; if err != nil { return n, err }
return n, nil
}
nn, err := this.iprintf("nn, err = %s.EncodeValue(encoder, %s)\n", valueSource, tagSource)
n += nn; if err != nil { return n, err }
nn, err = this.generateErrorCheck()
@ -586,27 +591,8 @@ func (this *Generator) resolveMessageName(message string) string {
}
func (this *Generator) resolveTypeName(name string) (Type, error) {
switch name {
case "U8": return TypeInt { Bits: 8 }, nil
case "U16": return TypeInt { Bits: 16 }, nil
case "U32": return TypeInt { Bits: 32 }, nil
case "U64": return TypeInt { Bits: 64 }, nil
case "U128": return TypeInt { Bits: 128 }, nil
case "U256": return TypeInt { Bits: 256 }, nil
case "I8": return TypeInt { Bits: 8, Signed: true }, nil
case "I16": return TypeInt { Bits: 16, Signed: true }, nil
case "I32": return TypeInt { Bits: 32, Signed: true }, nil
case "I64": return TypeInt { Bits: 64, Signed: true }, nil
case "I128": return TypeInt { Bits: 128, Signed: true }, nil
case "I256": return TypeInt { Bits: 256, Signed: true }, nil
case "F16": return TypeFloat { Bits: 16 }, nil
case "F32": return TypeFloat { Bits: 32 }, nil
case "F64": return TypeFloat { Bits: 64 }, nil
case "F128": return TypeFloat { Bits: 128 }, nil
case "F256": return TypeFloat { Bits: 256 }, nil
case "String": return TypeString { }, nil
case "Buffer": return TypeBuffer { }, nil
case "Table": return TypeTable { }, nil
if typ := this.resolveBuiltinType(name); typ != nil {
return typ, nil
}
if typ, ok := this.protocol.Types[name]; ok {
@ -619,6 +605,32 @@ func (this *Generator) resolveTypeName(name string) (Type, error) {
return nil, fmt.Errorf("no type exists called %s", name)
}
func (this *Generator) resolveBuiltinType(name string) Type {
switch name {
case "U8": return TypeInt { Bits: 8 }
case "U16": return TypeInt { Bits: 16 }
case "U32": return TypeInt { Bits: 32 }
case "U64": return TypeInt { Bits: 64 }
case "U128": return TypeInt { Bits: 128 }
case "U256": return TypeInt { Bits: 256 }
case "I8": return TypeInt { Bits: 8, Signed: true }
case "I16": return TypeInt { Bits: 16, Signed: true }
case "I32": return TypeInt { Bits: 32, Signed: true }
case "I64": return TypeInt { Bits: 64, Signed: true }
case "I128": return TypeInt { Bits: 128, Signed: true }
case "I256": return TypeInt { Bits: 256, Signed: true }
case "F16": return TypeFloat { Bits: 16 }
case "F32": return TypeFloat { Bits: 32 }
case "F64": return TypeFloat { Bits: 64 }
case "F128": return TypeFloat { Bits: 128 }
case "F256": return TypeFloat { Bits: 256 }
case "String": return TypeString { }
case "Buffer": return TypeBuffer { }
case "Table": return TypeTable { }
}
return nil
}
func bitsToBytes(bits int) int {
return int(math.Ceil(float64(bits) / 8.0))
}