mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 00:38:14 +01:00
Check "var z u64[:123]".
This commit is contained in:
1 parent
c3d9ea2aa6
commit
c1ce1b08de
5 files changed
+99
-38
No files matched your search
+23
-16
@@ -4,6 +4,8 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
t "github.com/google/puffs/lang/token"
|
||||
)
|
||||
|
||||
@@ -73,7 +75,8 @@ type Node struct {
|
||||
kind Kind
|
||||
flags Flags
|
||||
|
||||
mType *TypeExpr
|
||||
constValue *big.Int
|
||||
mType *TypeExpr
|
||||
|
||||
filename string
|
||||
line uint32
|
||||
@@ -158,16 +161,18 @@ func (n *Raw) SetFilenameLine(f string, l uint32) { n.filename, n.line = f, l }
|
||||
// For selectors, like "LHS.ID1", ID0 is IDDot.
|
||||
type Expr Node
|
||||
|
||||
func (n *Expr) Node() *Node { return (*Node)(n) }
|
||||
func (n *Expr) MType() *TypeExpr { return n.mType }
|
||||
func (n *Expr) ID0() t.ID { return n.id0 }
|
||||
func (n *Expr) ID1() t.ID { return n.id1 }
|
||||
func (n *Expr) LHS() *Node { return n.lhs }
|
||||
func (n *Expr) MHS() *Node { return n.mhs }
|
||||
func (n *Expr) RHS() *Node { return n.rhs }
|
||||
func (n *Expr) Args() []*Node { return n.list0 }
|
||||
func (n *Expr) Node() *Node { return (*Node)(n) }
|
||||
func (n *Expr) ConstValue() *big.Int { return n.constValue }
|
||||
func (n *Expr) MType() *TypeExpr { return n.mType }
|
||||
func (n *Expr) ID0() t.ID { return n.id0 }
|
||||
func (n *Expr) ID1() t.ID { return n.id1 }
|
||||
func (n *Expr) LHS() *Node { return n.lhs }
|
||||
func (n *Expr) MHS() *Node { return n.mhs }
|
||||
func (n *Expr) RHS() *Node { return n.rhs }
|
||||
func (n *Expr) Args() []*Node { return n.list0 }
|
||||
|
||||
func (n *Expr) SetMType(x *TypeExpr) { n.mType = x }
|
||||
func (n *Expr) SetConstValue(x *big.Int) { n.constValue = x }
|
||||
func (n *Expr) SetMType(x *TypeExpr) { n.mType = x }
|
||||
|
||||
func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mhs *Node, rhs *Node, args []*Node) *Expr {
|
||||
return &Expr{
|
||||
@@ -344,24 +349,26 @@ func NewContinue() *Continue {
|
||||
//
|
||||
// Other ID0 values mean a (possibly package-qualified) type like "pkg.foo" or
|
||||
// "foo". ID0 is the "pkg" or zero, ID1 is the "foo". Such a type can be
|
||||
// refined as "pkg.foo[LHS:MHS]". LHS and MHS are Expr's, possibly nil. For
|
||||
// refined as "foo[LHS:MHS]". LHS and MHS are Expr's, possibly nil. For
|
||||
// example, the MHS for "u32[:4096]" is nil.
|
||||
type TypeExpr Node
|
||||
|
||||
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
|
||||
func (n *TypeExpr) PackageOrDecorator() t.ID { return n.id0 }
|
||||
func (n *TypeExpr) Name() t.ID { return n.id1 }
|
||||
func (n *TypeExpr) LHS() *Expr { return n.lhs.Expr() }
|
||||
func (n *TypeExpr) MHS() *Expr { return n.mhs.Expr() }
|
||||
func (n *TypeExpr) ArrayLength() *Expr { return n.lhs.Expr() }
|
||||
func (n *TypeExpr) Bounds() [2]*Expr { return [2]*Expr{n.lhs.Expr(), n.mhs.Expr()} }
|
||||
func (n *TypeExpr) InclMin() *Expr { return n.lhs.Expr() }
|
||||
func (n *TypeExpr) ExclMax() *Expr { return n.mhs.Expr() }
|
||||
func (n *TypeExpr) Inner() *TypeExpr { return n.rhs.TypeExpr() }
|
||||
|
||||
func NewTypeExpr(pkgOrDec t.ID, name t.ID, lhs *Expr, mhs *Expr, inner *TypeExpr) *TypeExpr {
|
||||
func NewTypeExpr(pkgOrDec t.ID, name t.ID, arrayLengthInclMin *Expr, exclMax *Expr, inner *TypeExpr) *TypeExpr {
|
||||
return &TypeExpr{
|
||||
kind: KTypeExpr,
|
||||
id0: pkgOrDec,
|
||||
id1: name,
|
||||
lhs: lhs.Node(),
|
||||
mhs: mhs.Node(),
|
||||
lhs: arrayLengthInclMin.Node(),
|
||||
mhs: exclMax.Node(),
|
||||
rhs: inner.Node(),
|
||||
}
|
||||
}
|
||||
|
||||
+29
-21
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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!"
|
||||
}
|
||||
@@ -173,6 +173,8 @@ loop:
|
||||
// TODO: 0x12 hex and 0b11 binary numbers.
|
||||
//
|
||||
// TODO: allow underscores like 0b1000_0000_1111?
|
||||
//
|
||||
// TODO: reject 0755 octal numbers.
|
||||
j := i + 1
|
||||
for ; j < len(src) && numeric(src[j]); j++ {
|
||||
if j-i == maxTokenSize {
|
||||
|
||||
Reference in new issue
Block a user