mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-26 09:01:35 +01:00
Factor out cgen/expr.go.
This commit is contained in:
1 parent
0f86add17e
commit
dd12b469a3
3 files changed
+379
-368
No files matched your search
@@ -672,195 +672,3 @@ func (g *gen) writeCtorImpl(b *buffer, n *a.Struct) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprUnaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
// TODO.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
op := n.ID0()
|
||||
if op.Key() == t.KeyXBinaryAs {
|
||||
return g.writeExprAs(b, n.LHS().Expr(), n.RHS().TypeExpr(), rp, depth)
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
}
|
||||
if err := g.writeExpr(b, n.LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: does KeyXBinaryAmpHat need special consideration?
|
||||
b.writes(cOpNames[0xFF&op.Key()])
|
||||
if err := g.writeExpr(b, n.RHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb(')')
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprAs(b *buffer, lhs *a.Expr, rhs *a.TypeExpr, rp replacementPolicy, depth uint32) error {
|
||||
b.writes("((")
|
||||
// TODO: watch for passing an array type to writeCTypeName? In C, an array
|
||||
// type can decay into a pointer.
|
||||
if err := g.writeCTypeName(b, rhs, "", ""); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes(")(")
|
||||
if err := g.writeExpr(b, lhs, rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
b.writes("))")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeExprAssociativeOp(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
}
|
||||
opName := cOpNames[0xFF&n.ID0().Key()]
|
||||
for i, o := range n.Args() {
|
||||
if i != 0 {
|
||||
b.writes(opName)
|
||||
}
|
||||
if err := g.writeExpr(b, o.Expr(), rp, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb(')')
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *gen) writeCTypeName(b *buffer, n *a.TypeExpr, varNamePrefix string, varName string) error {
|
||||
// It may help to refer to http://unixwiz.net/techtips/reading-cdecl.html
|
||||
|
||||
// maxNumPointers is an arbitrary implementation restriction.
|
||||
const maxNumPointers = 16
|
||||
|
||||
x := n
|
||||
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
|
||||
}
|
||||
|
||||
numPointers, innermost := 0, x
|
||||
for ; innermost != nil && innermost.Inner() != nil; innermost = innermost.Inner() {
|
||||
// TODO: "nptr T", not just "ptr T".
|
||||
if p := innermost.Decorator().Key(); p == t.KeyPtr {
|
||||
if numPointers == maxNumPointers {
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C: too many ptr's", n.String(g.tm))
|
||||
}
|
||||
numPointers++
|
||||
continue
|
||||
}
|
||||
// TODO: fix this.
|
||||
return fmt.Errorf("cannot convert Puffs type %q to C", n.String(g.tm))
|
||||
}
|
||||
|
||||
fallback := true
|
||||
if key := innermost.Name().Key(); key < t.Key(len(cTypeNames)) {
|
||||
if s := cTypeNames[key]; s != "" {
|
||||
b.writes(s)
|
||||
fallback = false
|
||||
}
|
||||
}
|
||||
if fallback {
|
||||
b.printf("%s%s", g.pkgPrefix, innermost.Name().String(g.tm))
|
||||
}
|
||||
|
||||
for i := 0; i < numPointers; i++ {
|
||||
b.writeb('*')
|
||||
}
|
||||
|
||||
b.writeb(' ')
|
||||
b.writes(varNamePrefix)
|
||||
b.writes(varName)
|
||||
|
||||
x = n
|
||||
for ; x != nil && x.Decorator().Key() == t.KeyOpenBracket; x = x.Inner() {
|
||||
b.writeb('[')
|
||||
b.writes(x.ArrayLength().ConstValue().String())
|
||||
b.writeb(']')
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var numTypeBounds = [256][2]*big.Int{
|
||||
t.KeyI8: {big.NewInt(-1 << 7), big.NewInt(1<<7 - 1)},
|
||||
t.KeyI16: {big.NewInt(-1 << 15), big.NewInt(1<<15 - 1)},
|
||||
t.KeyI32: {big.NewInt(-1 << 31), big.NewInt(1<<31 - 1)},
|
||||
t.KeyI64: {big.NewInt(-1 << 63), big.NewInt(1<<63 - 1)},
|
||||
t.KeyU8: {zero, big.NewInt(0).SetUint64(1<<8 - 1)},
|
||||
t.KeyU16: {zero, big.NewInt(0).SetUint64(1<<16 - 1)},
|
||||
t.KeyU32: {zero, big.NewInt(0).SetUint64(1<<32 - 1)},
|
||||
t.KeyU64: {zero, big.NewInt(0).SetUint64(1<<64 - 1)},
|
||||
t.KeyUsize: {zero, zero},
|
||||
t.KeyBool: {zero, one},
|
||||
}
|
||||
|
||||
var cTypeNames = [...]string{
|
||||
t.KeyI8: "int8_t",
|
||||
t.KeyI16: "int16_t",
|
||||
t.KeyI32: "int32_t",
|
||||
t.KeyI64: "int64_t",
|
||||
t.KeyU8: "uint8_t",
|
||||
t.KeyU16: "uint16_t",
|
||||
t.KeyU32: "uint32_t",
|
||||
t.KeyU64: "uint64_t",
|
||||
t.KeyUsize: "size_t",
|
||||
t.KeyBool: "bool",
|
||||
t.KeyBuf1: "puffs_base_buf1",
|
||||
t.KeyBuf2: "puffs_base_buf2",
|
||||
t.KeyReader1: "puffs_base_reader1",
|
||||
t.KeyWriter1: "puffs_base_writer1",
|
||||
}
|
||||
|
||||
var cOpNames = [256]string{
|
||||
t.KeyEq: " = ",
|
||||
t.KeyPlusEq: " += ",
|
||||
t.KeyMinusEq: " -= ",
|
||||
t.KeyStarEq: " *= ",
|
||||
t.KeySlashEq: " /= ",
|
||||
t.KeyShiftLEq: " <<= ",
|
||||
t.KeyShiftREq: " >>= ",
|
||||
t.KeyAmpEq: " &= ",
|
||||
t.KeyAmpHatEq: " no_such_amp_hat_C_operator ",
|
||||
t.KeyPipeEq: " |= ",
|
||||
t.KeyHatEq: " ^= ",
|
||||
t.KeyPercentEq: " %= ",
|
||||
|
||||
t.KeyXUnaryPlus: "+",
|
||||
t.KeyXUnaryMinus: "-",
|
||||
t.KeyXUnaryNot: "!",
|
||||
|
||||
t.KeyXBinaryPlus: " + ",
|
||||
t.KeyXBinaryMinus: " - ",
|
||||
t.KeyXBinaryStar: " * ",
|
||||
t.KeyXBinarySlash: " / ",
|
||||
t.KeyXBinaryShiftL: " << ",
|
||||
t.KeyXBinaryShiftR: " >> ",
|
||||
t.KeyXBinaryAmp: " & ",
|
||||
t.KeyXBinaryAmpHat: " no_such_amp_hat_C_operator ",
|
||||
t.KeyXBinaryPipe: " | ",
|
||||
t.KeyXBinaryHat: " ^ ",
|
||||
t.KeyXBinaryPercent: " % ",
|
||||
t.KeyXBinaryNotEq: " != ",
|
||||
t.KeyXBinaryLessThan: " < ",
|
||||
t.KeyXBinaryLessEq: " <= ",
|
||||
t.KeyXBinaryEqEq: " == ",
|
||||
t.KeyXBinaryGreaterEq: " >= ",
|
||||
t.KeyXBinaryGreaterThan: " > ",
|
||||
t.KeyXBinaryAnd: " && ",
|
||||
t.KeyXBinaryOr: " || ",
|
||||
t.KeyXBinaryAs: " no_such_as_C_operator ",
|
||||
|
||||
t.KeyXAssociativePlus: " + ",
|
||||
t.KeyXAssociativeStar: " * ",
|
||||
t.KeyXAssociativeAmp: " & ",
|
||||
t.KeyXAssociativePipe: " | ",
|
||||
t.KeyXAssociativeHat: " ^ ",
|
||||
t.KeyXAssociativeAnd: " && ",
|
||||
t.KeyXAssociativeOr: " || ",
|
||||
}
|
||||
Reference in new issue
Block a user