Check "b = not true".

This commit is contained in:
Nigel Tao committed 2017-05-12 16:03:35 +10:00
1 parent 5d51c6c42c
commit 0e59b05432
4 files changed
+56 -18

No files matched your search

+8 -2
View File
@@ -140,8 +140,11 @@ var opStrings = [256]string{
// node, including the sum nodes, has a ConstValue), both sums will have the
// same value and will compare equal.
func (n *Expr) Eq(o *Expr) bool {
if n == o {
return true
}
if n == nil || o == nil {
return n == nil && o == nil
return false
}
if n.constValue != nil && o.constValue != nil {
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.
func (n *TypeExpr) Eq(o *TypeExpr) bool {
for {
if n == o {
return true
}
if n == nil || o == nil {
return n == nil && o == nil
return false
}
if n.id0 != o.id0 || n.id1 != o.id1 ||
!n.lhs.Expr().Eq(o.lhs.Expr()) ||
+10 -4
View File
@@ -12,9 +12,12 @@ import (
t "github.com/google/puffs/lang/token"
)
// DummyTypeIdealNumber is an *ast.Node MType (implicit type) that means that n
// is an ideal numeric constant.
var DummyTypeIdealNumber = a.NewTypeExpr(0, t.IDIdeal, nil, nil, nil)
// TypeExprFoo is an *ast.Node MType (implicit type) that means that n is a
// boolean or a ideal numeric constant.
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.
type TypeMap map[t.ID]*a.TypeExpr
@@ -152,4 +155,7 @@ var numTypeRanges = [256][2]*big.Int{
t.IDUsize >> t.KeyShift: {zero, zero},
}
var zero = big.NewInt(0)
var (
zero = big.NewInt(0)
one = big.NewInt(1)
)
+3
View File
@@ -21,9 +21,11 @@ func TestCheck(t *testing.T) {
var y i32 = 2
var z u64[:123]
var a [4]u8
var b bool
x = 0
y = -y
b = not true
y = x as i32
}
@@ -73,6 +75,7 @@ func TestCheck(t *testing.T) {
want := [][2]string{
{"a", "[4] u8"},
{"b", "bool"},
{"x", "u8"},
{"y", "i32"},
{"z", "u64[:123]"},
+35 -12
View File
@@ -67,7 +67,7 @@ func (c *typeChecker) checkStatement(n *a.Node) error {
lTyp := l.MType()
rTyp := r.MType()
// TODO, look at o.Operator().
if (rTyp == DummyTypeIdealNumber && lTyp.IsNumType()) || lTyp.Eq(rTyp) {
if (rTyp == TypeExprIdealNumber && lTyp.IsNumType()) || lTyp.Eq(rTyp) {
return nil
}
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 {
switch n.ID0() {
case 0:
if n.ID1().IsNumLiteral() {
switch id1 := n.ID1(); {
case id1.IsNumLiteral():
z := big.NewInt(0)
s := c.idMap.ByID(n.ID1())
s := c.idMap.ByID(id1)
if _, ok := z.SetString(s, 0); !ok {
return fmt.Errorf("check: invalid numeric literal %q", s)
}
n.SetConstValue(z)
n.SetMType(DummyTypeIdealNumber)
n.SetMType(TypeExprIdealNumber)
return nil
}
if n.ID1().IsIdent() {
if typ, ok := c.typeMap[n.ID1()]; ok {
case id1.IsIdent():
if typ, ok := c.typeMap[id1]; ok {
n.SetMType(typ)
return nil
}
// 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:
@@ -205,19 +221,26 @@ func (c *typeChecker) checkExprUnaryOp(n *a.Expr) error {
switch n.ID0() {
case t.IDXUnaryPlus, t.IDXUnaryMinus:
if rTyp != DummyTypeIdealNumber && !rTyp.IsNumType() {
if rTyp != TypeExprIdealNumber && !rTyp.IsNumType() {
op := '+'
if n.ID0() == t.IDXUnaryMinus {
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))
}
// TODO: SetConstValue.
n.SetMType(rTyp)
return nil
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())
}
@@ -233,7 +256,7 @@ func (c *typeChecker) checkExprBinaryOp(n *a.Expr) error {
return err
}
lTyp := lhs.MType()
if (lTyp == DummyTypeIdealNumber || lTyp.IsNumType()) && rhs.IsNumType() {
if (lTyp == TypeExprIdealNumber || lTyp.IsNumType()) && rhs.IsNumType() {
n.SetMType(rhs)
return nil
}