mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-23 08:47:56 +01:00
Check "b = not true".
This commit is contained in:
1 parent
5d51c6c42c
commit
0e59b05432
4 files changed
+56
-18
No files matched your search
+8
-2
@@ -140,8 +140,11 @@ var opStrings = [256]string{
|
|||||||
// node, including the sum nodes, has a ConstValue), both sums will have the
|
// node, including the sum nodes, has a ConstValue), both sums will have the
|
||||||
// same value and will compare equal.
|
// same value and will compare equal.
|
||||||
func (n *Expr) Eq(o *Expr) bool {
|
func (n *Expr) Eq(o *Expr) bool {
|
||||||
|
if n == o {
|
||||||
|
return true
|
||||||
|
}
|
||||||
if n == nil || o == nil {
|
if n == nil || o == nil {
|
||||||
return n == nil && o == nil
|
return false
|
||||||
}
|
}
|
||||||
if n.constValue != nil && o.constValue != nil {
|
if n.constValue != nil && o.constValue != nil {
|
||||||
return n.constValue.Cmp(o.constValue) == 0
|
return n.constValue.Cmp(o.constValue) == 0
|
||||||
@@ -201,8 +204,11 @@ func (n *TypeExpr) String(m *t.IDMap) string {
|
|||||||
// Eq returns whether n and o are equal.
|
// Eq returns whether n and o are equal.
|
||||||
func (n *TypeExpr) Eq(o *TypeExpr) bool {
|
func (n *TypeExpr) Eq(o *TypeExpr) bool {
|
||||||
for {
|
for {
|
||||||
|
if n == o {
|
||||||
|
return true
|
||||||
|
}
|
||||||
if n == nil || o == nil {
|
if n == nil || o == nil {
|
||||||
return n == nil && o == nil
|
return false
|
||||||
}
|
}
|
||||||
if n.id0 != o.id0 || n.id1 != o.id1 ||
|
if n.id0 != o.id0 || n.id1 != o.id1 ||
|
||||||
!n.lhs.Expr().Eq(o.lhs.Expr()) ||
|
!n.lhs.Expr().Eq(o.lhs.Expr()) ||
|
||||||
|
|||||||
+10
-4
@@ -12,9 +12,12 @@ import (
|
|||||||
t "github.com/google/puffs/lang/token"
|
t "github.com/google/puffs/lang/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DummyTypeIdealNumber is an *ast.Node MType (implicit type) that means that n
|
// TypeExprFoo is an *ast.Node MType (implicit type) that means that n is a
|
||||||
// is an ideal numeric constant.
|
// boolean or a ideal numeric constant.
|
||||||
var DummyTypeIdealNumber = a.NewTypeExpr(0, t.IDIdeal, nil, nil, nil)
|
var (
|
||||||
|
TypeExprBoolean = a.NewTypeExpr(0, t.IDBool, nil, nil, nil)
|
||||||
|
TypeExprIdealNumber = a.NewTypeExpr(0, t.IDIdeal, nil, nil, nil)
|
||||||
|
)
|
||||||
|
|
||||||
// TypeMap maps from variable names (as token IDs) to types.
|
// TypeMap maps from variable names (as token IDs) to types.
|
||||||
type TypeMap map[t.ID]*a.TypeExpr
|
type TypeMap map[t.ID]*a.TypeExpr
|
||||||
@@ -152,4 +155,7 @@ var numTypeRanges = [256][2]*big.Int{
|
|||||||
t.IDUsize >> t.KeyShift: {zero, zero},
|
t.IDUsize >> t.KeyShift: {zero, zero},
|
||||||
}
|
}
|
||||||
|
|
||||||
var zero = big.NewInt(0)
|
var (
|
||||||
|
zero = big.NewInt(0)
|
||||||
|
one = big.NewInt(1)
|
||||||
|
)
|
||||||
@@ -21,9 +21,11 @@ func TestCheck(t *testing.T) {
|
|||||||
var y i32 = 2
|
var y i32 = 2
|
||||||
var z u64[:123]
|
var z u64[:123]
|
||||||
var a [4]u8
|
var a [4]u8
|
||||||
|
var b bool
|
||||||
|
|
||||||
x = 0
|
x = 0
|
||||||
y = -y
|
y = -y
|
||||||
|
b = not true
|
||||||
|
|
||||||
y = x as i32
|
y = x as i32
|
||||||
}
|
}
|
||||||
@@ -73,6 +75,7 @@ func TestCheck(t *testing.T) {
|
|||||||
|
|
||||||
want := [][2]string{
|
want := [][2]string{
|
||||||
{"a", "[4] u8"},
|
{"a", "[4] u8"},
|
||||||
|
{"b", "bool"},
|
||||||
{"x", "u8"},
|
{"x", "u8"},
|
||||||
{"y", "i32"},
|
{"y", "i32"},
|
||||||
{"z", "u64[:123]"},
|
{"z", "u64[:123]"},
|
||||||
|
|||||||
+35
-12
@@ -67,7 +67,7 @@ func (c *typeChecker) checkStatement(n *a.Node) error {
|
|||||||
lTyp := l.MType()
|
lTyp := l.MType()
|
||||||
rTyp := r.MType()
|
rTyp := r.MType()
|
||||||
// TODO, look at o.Operator().
|
// TODO, look at o.Operator().
|
||||||
if (rTyp == DummyTypeIdealNumber && lTyp.IsNumType()) || lTyp.Eq(rTyp) {
|
if (rTyp == TypeExprIdealNumber && lTyp.IsNumType()) || lTyp.Eq(rTyp) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
|
return fmt.Errorf("check: cannot assign %q of type %q to %q of type %q",
|
||||||
@@ -157,24 +157,40 @@ func (c *typeChecker) checkExpr(n *a.Expr) error {
|
|||||||
func (c *typeChecker) checkExprOther(n *a.Expr) error {
|
func (c *typeChecker) checkExprOther(n *a.Expr) error {
|
||||||
switch n.ID0() {
|
switch n.ID0() {
|
||||||
case 0:
|
case 0:
|
||||||
if n.ID1().IsNumLiteral() {
|
switch id1 := n.ID1(); {
|
||||||
|
case id1.IsNumLiteral():
|
||||||
z := big.NewInt(0)
|
z := big.NewInt(0)
|
||||||
s := c.idMap.ByID(n.ID1())
|
s := c.idMap.ByID(id1)
|
||||||
if _, ok := z.SetString(s, 0); !ok {
|
if _, ok := z.SetString(s, 0); !ok {
|
||||||
return fmt.Errorf("check: invalid numeric literal %q", s)
|
return fmt.Errorf("check: invalid numeric literal %q", s)
|
||||||
}
|
}
|
||||||
n.SetConstValue(z)
|
n.SetConstValue(z)
|
||||||
n.SetMType(DummyTypeIdealNumber)
|
n.SetMType(TypeExprIdealNumber)
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
if n.ID1().IsIdent() {
|
case id1.IsIdent():
|
||||||
if typ, ok := c.typeMap[n.ID1()]; ok {
|
if typ, ok := c.typeMap[id1]; ok {
|
||||||
n.SetMType(typ)
|
n.SetMType(typ)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// TODO: look for (global) names (constants, funcs, structs).
|
// TODO: look for (global) names (constants, funcs, structs).
|
||||||
return fmt.Errorf("check: unrecognized identifier %q", c.idMap.ByID(n.ID1()))
|
return fmt.Errorf("check: unrecognized identifier %q", c.idMap.ByID(id1))
|
||||||
|
|
||||||
|
case id1 == t.IDFalse:
|
||||||
|
n.SetConstValue(zero)
|
||||||
|
n.SetMType(TypeExprBoolean)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case id1 == t.IDTrue:
|
||||||
|
n.SetConstValue(one)
|
||||||
|
n.SetMType(TypeExprBoolean)
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case id1 == t.IDUnderscore:
|
||||||
|
// TODO.
|
||||||
|
|
||||||
|
case id1 == t.IDThis:
|
||||||
|
// TODO.
|
||||||
}
|
}
|
||||||
|
|
||||||
case t.IDOpenParen:
|
case t.IDOpenParen:
|
||||||
@@ -205,19 +221,26 @@ func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error {
|
|||||||
|
|
||||||
switch n.ID0() {
|
switch n.ID0() {
|
||||||
case t.IDXUnaryPlus, t.IDXUnaryMinus:
|
case t.IDXUnaryPlus, t.IDXUnaryMinus:
|
||||||
if rTyp != DummyTypeIdealNumber && !rTyp.IsNumType() {
|
if rTyp != TypeExprIdealNumber && !rTyp.IsNumType() {
|
||||||
op := '+'
|
op := '+'
|
||||||
if n.ID0() == t.IDXUnaryMinus {
|
if n.ID0() == t.IDXUnaryMinus {
|
||||||
op = '-'
|
op = '-'
|
||||||
}
|
}
|
||||||
return fmt.Errorf("check: unary '%c': %q, of type %q, does not have a numeric type",
|
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))
|
op, rhs.String(c.idMap), rTyp.String(c.idMap))
|
||||||
}
|
}
|
||||||
|
// TODO: SetConstValue.
|
||||||
n.SetMType(rTyp)
|
n.SetMType(rTyp)
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
case t.IDXUnaryNot:
|
case t.IDXUnaryNot:
|
||||||
// TODO: check for bool.
|
if !rTyp.Eq(TypeExprBoolean) {
|
||||||
|
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.
|
||||||
|
n.SetMType(TypeExprBoolean)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprUnaryOp", n.ID0().Key())
|
return fmt.Errorf("check: unrecognized token.Key (0x%X) for checkExprUnaryOp", n.ID0().Key())
|
||||||
}
|
}
|
||||||
@@ -233,7 +256,7 @@ func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
lTyp := lhs.MType()
|
lTyp := lhs.MType()
|
||||||
if (lTyp == DummyTypeIdealNumber || lTyp.IsNumType()) && rhs.IsNumType() {
|
if (lTyp == TypeExprIdealNumber || lTyp.IsNumType()) && rhs.IsNumType() {
|
||||||
n.SetMType(rhs)
|
n.SetMType(rhs)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user