generate: Implement bool in static system

This commit is contained in:
Sasha Koshka 2025-10-15 00:58:14 -04:00
parent 13d35e54f5
commit 00b0f13d3e

View File

@ -47,6 +47,15 @@ func canAssign(destination, source tape.Tag) bool {
}
return false
}
// boolInt converts a bool to an integer.
func boolInt(input bool) int {
if input {
return 1
} else {
return 0
}
}
`
// Generator converts protocols into Go code.
@ -315,6 +324,9 @@ func (this *Generator) generateMessage(method uint16, message Message) (n int, e
// - nn int
func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource string) (n int, err error) {
switch typ := typ.(type) {
case TypeBool:
// SI: (none)
// SI stores the value in the tag, so we write nothing here
case TypeInt:
// SI: (none)
// LI/LSI: <value: IntN>
@ -518,6 +530,11 @@ func (this *Generator) generateEncodeValue(typ Type, valueSource, tagSource stri
// for [Generator.generateDecodeBranch].
func (this *Generator) generateDecodeValue(typ Type, typeName, valueSource, tagSource string) (n int, err error) {
switch typ := typ.(type) {
case TypeBool:
// SI: (none)
// SI stores the value in the tag
nn, err := this.iprintf("*%s = %s.CN() > 0\n", valueSource, tagSource)
n += nn; if err != nil { return n, err }
case TypeInt:
// SI: (none)
// LI/LSI: <value: IntN>
@ -944,6 +961,9 @@ func (this *Generator) generateBareErrorCheck() (n int, err error) {
func (this *Generator) generateTag(typ Type, source string) (tagVar string, n int, err error) {
tagVar = this.newTemporaryVar("tag")
switch typ := typ.(type) {
case TypeBool:
nn, err := this.iprintf("%s := tape.SI.WithCN(boolInt(bool(%s)))\n", tagVar, source)
n += nn; if err != nil { return tagVar, n, err }
case TypeInt:
if typ.Bits <= 5 {
nn, err := this.iprintf("%s := tape.SI.WithCN(int(%s))\n", tagVar, source)
@ -997,6 +1017,9 @@ func (this *Generator) generateTag(typ Type, source string) (tagVar string, n in
// information is chosen.
func (this *Generator) generateTN(typ Type) (n int, err error) {
switch typ := typ.(type) {
case TypeBool:
nn, err := this.printf("tape.SI")
n += nn; if err != nil { return n, err }
case TypeInt:
if typ.Bits <= 5 {
nn, err := this.printf("tape.SI")
@ -1040,6 +1063,9 @@ func (this *Generator) generateTN(typ Type) (n int, err error) {
func (this *Generator) generateType(typ Type) (n int, err error) {
switch typ := typ.(type) {
case TypeBool:
nn, err := this.printf("bool")
n += nn; if err != nil { return n, err }
case TypeInt:
if err := this.validateIntBitSize(typ.Bits); err != nil {
return n, err