Check "var z u64[:123]".

This commit is contained in:
Nigel Tao committed 2017-05-11 17:11:17 +10:00
1 parent c3d9ea2aa6
commit c1ce1b08de
5 files changed
+99 -38

No files matched your search

+29 -21
View File
@@ -16,23 +16,6 @@ import (
// is an ideal numeric constant.
var DummyTypeIdealNumber = a.NewTypeExpr(0, t.IDIdeal, nil, nil, nil)
// TypeString returns a string form of the type n.
func TypeString(m *t.IDMap, n *a.TypeExpr) string {
if n != nil {
switch n.PackageOrDecorator() {
case 0:
return m.ByID(n.Name())
case t.IDPtr:
return "ptr " + TypeString(m, n.Inner())
case t.IDOpenBracket:
// TODO.
default:
return m.ByID(n.PackageOrDecorator()) + "." + m.ByID(n.Name())
}
}
return "!invalid_type!"
}
// TypeMap maps from variable names (as token IDs) to types.
type TypeMap map[t.ID]*a.TypeExpr
@@ -277,6 +260,12 @@ func (c *Checker) checkExprOther(n *a.Expr) error {
case 0:
// n is an identifier or a literal.
if n.ID1().IsNumLiteral() {
z := big.NewInt(0)
s := c.idMap.ByID(n.ID1())
if _, ok := z.SetString(s, 0); !ok {
return fmt.Errorf("check: invalid numeric literal %q", s)
}
n.SetConstValue(z)
n.SetMType(DummyTypeIdealNumber)
return nil
}
@@ -336,22 +325,41 @@ func (c *Checker) checkExprAssociativeOp(n *a.Expr) error {
}
func (c *Checker) checkTypeExpr(n *a.TypeExpr) error {
for n != nil {
for ; n != nil; n = n.Inner() {
switch n.PackageOrDecorator() {
case 0:
name := n.Name()
if name.IsNumType() || name == t.IDBool {
for _, bound := range n.Bounds() {
if bound == nil {
continue
}
if err := c.checkExpr(bound); err != nil {
return err
}
if bound.ConstValue() == nil {
return fmt.Errorf("check: %q is not constant", ExprString(c.idMap, bound))
}
}
return nil
}
if n.InclMin() != nil || n.ExclMax() != nil {
// TODO: reject.
}
// TODO: see if name refers to a struct type.
return fmt.Errorf("check: %q is not a type", c.idMap.ByID(name))
case t.IDPtr:
n = n.Inner()
// No-op.
case t.IDOpenBracket:
// TODO.
fallthrough
aLen := n.ArrayLength()
if err := c.checkExpr(aLen); err != nil {
return err
}
if aLen.ConstValue() == nil {
return fmt.Errorf("check: %q is not constant", ExprString(c.idMap, aLen))
}
default:
return fmt.Errorf("check: unrecognized node for checkTypeExpr")
+3 -1
View File
@@ -19,6 +19,7 @@ func TestCheck(t *testing.T) {
func bar()() {
var x u8
var y i32 = 2
var z u64[:123]
}
`
@@ -57,7 +58,7 @@ func TestCheck(t *testing.T) {
for id, typ := range bar.LocalVars {
got = append(got, [2]string{
idMap.ByID(id),
TypeString(idMap, typ),
TypeExprString(idMap, typ),
})
}
sort.Slice(got, func(i, j int) bool {
@@ -67,6 +68,7 @@ func TestCheck(t *testing.T) {
want := [][2]string{
{"x", "u8"},
{"y", "i32"},
{"z", "u64[:123]"},
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("\ngot %v\nwant %v", got, want)
+42
View File
@@ -0,0 +1,42 @@
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.
package check
import (
a "github.com/google/puffs/lang/ast"
t "github.com/google/puffs/lang/token"
)
// ExprString returns a string form of the Expr n.
func ExprString(m *t.IDMap, n *a.Expr) string {
if n == nil {
return ""
}
if v := n.ConstValue(); v != nil {
return v.String()
}
return "ExprString.TODO"
}
// TypeExprString returns a string form of the TypeExpr n.
func TypeExprString(m *t.IDMap, n *a.TypeExpr) string {
if n != nil {
s := ""
switch n.PackageOrDecorator() {
case 0:
s = m.ByID(n.Name())
case t.IDPtr:
return "ptr " + TypeExprString(m, n.Inner())
case t.IDOpenBracket:
// TODO.
default:
s = m.ByID(n.PackageOrDecorator()) + "." + m.ByID(n.Name())
}
if n.InclMin() == nil && n.ExclMax() == nil {
return s
}
return s + "[" + ExprString(m, n.InclMin()) + ":" + ExprString(m, n.ExclMax()) + "]"
}
return "!invalid_type!"
}