mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Set a unary op expr's const value.
This commit is contained in:
1 parent
0e59b05432
commit
8dd151bf74
3 files changed
+23
-5
No files matched your search
@@ -115,6 +115,7 @@ type Raw Node
|
||||
func (n *Raw) Node() *Node { return (*Node)(n) }
|
||||
func (n *Raw) Kind() Kind { return n.kind }
|
||||
func (n *Raw) Flags() Flags { return n.flags }
|
||||
func (n *Raw) ConstValue() *big.Int { return n.constValue }
|
||||
func (n *Raw) MType() *TypeExpr { return n.mType }
|
||||
func (n *Raw) FilenameLine() (string, uint32) { return n.filename, n.line }
|
||||
func (n *Raw) Filename() string { return n.filename }
|
||||
|
||||
@@ -18,7 +18,7 @@ func TestCheck(t *testing.T) {
|
||||
const src = `
|
||||
func bar()() {
|
||||
var x u8
|
||||
var y i32 = 2
|
||||
var y i32 = +2
|
||||
var z u64[:123]
|
||||
var a [4]u8
|
||||
var b bool
|
||||
@@ -85,8 +85,13 @@ func TestCheck(t *testing.T) {
|
||||
}
|
||||
|
||||
walk(file.Node(), func(n *ast.Node) error {
|
||||
if n.Kind() == ast.KExpr && n.Expr().MType() == nil {
|
||||
t.Errorf("expression node has no (implicit) type: n=%v", n)
|
||||
if n.Kind() == ast.KExpr {
|
||||
e := n.Expr()
|
||||
if typ := e.MType(); typ == nil {
|
||||
t.Errorf("expression node %q has no (implicit) type", n.Expr().String(idMap))
|
||||
} else if typ == TypeExprIdealNumber && e.ConstValue() == nil {
|
||||
t.Errorf("expression node %q has ideal number type but no const value", n.Expr().String(idMap))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
+14
-2
@@ -229,7 +229,12 @@ func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error {
|
||||
return fmt.Errorf(`check: unary "%c": %q, of type %q, does not have a numeric type`,
|
||||
op, rhs.String(c.idMap), rTyp.String(c.idMap))
|
||||
}
|
||||
// TODO: SetConstValue.
|
||||
if cv := rhs.ConstValue(); cv != nil {
|
||||
if n.ID0() == t.IDXUnaryMinus {
|
||||
cv = big.NewInt(0).Neg(cv)
|
||||
}
|
||||
n.SetConstValue(cv)
|
||||
}
|
||||
n.SetMType(rTyp)
|
||||
return nil
|
||||
|
||||
@@ -238,7 +243,14 @@ func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error {
|
||||
return fmt.Errorf(`check: unary "not": %q, of type %q, does not have a boolean type`,
|
||||
rhs.String(c.idMap), rTyp.String(c.idMap))
|
||||
}
|
||||
// TODO: SetConstValue.
|
||||
if cv := rhs.ConstValue(); cv != nil {
|
||||
if cv.Cmp(zero) == 0 {
|
||||
cv = one
|
||||
} else {
|
||||
cv = zero
|
||||
}
|
||||
n.SetConstValue(cv)
|
||||
}
|
||||
n.SetMType(TypeExprBoolean)
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user