mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 16:55:11 +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
|
package ast
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/big"
|
||||||
|
|
||||||
t "github.com/google/puffs/lang/token"
|
t "github.com/google/puffs/lang/token"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -73,7 +75,8 @@ type Node struct {
|
|||||||
kind Kind
|
kind Kind
|
||||||
flags Flags
|
flags Flags
|
||||||
|
|
||||||
mType *TypeExpr
|
constValue *big.Int
|
||||||
|
mType *TypeExpr
|
||||||
|
|
||||||
filename string
|
filename string
|
||||||
line uint32
|
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.
|
// For selectors, like "LHS.ID1", ID0 is IDDot.
|
||||||
type Expr Node
|
type Expr Node
|
||||||
|
|
||||||
func (n *Expr) Node() *Node { return (*Node)(n) }
|
func (n *Expr) Node() *Node { return (*Node)(n) }
|
||||||
func (n *Expr) MType() *TypeExpr { return n.mType }
|
func (n *Expr) ConstValue() *big.Int { return n.constValue }
|
||||||
func (n *Expr) ID0() t.ID { return n.id0 }
|
func (n *Expr) MType() *TypeExpr { return n.mType }
|
||||||
func (n *Expr) ID1() t.ID { return n.id1 }
|
func (n *Expr) ID0() t.ID { return n.id0 }
|
||||||
func (n *Expr) LHS() *Node { return n.lhs }
|
func (n *Expr) ID1() t.ID { return n.id1 }
|
||||||
func (n *Expr) MHS() *Node { return n.mhs }
|
func (n *Expr) LHS() *Node { return n.lhs }
|
||||||
func (n *Expr) RHS() *Node { return n.rhs }
|
func (n *Expr) MHS() *Node { return n.mhs }
|
||||||
func (n *Expr) Args() []*Node { return n.list0 }
|
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 {
|
func NewExpr(flags Flags, operator t.ID, nameLiteralSelector t.ID, lhs *Node, mhs *Node, rhs *Node, args []*Node) *Expr {
|
||||||
return &Expr{
|
return &Expr{
|
||||||
@@ -344,24 +349,26 @@ func NewContinue() *Continue {
|
|||||||
//
|
//
|
||||||
// Other ID0 values mean a (possibly package-qualified) type like "pkg.foo" or
|
// 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
|
// "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.
|
// example, the MHS for "u32[:4096]" is nil.
|
||||||
type TypeExpr Node
|
type TypeExpr Node
|
||||||
|
|
||||||
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
|
func (n *TypeExpr) Node() *Node { return (*Node)(n) }
|
||||||
func (n *TypeExpr) PackageOrDecorator() t.ID { return n.id0 }
|
func (n *TypeExpr) PackageOrDecorator() t.ID { return n.id0 }
|
||||||
func (n *TypeExpr) Name() t.ID { return n.id1 }
|
func (n *TypeExpr) Name() t.ID { return n.id1 }
|
||||||
func (n *TypeExpr) LHS() *Expr { return n.lhs.Expr() }
|
func (n *TypeExpr) ArrayLength() *Expr { return n.lhs.Expr() }
|
||||||
func (n *TypeExpr) MHS() *Expr { return n.mhs.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 (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{
|
return &TypeExpr{
|
||||||
kind: KTypeExpr,
|
kind: KTypeExpr,
|
||||||
id0: pkgOrDec,
|
id0: pkgOrDec,
|
||||||
id1: name,
|
id1: name,
|
||||||
lhs: lhs.Node(),
|
lhs: arrayLengthInclMin.Node(),
|
||||||
mhs: mhs.Node(),
|
mhs: exclMax.Node(),
|
||||||
rhs: inner.Node(),
|
rhs: inner.Node(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+29
-21
@@ -16,23 +16,6 @@ import (
|
|||||||
// is an ideal numeric constant.
|
// is an ideal numeric constant.
|
||||||
var DummyTypeIdealNumber = a.NewTypeExpr(0, t.IDIdeal, nil, nil, nil)
|
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.
|
// TypeMap maps from variable names (as token IDs) to types.
|
||||||
type TypeMap map[t.ID]*a.TypeExpr
|
type TypeMap map[t.ID]*a.TypeExpr
|
||||||
|
|
||||||
@@ -277,6 +260,12 @@ func (c *Checker) checkExprOther(n *a.Expr) error {
|
|||||||
case 0:
|
case 0:
|
||||||
// n is an identifier or a literal.
|
// n is an identifier or a literal.
|
||||||
if n.ID1().IsNumLiteral() {
|
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)
|
n.SetMType(DummyTypeIdealNumber)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -336,22 +325,41 @@ func (c *Checker) checkExprAssociativeOp(n *a.Expr) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Checker) checkTypeExpr(n *a.TypeExpr) error {
|
func (c *Checker) checkTypeExpr(n *a.TypeExpr) error {
|
||||||
for n != nil {
|
for ; n != nil; n = n.Inner() {
|
||||||
switch n.PackageOrDecorator() {
|
switch n.PackageOrDecorator() {
|
||||||
case 0:
|
case 0:
|
||||||
name := n.Name()
|
name := n.Name()
|
||||||
if name.IsNumType() || name == t.IDBool {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
if n.InclMin() != nil || n.ExclMax() != nil {
|
||||||
|
// TODO: reject.
|
||||||
|
}
|
||||||
// TODO: see if name refers to a struct type.
|
// TODO: see if name refers to a struct type.
|
||||||
return fmt.Errorf("check: %q is not a type", c.idMap.ByID(name))
|
return fmt.Errorf("check: %q is not a type", c.idMap.ByID(name))
|
||||||
|
|
||||||
case t.IDPtr:
|
case t.IDPtr:
|
||||||
n = n.Inner()
|
// No-op.
|
||||||
|
|
||||||
case t.IDOpenBracket:
|
case t.IDOpenBracket:
|
||||||
// TODO.
|
aLen := n.ArrayLength()
|
||||||
fallthrough
|
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:
|
default:
|
||||||
return fmt.Errorf("check: unrecognized node for checkTypeExpr")
|
return fmt.Errorf("check: unrecognized node for checkTypeExpr")
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ func TestCheck(t *testing.T) {
|
|||||||
func bar()() {
|
func bar()() {
|
||||||
var x u8
|
var x u8
|
||||||
var y i32 = 2
|
var y i32 = 2
|
||||||
|
var z u64[:123]
|
||||||
}
|
}
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -57,7 +58,7 @@ func TestCheck(t *testing.T) {
|
|||||||
for id, typ := range bar.LocalVars {
|
for id, typ := range bar.LocalVars {
|
||||||
got = append(got, [2]string{
|
got = append(got, [2]string{
|
||||||
idMap.ByID(id),
|
idMap.ByID(id),
|
||||||
TypeString(idMap, typ),
|
TypeExprString(idMap, typ),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
sort.Slice(got, func(i, j int) bool {
|
sort.Slice(got, func(i, j int) bool {
|
||||||
@@ -67,6 +68,7 @@ func TestCheck(t *testing.T) {
|
|||||||
want := [][2]string{
|
want := [][2]string{
|
||||||
{"x", "u8"},
|
{"x", "u8"},
|
||||||
{"y", "i32"},
|
{"y", "i32"},
|
||||||
|
{"z", "u64[:123]"},
|
||||||
}
|
}
|
||||||
if !reflect.DeepEqual(got, want) {
|
if !reflect.DeepEqual(got, want) {
|
||||||
t.Fatalf("\ngot %v\nwant %v", 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: 0x12 hex and 0b11 binary numbers.
|
||||||
//
|
//
|
||||||
// TODO: allow underscores like 0b1000_0000_1111?
|
// TODO: allow underscores like 0b1000_0000_1111?
|
||||||
|
//
|
||||||
|
// TODO: reject 0755 octal numbers.
|
||||||
j := i + 1
|
j := i + 1
|
||||||
for ; j < len(src) && numeric(src[j]); j++ {
|
for ; j < len(src) && numeric(src[j]); j++ {
|
||||||
if j-i == maxTokenSize {
|
if j-i == maxTokenSize {
|
||||||
|
|||||||
Reference in new issue
Block a user